Most developers likes to provide their applications with sending mails feature which became no much easier by using .NET Framework 2.0

In this article I’ll go through System.Net.Mail Namespace to explain how to create your e-mail then sending it.

mailSending

Creating MailMessage Object

First Step to do is creating an instance of MailMessage Class , which has four types of constructors, or you can specify the prosperities as how will shown here.

VB.NET

Dim mm As New System.Net.Mail.MailMessage

C#

public System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage();

The major properties of MailMessage Class are:

Property Name Description Data Type
From Sender’s email address MailAddress
To Recipient(s) Email Address MailAddress
CC Carbon Copies MailAddress
BCC Blind Carbon Copies MailAddress
Subject Subject of the Email String
Body Body of the Email (The Message) String
IsBodyHTML Specify whether body contains text or HTML mark up Boolean (True/False)

MailMessage has the following less frequently used properties:

Property Name Description Data Type
Attachments Attachments if any Attachment
ReplyTo e-mail Address that replies will sent to. MailAddress
Priority Priority of e-mail Enumerator
DeliveryNotivicationOption Instructs the SMTP server to send message on delay, success or fails Enumerator

 

VB.NET

 mm.From = New MailAddress(TextBox1.Text)       
 mm.To.Add(New MailAddress(TextBox2.Text))        
 mm.CC.Add(New MailAddress(TextBox3.Text))         
 mm.Bcc.Add(New MailAddress(TextBox4.Text))       
 mm.Subject = TextBox5.Text       
 mm.Body = TextBox6.Text       
 If CheckBox1.Checked = True Then           
    mm.IsBodyHtml = True       
 Else           
    mm.IsBodyHtml = False       
 End If       
 mm.Attachments.Add(New Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName))         mm.Priority = MailPriority.Normal       
 mm.ReplyTo = New MailAddress("email@host.com")       
 mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure

C#

Mail Sending

The second step is sending the mailMessage object by using SMTP, you can define the SMTP information inside web.config file to be applied to the whole application otherwise doing that in code behind.

VB.NET

Dim smtp As New System.Net.Mail.SmtpClient

C#

System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage();

Properties of  SmtpClient Class that would be used are:

Property Name Description Data Type
Host Your SMTP Server String
Port Port Number of the SMTP Server Integer
EnableSSL Specify whether your host accept SSL connection Boolean
Credentials Valid login credentials for the SMTP Server Class
UseDefaultCredentials Set True if used Boolean

VB.NET

 smtp.Host = "smtp server name"        
 smtp.Port = 587  'port number       
 smtp.EnableSsl = True       
 Dim crdl As New System.Net.NetworkCredential       
 crdl.UserName = "userName"       
 crdl.Password = "password"       
 smtp.Credentials = crdl

After applying all SMTP Property you are now able to send the mail by using Send Method, see example below

VB.NET

smtp.Send(mm)

C#

smtp.Send(mm);

But you have to know that sending e-mails requires some time to be done, and to avoid waiting for e-mail sending time and if server not reachable, there is an advice to use sending mail method in a background thread.. like how shown below :

VB.NET

Dim threadSendMails As Thread        
threadSendMails = New Thread(AddressOf sendmail)        
threadSendMails.IsBackground = True        
threadSendMails.Start()  ' Mehod of sending mail    

Private Sub sendMail()       
 smtp.Send(mm)    
End Sub

C#

Thread threadSendMails; threadSendMails = new Thread(delegate() {     sendemail(mm);
     threadSendMails.IsBackground = true;     threadSendMails.Start(); }

Hope that article was helped you to understand the methodology of sending mails using .NET Framework 2.0

Happy Proramming :)
Rami M. Nassar