The System.Net.Mail namespace contains classes used to send electronic mail to a Simple Mail Transfer Protocol (SMTP) server for delivery. The most important classes of System.Net.Mail namespace are as follows:
- MailMessage Classs
- SmtpClient Class
- Attachment Class
MailMessage Class:
Instances of the MailMessage class are used to construct e-mail messages that are transmitted to an SMTP server for delivery using the SmtpClient class.
To specify the sender, recipient, and contents of an e-mail message, use the associated properties of the MailMessage class.Use the AlternateViews property to specify copies of an e-mail message in different formats. For example, if you send a message in HTML, you might also want to provide a plain text version in case some of the recipients use e-mail readers that cannot display HTML content.
After assembling your e-mail message, you can send it by using the Send or SendAsync methods.
SmtpClient Class:
The SmtpClient class is used to send e-mail to an SMTP server for delivery. The following classes shown below are used to construct e-mail messages that can be sent using SmtpClient.
- Attachment:
Represents file attachments. This class allows you to attach files, streams, or text to an e-mail message.
- MailAddress:
Represents the e-mail address of the sender and recipients.
- MailMessage:That is describe above.
To include an attachment with an e-mail message, first create the attachment by using the Attachment class, and then add it to the message by using the Message.Attachments property. Depending on the e-mail reader used by the recipients and the file type of the attachment, some recipients might not be able to read the attachment.
You can use the application or machine configuration files to specify default host, port, and credentials values for all SmtpClient objects. Below shows how you can set it.
<configuration>
<system.net>
<mailSettings>
<smtp deliveryMethod="network">
<network host="localhost" port="25" defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>
</configuration>
To send the e-mail message and block while waiting for the e-mail to be transmitted to the SMTP server, use one of the synchronous Send methods. To allow your program's main thread to continue executing while the e-mail is transmitted, use one of the asynchronous SendAsync methods. The SendCompleted event is raised when a SendAsync operation completes. To receive this event, you must add a SendCompletedEventHandlerdelegate to SendCompleted. The SendCompletedEventHandler delegate must reference a callback method that handles notification of SendCompleted events. To cancel an asynchronous e-mail transmission, use the SendAsynCancel method.
The code shown below gives you complete understanding how you can send Mail using System.Net.Mail namespace Asynchroneously.
Imports System
Imports System.Net
Imports System.Net.Mail
Imports System.Net.Mime
Imports System.Threading
Imports System.ComponentModel
Namespace MailHelper
Public Class SimpleAsynchronous
Public Shared mailSent As Boolean = False
Public Shared Sub SendCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
' Get the unique identifier for this asynchronous operation.
Dim token As String = e.UserState
If e.Cancelled Then
'Show Message
ShowMessage("Sending Mail Canceled : Token No : " & token)
End If
If Not e.Error Is Nothing Then
ShowMessage("Error occured", e.Error.ToString())
Else
ShowMessage("Message Sent")
End If
End Sub
Public Shared Sub Process_Mail(ByVal sender As String, ByVal sender_name As String, ByVal recipent As String, ByVal _subject As String, ByVal body As String)
Dim client As New SmtpClient()
'Specify the e-mail sender.
Dim from As MailAddress = New MailAddress(sender, sender_name)
'Set Destination Address
Dim _to As MailAddress = New MailAddress(recipent)
'Specify message content
Dim message As MailMessage = New MailMessage(from, _to)
message.Body = body
message.BodyEncoding = System.Text.Encoding.UTF8
message.Subject = "test message 1"
message.SubjectEncoding = System.Text.Encoding.UTF8
' Set the method that is called back when the send operation ends.
AddHandler client.SendCompleted, AddressOf SendCompletedCallback
' The userState can be any object that allows your callback
' method to identify this send operation.
'For this example, the userToken is a string constant.
Dim userState As String = _subject
client.SendAsync(message, userState)
ShowMessage("Sending Message ....")
'// Performe condition here if you want to cancel e-mail sending operation.
if .... any condition then
client.SendAsyncCancel()
End If
message.Dispose()
ShowMessage("Good Bye")
End Sub
End Namespace
You can use the following helper class in your code for sending mail as follows:
Protected Sub Btn_Send_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
SimpleAsynchronous.Process_Mail("info@iepak.com", "Shane", "recipend@iepak.com", "Sample Message", "Sample Body")
End Sub