Skip to main content
Email Password   helpLost your password?

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", "sample@sample.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.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralUrgent help needed on this article [modified] Pin
Adeyemi
2:28 27 Jul '09  
GeneralSending emails without IIS Pin
Member 3719950
22:23 9 Jun '09  
General2 things need to discuss Pin
el3ashe2
1:41 30 May '09  
QuestionError while running the code Pin
Bhuvan9411
23:44 23 Apr '09  
AnswerRe: Error while running the code Pin
el3ashe2
1:42 30 May '09  
QuestionTwo errors found.. Pin
vinayweb
3:13 31 Oct '08  
AnswerRe: Two errors found.. Pin
Member 3632403
12:40 15 Dec '08  
GeneralPocket Pc mail Pin
serkan3123
23:19 24 Jun '07  
GeneralImports System.Web.mail is not available in the namespace Pin
SelvanPPK
0:17 23 Jun '07  
GeneralRe: Imports System.Web.mail is not available in the namespace Pin
hevesir
23:25 29 Jul '07  
GeneralThanks! Pin
morry80
21:16 9 May '07  
Generalhi Pin
hebry
0:51 19 Mar '07  
QuestionFormatting text using MailFormat.Html Pin
booboo13alright
8:16 9 Nov '06  
Generalif there's a solution for CDO.message problem , reply me:) Pin
shery_bn
21:12 12 Sep '06  
QuestionPlease help me! Pin
tuonginfo
10:50 2 Sep '06  
Generali want Help... Pin
madhan_genn
1:58 29 Jul '06  
QuestionCDO.Message Pin
parigi_anantha
1:56 25 Jul '06  
GeneralNo errors while mailing but, no mails also Pin
ronnienk
22:06 20 May '06  
GeneralProblem using Proxy Server Pin
techtrix
3:59 18 May '06  
GeneralHELP! I can't receive the email if sent to Yahoo. Pin
ardiaz
18:05 9 Apr '06  
GeneralSMTP Server Authentication : How To? Pin
MehdiAnis
5:36 7 Mar '06  
GeneralRe: SMTP Server Authentication : How To? Pin
Santanu Biswas
7:11 1 Sep '07  
GeneralRe: SMTP Server Authentication : How To? Pin
Void I.T.O.
10:01 4 Dec '07  
GeneralRe: SMTP Server Authentication : How To? Pin
el3ashe2
22:25 17 Aug '09  
GeneralProblems with References Pin
kostasdiktia2
11:22 9 Feb '06  


Last Updated 3 Mar 2004 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009