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.
Attachmentspecifies the list of the attachments that are transmitted with the message.
Bccis a list of semicolon delimited email addresses that receive a Blind Carbon Copy of the message.
Bodycontains the message text that has to be sent.
BodyEncodingis the encoding type of the email message.
BodyFormatdefines the content type of the body of the message.
Ccis a list of semicolon delimited email addresses that receive a Carbon Copy of the message.
Fromis the email address of the sender.
Headerspecifies the custom headers which are transmitted with the
Priorityis the priority of the email message.
Subjectis the subject line of the email message.
Tois the email address of the recipient.
Class:
MailAttachments manages the mail attachment.
Class:
SmtpMail sends email to the mail server.
Let us see it step by step:
Create a Visual Basic .NET application and drop the following controls and set the properties accordingly.
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." '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.
General
News
Question
Answer
Joke
Rant
Admin