Send Mail with Attachment Command Line Utility (.NET)

Download PDF Version

Send Mail with attachment command line utility can be used for various purposes. I have written this utility to take the schedule backups of my software database in email. I use this utility along with windows scheduler.

This is a command line based utility, which also supports debug mode to check errors. Utility uses SmtpClient Class for sending E-Mail. I have tested this utility with Gmail account.

When using with Gmail account you will encounter a problem “The SMTP server requires a secure connection or the client was not authenticated.” I got this problem solved after resetting my Gmail account password with a strong password.

Code Section

[sourcecode language=”vbnet” title=”SendMail Module”]
Imports System
Imports System.Net
Imports System.Net.Mail
Imports System.Net.Mime
Imports System.Threading
Imports System.ComponentModel</code>

Module Module1

Sub Main(ByVal args() As String)

CreateMessageWithAttachment("smtp.gmail.com", 587, args(0), args(1)) ‘ Host Name, Port Number, Send File Path including file name in quotes, Subject
If args(2) = "D" Then ‘ D for Debug Mode
Console.ReadKey()
End If

End Sub

Public Sub CreateMessageWithAttachment(ByVal hst As String, ByVal prt As Int32, ByVal sendfile As String, ByVal Subj As String)
‘ Specify the file to be attached and sent.
‘ This example assumes that a file named Data.xls exists in the
‘ current working directory.
Dim file As String = sendfile
‘ Create a message and set up the recipients.
Dim message As New MailMessage("Test Backup Software", "receiver1@gmail.com,receiver2@gmail.com", Subj, "See the attached File.")

‘ Create the file attachment for this e-mail message.
Dim data As New Attachment(file, MediaTypeNames.Application.Octet)
‘ Add time stamp information for the file.
Dim disposition As ContentDisposition = data.ContentDisposition
disposition.CreationDate = IO.File.GetCreationTime(file)
disposition.ModificationDate = IO.File.GetLastWriteTime(file)
disposition.ReadDate = IO.File.GetLastAccessTime(file)
‘ Add the file attachment to this e-mail message.
message.Attachments.Add(data)

‘Send the message.
Dim client As New SmtpClient(hst, prt)
‘ Add credentials if the SMTP server requires them.

client.Credentials = New NetworkCredential("senduser@gmail.com", "password of user")
client.EnableSsl = True
client.Timeout = 300

Try
client.Send(message)
Catch ex As Exception
Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}", ex.ToString())
End Try
‘ Display the values in the ContentDisposition for the attachment.
Dim cd As ContentDisposition = data.ContentDisposition
Console.WriteLine("Content disposition")
Console.WriteLine(cd.ToString())
Console.WriteLine("File {0}", cd.FileName)
Console.WriteLine("Size {0}", cd.Size)
Console.WriteLine("Creation {0}", cd.CreationDate)
Console.WriteLine("Modification {0}", cd.ModificationDate)
Console.WriteLine("Read {0}", cd.ReadDate)
Console.WriteLine("Inline {0}", cd.Inline)
Console.WriteLine("Parameters: {0}", cd.Parameters.Count)
For Each d In cd.Parameters
Console.WriteLine("{0} = {1}", d.Key, d.Value)
Next d
data.Dispose()
End Sub
End Module
[/sourcecode]

Execution Commands

With Debugging:

SendMail.exe “c:\OMS Base (16.06.09)\Database\oms.mdb” “Software Database Backup” D

Without Debugging:

SendMail.exe “c:\OMS Base (16.06.09)\Database\oms.mdb” “Software Database Backup” N

Sample Output