Introduction
The System.Web.Mail
namespace provides the classes for sending email in .NET. This tutorial explains how to send emails with attachments.
Class: MailMessage
manages the mail message contents.
Properties
Attachment
specifies the list of the attachments that are transmitted with the message.
Bcc
is a list of semicolon delimited email addresses that receive a Blind Carbon Copy of the message.
Body
contains the message text that has to be sent.
BodyEncoding
is the encoding type of the email message.
BodyFormat
defines the content type of the body of the message.
Cc
is a list of semicolon delimited email addresses that receive a Carbon Copy of the message.
From
is the email address of the sender.
Header
specifies the custom headers which are transmitted with the Message
.
Priority
is the priority of the email message.
Subject
is the subject line of the email message.
To
is the email address of the recipient.
Class: MailAttachments
manages the mail attachment.
Class: SmtpMail
sends email to the mail server.
The code
Let us see it step by step:
Create a Visual Basic .NET application and drop the following controls and set the properties accordingly.
Control |
Property |
Label Text |
SMTP Server |
TextBox Name |
txtSMTPServer |
Label Text |
From |
TextBox Name |
txtFrom |
Label Text |
From Display Name |
TextBox Name |
txtFromDisplayName |
Label Text |
Recipient |
TextBox txtTo |
|
Label Text |
Attachment |
ListBox Name |
lstAttachment |
Label Text |
Subject |
TextBox Name |
txtSubject |
|
|
Label Text |
Message |
TextBox Name |
txtMessage |
Multiline |
True |
Scrollbars |
Both |
|
|
Button Text |
Add attachment |
Name |
BtnAdd |
|
|
Button Text |
Remove attachment |
Name |
btnRemove |
|
|
Button Text |
Send |
Name |
btnSend |
|
|
CheckBox Text |
Send As HTML |
Name |
chkFormat |
|
|
OpenFileDialog Name |
OFD |
DefaultExt |
*.* |
InitialDirectory |
c:\ |
Multiselect |
True |
Now let us see the coding part
Invoke the code window and type the following statement above the class declaration.
Imports System.Web.Mail
Within the Class declaration, in the general section, declare variables required for this project.
Dim obj As System.Web.Mail.SmtpMail
Dim Attachment As System.Web.Mail.MailAttachment
Dim Mailmsg As New System.Web.Mail.MailMessage()
Double click on the "Add attachment" button to add the code. Type the following lines:
Dim Counter As Integer
OFD.CheckFileExists = True
OFD.Title = "Select file(s) to attach"
OFD.ShowDialog()
For Counter = 0 To UBound(OFD.FileNames)
lstAttachment.Items.Add(OFD.FileNames(Counter))
Next
Double click on the "Remove attachment" button. Type the following lines:
If lstAttachment.SelectedIndex > -1 Then
lstAttachment.Items.RemoveAt(lstAttachment.SelectedIndex)
End If
Double click on the "Send" button. Type the following lines:
Dim Counter As Integer
If txtSMTPServer.Text = "" Then
MsgBox("Enter the SMTP server info ...!!!", _
MsgBoxStyle.Information, "Send Email")
Exit Sub
End If
If txtFrom.Text = "" Then
MsgBox("Enter the From email address ...!!!", _
MsgBoxStyle.Information, "Send Email")
Exit Sub
End If
If txtTo.Text = "" Then
MsgBox("Enter the Recipient email address ...!!!", _
MsgBoxStyle.Information, "Send Email")
Exit Sub
End If
If txtSubject.Text = "" Then
MsgBox("Enter the Email subject ...!!!", _
MsgBoxStyle.Information, "Send Email")
Exit Sub
End If
obj.SmtpServer = txtSMTPServer.Text
Mailmsg.To = txtTo.Text
Mailmsg.From = "\" & txtFromDisplayName.Text & "\ <" & txtFrom.Text & ">"
If chkFormat.Checked = True Then
Mailmsg.BodyFormat = MailFormat.Html Else
Mailmsg.BodyFormat = MailFormat.Text
End If
Mailmsg.Subject = txtSubject.Text
For Counter = 0 To lstAttachment.Items.Count - 1
Attachment = New MailAttachment(lstAttachment.Items(Counter))
Mailmsg.Attachments.Add(Attachment)
Next
Mailmsg.Body = txtMessage.Text
obj.Send(Mailmsg)
This application is now ready to run, try it.