Click here to Skip to main content
Licence 
First Posted 3 Mar 2004
Views 341,778
Bookmarked 77 times

Send mail using System.Web.Mail namespace

By | 3 Mar 2004 | Article
This tutorial explains how to send mails with attachments

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.

' 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", "Manoj@geinetech.net")
'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. If you have any queries, mail it to manoj@geinetech.net.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

ManojRajan

Web Developer

United States United States

Member

I'm ManojRajan, Working as a consultant architect in Tennessee. I have more than 8 years of experience in Microsoft Technologies.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalsending email via .net web site code PinmemberParth201223:24 27 Feb '11  
GeneralPlz help! Not able to fix this error Pinmembernimi_201017:05 13 Dec '10  
GeneralError of Sending email PinmemberMember 75740802:50 6 Nov '10  
GeneralUrgent help needed on this article [modified] PinmemberAdeyemi1:28 27 Jul '09  
GeneralSending emails without IIS PinmemberMember 371995021:23 9 Jun '09  
General2 things need to discuss Pinmemberel3ashe20:41 30 May '09  
QuestionError while running the code PinmemberBhuvan941122:44 23 Apr '09  
AnswerRe: Error while running the code Pinmemberel3ashe20:42 30 May '09  
QuestionTwo errors found.. Pinmembervinayweb2:13 31 Oct '08  
AnswerRe: Two errors found.. PinmemberMember 363240311:40 15 Dec '08  
GeneralPocket Pc mail Pinmemberserkan312322:19 24 Jun '07  
Hi,firstly thank you very much for this informations.But I have a another question about pocket pc development.I want to send and receive mail in Pocket Pc,I wrote only send part of program.How can I write recive part because there is no namespaces about receive mail (in vs .net 2005). If you have any comment abou tthis problem pls help...
thank you very much

 
seko
GeneralImports System.Web.mail is not available in the namespace PinmemberSelvanPPK23:17 22 Jun '07  
GeneralRe: Imports System.Web.mail is not available in the namespace Pinmemberhevesir22:25 29 Jul '07  
GeneralThanks! Pinmembermorry8020:16 9 May '07  
Generalhi Pinmemberhebry23:51 18 Mar '07  
QuestionFormatting text using MailFormat.Html Pinmemberbooboo13alright7:16 9 Nov '06  
Generalif there's a solution for CDO.message problem , reply me:) Pinmembershery_bn20:12 12 Sep '06  
QuestionPlease help me! Pinmembertuonginfo9:50 2 Sep '06  
Generali want Help... Pinmembermadhan_genn0:58 29 Jul '06  
QuestionCDO.Message Pinmemberparigi_anantha0:56 25 Jul '06  
GeneralNo errors while mailing but, no mails also Pinmemberronnienk21:06 20 May '06  
GeneralProblem using Proxy Server Pinmembertechtrix2:59 18 May '06  
GeneralHELP! I can't receive the email if sent to Yahoo. Pinmemberardiaz17:05 9 Apr '06  
QuestionSMTP Server Authentication : How To? PinmemberMehdiAnis4:36 7 Mar '06  
AnswerRe: SMTP Server Authentication : How To? PinmemberSantanu Biswas6:11 1 Sep '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 4 Mar 2004
Article Copyright 2004 by ManojRajan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid