Introduction
The process of sending email from the application should be as simple as creating an instance of a class, assigning the properties and invoking a method to send the email. To its simplest way, the method could return a boolean to indicate the success of the operation. This is what we will cover in this introductory article on this topic.
The class
The class should include two imports. One is the System.Net.Mail which makes available the email component. The second is System.IO which is needed to verify if a file exists before trying to add it in the list of attachments to the email.
The following properties should be made available in order for the client to be able to assign the values to each of them, where applicable:
The class contains one method which is the method to send the email:
Here is the class:
Imports System.Net.Mail Imports System.IO Public Class InternetSendEmail Public cSMTP As String = "" Public cFrom As String = "" Public cFromName As String = "" Public cSubject As String = "" Public cTo As String = "" Public cToName As String = "" Public cBody As String = "" Public cCC As String = "" Public cCCName As String = "" Public cBCC As String = "" Public cBCCName As String = "" Public cFile As String = "" ' Send an email Public Function SendEmail() As Boolean Dim llSuccess As Boolean Dim loEmail As MailMessage Dim loFrom As New MailAddress(cFromName + " <" + cFrom + ">") Dim loSMTP As SmtpClient = New SmtpClient Dim lnCounter As Integer = 0 llSuccess = False loEmail = New MailMessage loEmail.IsBodyHtml = False loEmail.From = loFrom ' To For lnCounter = 1 To Framework.StringFunction.ParmCnt(cTo) Dim loTo As New _ MailAddress(Framework.StringFunction.GetParm(cToName, lnCounter) + _ " <" + Framework.StringFunction.GetParm(cTo, lnCounter) + ">") loEmail.To.Add(loTo) Next ' CC If cCC.Length > 0 Then For lnCounter = 1 To Framework.StringFunction.ParmCnt(cCC) Dim loCC As New _ MailAddress(Framework.StringFunction.GetParm(cCCName, lnCounter) + _ " <" + Framework.StringFunction.GetParm(cCC, lnCounter) + ">") loEmail.CC.Add(loCC) Next End If ' BCC If cBCC.Length > 0 Then For lnCounter = 1 To Framework.StringFunction.ParmCnt(cBCC) Dim loBCC As New _ MailAddress(Framework.StringFunction.GetParm(cBCCName, lnCounter) + _ " <" + Framework.StringFunction.GetParm(cBCC, lnCounter) + ">") loEmail.Bcc.Add(loBCC) Next End If loEmail.Subject = cSubject loEmail.Body = cBody loSMTP.Host = cSMTP ' If we have an attachment If Len(cFile) > 0 Then For lnCounter = 1 To Framework.StringFunction.ParmCnt(cFile) Dim loAttachment As Attachment = New _ Attachment(Framework.StringFunction.GetParm(cFile, lnCounter)) loEmail.Attachments.Add(loAttachment) Next End If Try loSMTP.Send(loEmail) llSuccess = True Catch loError As Exception App.ErrorSetup(loError) End Try Return llSuccess End Function End Class
Invoking the class
From your application, whenever you want to benefit of this class, simply define an object as follow:
Dim loInternetSendEmail As Framework.InternetSendEmail = New Framework.InternetSendEmail()
As I mentioned at the beginning of the article, the next step should be about assigning the values to the object properties. The following code shows the minimum setting for sending an email:
loInternetSendEmail.cSMTP = "100.100.100.100" loInternetSendEmail.cFrom = "mfournier@levelextreme.com" loInternetSendEmail.cFromName = "Michel Fournier" loInternetSendEmail.cSubject = "This is the subject" loInternetSendEmail.cTo = "info@somewhere.com,info@somewhere2.com" loInternetSendEmail.cToName = "Name1,Name2" loInternetSendEmail.cBody = "This is the content of the email." loInternetSendEmail.cCC = "info@somewhere3.com" loInternetSendEmail.cCCName = "Name3" loInternetSendEmail.cFile = "d:\char.jpg,d:\taxe.txt"
If loInternetSendEmail.SendEmail() = False Then MessageBox.Show(Framework.App.cError, "Sending email", MessageBoxButtons.OK, _ MessageBoxIcon.Stop) End If
Conclusion
As you can see, sending an email with the new .NET components is real easy. Of course, this simple class can be enhanced in various ways to fit your own needs. I hope this article has provided some valuable information in you were in such needs to send email from your applications.