Click here to Skip to main content
Full site     10M members (39.8K online)    

Send mail using System.Web.Mail namespace

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

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.

' Variable which will send the mail
Dim obj As System.Web.Mail.SmtpMail

'Variable to store the attachments 
Dim Attachment As System.Web.Mail.MailAttachment 

'Variable to create the message to send
Dim Mailmsg As New System.Web.Mail.MailMessage()

Double click on the "Add attachment" button to add the code. Type the following lines:

'Show open dialogue box to select the files to attach

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:

'Remove the attachments
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

'Validate the data
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

'Set the properties
'Assign the SMTP server
obj.SmtpServer = txtSMTPServer.Text

'Multiple recepients can be specified using ; as the delimeter
'Address of the recipient
Mailmsg.To = txtTo.Text

'Your From Address
'You can also use a custom header Reply-To for a different replyto address
Mailmsg.From = "\" & txtFromDisplayName.Text & "\ <" & txtFrom.Text & ">"

'Specify the body format
If chkFormat.Checked = True Then
  Mailmsg.BodyFormat = MailFormat.Html 'Send the mail in HTML Format
Else
  Mailmsg.BodyFormat = MailFormat.Text
End If

'If you want you can add a reply to header 
'Mailmsg.Headers.Add("Reply-To", "testmail@mail.com")
'custom headersare added like this
'Mailmsg.Headers.Add("Manoj", "TestHeader")

'Mail Subject
Mailmsg.Subject = txtSubject.Text

'Attach the files one by one
For Counter = 0 To lstAttachment.Items.Count - 1
  Attachment = New MailAttachment(lstAttachment.Items(Counter))
  'Add it to the mail message
  Mailmsg.Attachments.Add(Attachment)
Next

'Mail Body
Mailmsg.Body = txtMessage.Text

'Call the send method to send the mail
obj.Send(Mailmsg)

This application is now ready to run, try it. 

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search 
Per page   
GeneralMy vote of 1
Vasudevan Deepak Kumar
23 Apr '13 - 4:14 
Generalsending email via .net web site code
Parth2012
27 Feb '11 - 23:24 
GeneralPlz help! Not able to fix this error
nimi_2010
13 Dec '10 - 17:05 
GeneralError of Sending email
Member 7574080
6 Nov '10 - 2:50 
GeneralUrgent help needed on this article [modified]
Adeyemi
27 Jul '09 - 1:28 
GeneralSending emails without IIS
Member 3719950
9 Jun '09 - 21:23 
General2 things need to discuss
el3ashe2
30 May '09 - 0:41 
QuestionError while running the code
Bhuvan9411
23 Apr '09 - 22:44 
AnswerRe: Error while running the code
el3ashe2
30 May '09 - 0:42 
QuestionTwo errors found..
vinayweb
31 Oct '08 - 2:13 
AnswerRe: Two errors found..
Member 3632403
15 Dec '08 - 11:40 
GeneralPocket Pc mail
serkan3123
24 Jun '07 - 22:19 
GeneralImports System.Web.mail is not available in the namespace
SelvanPPK
22 Jun '07 - 23:17 
GeneralRe: Imports System.Web.mail is not available in the namespace
hevesir
29 Jul '07 - 22:25 
GeneralThanks!
morry80
9 May '07 - 20:16 
Generalhi
hebry
18 Mar '07 - 23:51 
QuestionFormatting text using MailFormat.Html
booboo13alright
9 Nov '06 - 7:16 
Generalif there's a solution for CDO.message problem , reply me:)
shery_bn
12 Sep '06 - 20:12 
QuestionPlease help me!
tuonginfo
2 Sep '06 - 9:50 
Generali want Help...
madhan_genn
29 Jul '06 - 0:58 
QuestionCDO.Message
parigi_anantha
25 Jul '06 - 0:56 
GeneralNo errors while mailing but, no mails also
ronnienk
20 May '06 - 21:06 
GeneralProblem using Proxy Server
techtrix
18 May '06 - 2:59 
GeneralHELP! I can't receive the email if sent to Yahoo.
ardiaz
9 Apr '06 - 17:05 
QuestionSMTP Server Authentication : How To?
MehdiAnis
7 Mar '06 - 4:36 
AnswerRe: SMTP Server Authentication : How To?
Santanu Biswas
1 Sep '07 - 6:11 
GeneralRe: SMTP Server Authentication : How To?
Void I.T.O.
4 Dec '07 - 9:01 
GeneralRe: SMTP Server Authentication : How To?
el3ashe2
17 Aug '09 - 21:25 
GeneralProblems with References
kostasdiktia2
9 Feb '06 - 10:22 
GeneralRe: Problems with References
Steveoyoung1
29 Aug '07 - 5:29 
GeneralRe: Problems with References
kostasdiktia2
29 Aug '07 - 10:18 
Questionhow to send a mail to multiply address
stl0
6 Feb '06 - 16:33 
AnswerRe: how to send a mail to multiply address
ManojRajan
6 Feb '06 - 17:08 
AnswerRe: how to send a mail to multiply address
EnderWiggins
8 Jul '08 - 20:58 
GeneralOFD not found in toolbox
Shibe
15 Jan '06 - 22:20 
AnswerRe: OFD not found in toolbox
ManojRajan
15 Jan '06 - 23:01 
QuestionCOULD NOT ACCESS CDO.MESSAGE OBJECT
imdeepakpathak
14 Oct '05 - 1:10 
GeneralNo errors, no email
ilyail3
18 Sep '05 - 18:32 
GeneralCDO error
fcyjk,kl;
24 Aug '05 - 23:30 
GeneralAttachments doesnt fit. Help me pls
Anomymos
3 Jun '05 - 2:01 
Generalhin frnz, i need ur help
erlakhangarg
23 May '05 - 4:26 
GeneralRe: hin frnz, i need ur help
Shady Aly
24 May '05 - 21:56 
GeneralSend mail with no SMTP service available
Egy_Karim
3 Apr '05 - 10:40 
GeneralRe: Send mail with no SMTP service available
Egy_Karim
6 Apr '05 - 12:13 
GeneralRe: Send mail with no SMTP service available
12dilip12
4 Nov '05 - 20:04 
GeneralSystem.Web.Mail Error
PankajBanga
7 Mar '05 - 11:39 
GeneralRe: System.Web.Mail Error
Dejital1
23 Mar '05 - 5:22 
QuestionSMTP Server?
pleet
26 Dec '04 - 16:47 
GeneralThank You
Anonymous
31 Aug '04 - 23:22 
Generalerror checking makes it crash.
sameerashrafourkia
12 Jul '04 - 1:20 

Last Updated 23 Apr 2013 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2013