Sending Mail Using Dot net
System.Web.Mail
---------------
namespace provides the classes for sending Email in Dot net.
MailMessage
-----------
Manage the mail message contents.
Properties
==========
Attachment
-----------
Specifies the list of the attachments that are transmitted with the
message
Bcc
-----------
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
-------------
The encoding type of the email message.
BodyFormat
-----------
Defines the content type of the body of the message
Cc
-----------
A list of semicolon delimited email addresses that receive a Carbon Copy
of the message
From
-----------
The email address of the sender.
Header
-----------
Specifies the custom headers which are transmitted with the
Message
Priority
-----------
The priority of the email message
Subject
-----------
Subject Line of the email message.
To
-----------
email address of the recipient.
MailAttachments
----------------
Manage the mail attachment.
SmtpMail
-----------
Send email to the mail server.
Let us see it step by step
============================
Create a Visual basic application
And drop 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 widow and type the following statement above the Class
declaration
Imports System.Web.Mail '''-->Code
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 '''-->Code
'Variable to store the attachments
Dim Attachment As System.Web.Mail.MailAttachment '''-->Code
'Variable to create the message to send
Dim Mailmsg As New System.Web.Mail.MailMessage() '''-->Code
'Double click on the addattachment button to add the code
'Type the following lines
'Show open dialogue box to select the files to attach
Dim Counter As Integer '''-->Code
OFD.CheckFileExists = True '''-->Code
OFD.Title = "Select file(s) to attach" '''-->Code
OFD.ShowDialog() '''-->Code
For Counter = 0 To UBound(OFD.FileNames) '''-->Code
lstAttachment.Items.Add(OFD.FileNames(Counter)) '''-->Code
Next '''-->Code
'Double Click on the Removeattachment button
'Type the following lines
'Remove the attachments
If lstAttachment.SelectedIndex > -1 Then '''-->Code
lstAttachment.Items.RemoveAt(lstAttachment.Selecte dIndex) '''-->Code
End If '''-->Code
'Double Click on the Send button
'Type the following lines
Dim Counter As Integer '''-->Code
'Validate the data
If txtSMTPServer.Text = "" Then '''-->Code
MsgBox("Enter the SMTP server info ...!!!", '''-->Code
MsgBoxStyle.Information, "Send Email") '''-->Code
Exit Sub '''-->Code
End If '''-->Code
If txtFrom.Text = "" Then '''-->Code
MsgBox("Enter the From email address ...!!!", '''-->Code
MsgBoxStyle.Information, "Send Email") '''-->Code
Exit Sub '''-->Code
End If '''-->Code
If txtTo.Text = "" Then '''-->Code
MsgBox("Enter the Recipient email address ...!!!", '''-->Code
MsgBoxStyle.Information, "Send Email") '''-->Code
Exit Sub '''-->Code
End If '''-->Code
If txtSubject.Text = "" Then '''-->Code
MsgBox("Enter the Email subject ...!!!", '''-->Code
MsgBoxStyle.Information, "Send Email") '''-->Code
Exit Sub '''-->Code
End If '''-->Code
'Set the properties
‘Assign the SMTP server
obj.SmtpServer = txtSMTPServer.Text '''-->Code
'Multiple recepients can be specified using ; as the delimeter
‘Address of the recipient
Mailmsg.To = txtTo.Text '''-->Code
‘Your From Address
‘You can also use a custom header Reply-To for a different replyto
'address
Mailmsg.From = "\" & txtFromDisplayName.Text & "\ <" & txtFrom.Text &
">" '''-->Code
'Specify the body format
If chkFormat.Checked = True Then '''-->Code
Mailmsg.BodyFormat = MailFormat.Html 'Send the mail in HTML Format
Else '''-->Code
Mailmsg.BodyFormat = MailFormat.Text '''-->Code
End If '''-->Code
'If you want you can add a reply to header
'Mailmsg.Headers.Add("Reply-To", "Ma***@geinetech.net")
'custom headersare added like this
'Mailmsg.Headers.Add("Manoj", "TestHeader")
‘Mail Subject
Mailmsg.Subject = txtSubject.Text '''-->Code
‘Attach the files one by one
For Counter = 0 To lstAttachment.Items.Count - 1 '''-->Code
Attachment = New MailAttachment(lstAttachment.Items(Counter))
‘Add it to the mail message
Mailmsg.Attachments.Add(Attachment) '''-->Code
Next '''-->Code
‘Mail Body
Mailmsg.Body = txtMessage.Text '''-->Code
‘Call the send method to send the mail
obj.Send(Mailmsg) '''-->Code
************************************************** *********************
Send SMTP mail using VB.NET
By Chris Dufour
http://www.codeproject.com/vb/net/epsendmail.asp
Bye
Venkat_KL
For Anything and Everything, Please Let Me Know
--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com