Sending Mail Using Dot net
The 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
MessagePriority 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 Name: 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 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 addattachment 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 Removeattachment 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", "msreerajan@yahoo.co.uk") '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 msreerajan@yahoo.co.uk
|
| Author: Tim Boalch 15 Jun 2004 | Member Level: Bronze Points : 0 |
Good code example!
It's a shame reading E-Mail (eg. by IMAP) can't be done as simply, although I suppose it would be a security nightmare.
Still, I think that using this method to send an E-Mail via SMTP is great, and removes the need for hefty applications such as Outlook \ Outlook Express \ AOL, especially when you want to send something quickly.
It also removes the need for you to supply your own E-Mail address in the 'From' field, which can be useful in some situations!
Overall a good choice of example, especially considering the System.Web namespace is not, by default, a reference for Windows Forms and could therefore be overlooked.
|
| Author: Mahmoud Sami 30 Oct 2004 | Member Level: Bronze Points : 0 |
al salamo alekom .. first thanx for this code .. it helps me too much .. but i had i problem .. after i implemented the code and i run the program .. theres an error message results saying .. " Could not access 'CDO Message' Object "
|
| Author: Mahmoud Sami 30 Oct 2004 | Member Level: Bronze Points : 0 |
if u have any idea how to fix it .. or what is the reason for it .. please email me at modern_night@hotmail.com or just reply me here ..
|
| Author: Tim Boalch 30 Oct 2004 | Member Level: Bronze Points : 0 |
What Windows version are you using? You have to be running Windows 2000 or XP - (but probably not XP Home Edition??)
The only other thing I can think of is that you haven't typed some of the code correctly, or that a FIREWALL or ANTI-VIRUS \ ANTI-SPAM program may be stopping you from sending E-Mail in that way.
|
| Author: ManojRajan 01 Nov 2004 | Member Level: Gold Points : 0 |
The System.Web.mail expects a relay SMTP server which will be sending your mails. If you are in a network with an SMTP server, Provide the server address OR else if you are working in a Standalone system, you may need SMTP service enabled in IIS. After enabling the SMTP service use 127.0.0.1 for the SMTP server address. Also make sure that Anonymus access is possible with the SMTP or else provide the user name and password ( This is possible only in dot net version 1.1 framework) Hope this will solve your problem. Let me know if your are still in trouble. ManojRajan
|
| Author: Mahmoud Sami 02 Nov 2004 | Member Level: Bronze Points : 0 |
im installing SMTP server with IIS on my pc .. i dont know but i typed the ip address 127.0.0.1 in the place of SMTP address .. if that what you mean .. but i still have the same problem ..i dont know what is the problem .. should i pass this IP address as an IP address Variable or as astring .. i dont know .. i just typed the address in the place of the smtp server but the error appears again ..
i can send you the source code .. if you want to ..
thanx alot for your care man
|
| Author: Adelio STevanato 22 Dec 2004 | Member Level: Bronze Points : 0 |
I am using VB dot net and I want to be able (via a Windows or Web service) is send e-mails (including possibly attatchments)
This might be called from a users PC programatically or via a batch process that reads configuration data and e-mail reports to specfied e-mail addresses.
All the examples I have seen require the use of a path for attachments, but as the calls are from other machines, and I really do not want to have to "send" the document to the server, Save it to disk and then pass the path of the file to send the attachments.
Is there anyway I can just pass a text stream to the mailattachment object instead.
Will I have to use some other method.
|