Sending Email From .NET Applications

    
 
Home  About  Search  Contact  Link Exchange  Guestbook  Tell Your Friends 
 
 
Site Search
Search DotNetBips:

 

Advanced Search 
Content 
.NET Framework 
ASP.NET General 
Web Forms 
Web Controls 
Custom Controls 
Web Services 
ASP.NET Config 
ASP.NET Security 
Windows Forms 
ADO.NET 
Remoting 
Components 
VB.NET 
C# 
XML 
COM/DCOM

 
Links
Web Sites

 
Hosted By

 
 
Sending Email From .NET Applications
Introduction
Sending emails is one of the most common requirements of any web application. In traditional VB and ASP applications CDONT serves this requirement. In .NET the System.Web.Mail namespaces provides classes that form a wrapper over CDONT and can be use to quickly send emails. Even though it is found in System.Web namespace it can be used in windows forms also. This article will tell you how to perform many common email tasks with this namespace.
Sending email - Quick and Easy
This example shows the most basic way to send an email:
SmtpMail.SmtpServer="myserver.com"
SmtpMail.Send("me@mydomain.com",
"you@yourdomain.com",
"This is subject",
"This is message")

As you can see the SmtpMail class has a static method Send that takes four arguments - sender's email address, receiver's email address, subject of the email and message body. The SmtpServer property specifies the SMTP server to be used for sending email messages. If you do not provide this value the default local server will be used. This is the most obvious usage for simple confirmation kind of messages.
Sending CC, BCC and setting priority
Many times we also need to send CC and BCC to others. In such situation the MailMessage class can be effectively used as shown below:
Dim mm As New MailMessage()
mm.From = "me@mydomain.com"
mm.To = "you@yourdomain.com"
mm.Cc = "yourfriend@somedomain.com"
mm.Bcc = "youranotherfriend@somedomain.com"
mm.Priority = MailPriority.High
mm.Subject = "This is subject"
mm.Body = "This is message"
SmtpMail.Send(mm)

Here we have created an instance of MailMessage and set individual properties like From, To, CC and BCC. You can also set mail priority using Priority property and MailPriority enum. Then you can use another overloaded form of Send method that takes this instance of MailMessage.
Sending HTML messages with images and links
Now a days HTML messages are also very common. By default the message format is plain text. Following example shows how to send HTML messages:
Dim mm As New MailMessage()
mm.From = "me@mydomain.com"
mm.To = "you@yourdomain.com"
mm.BodyFormat = MailFormat.Html
mm.UrlContentBase = "http://www.yourdomain.com"
mm.UrlContentLocation = "images/"
mm.Subject = "This is subject"
mm.Body = "This is message"
SmtpMail.Send(mm)

Here we have set the BodyFormat property to HTML. The UrlContentBase and UrlContentLocation properties are typically used together. The former tells the base address for the referenced images and links where as the later tells the relative location further to the base location.
Sending Attachments
Our final example shows how to send attachments with the email.
Dim mm As New MailMessage()
mm.From = "me@mydomain.com"
mm.To = "you@yourdomain.com"
mm.Subject = "This is subject"
mm.Body = "This is message"
Dim ma as New MailAttachment("c:\attatchments\somefile.zip")
mm.Attachments.Add(ma)
SmtpMail.Send(mm)

Here, we have created an instance of MailAttachment class and passed the path of some disk file to its constructor. We then add it to the Attachments collection of the MailMessage class. Finally the Send method is called as usual. I hope this covers almost all the commonly used email tasks!
About the author
Name :
 Bipin Joshi
 
Email :
 webmaster@dotnetbips.com
 
Profile :
 Bipin Joshi - the creator and owner of DotNetBips - is a Microsoft MVP, Software Developer and Author. He has written dozens of articles for DotNetBips and other web sites. Know more about him and DotNetBips here.
 

Rate this article
(1 being poor and 5 being excellent)
1 2 3 4 5


 

 
Copyright ? 2000-2002 DotNetBips.com. All rights reserved.
webmaster@dotnetbips.com
Read Terms Of Use 


注意事項:


   MSDN Home >  MSDN Library >  .NET Development >  Visual Basic .NET >  Code Samples >  .NET Framework Code Samples 
Rate this page:  4 users      
  3.3 out of 5
 
 
Visual Basic .NET Code Sample: Send Email
Download the SendMail.exe sample.

This sample shows how to send email over SMTP using classes in the System.Web.Mail namespace.

Featured Highlights
This application has a single Windows Form with controls similar to the Microsoft Outlook email client.
When the Form loads it checks to make sure the user has the SMTP Service installed on their machine and that it is running.
Simple validation is performed on the "To" and "From" email address fields, with the errors displayed using an ErrorProvider control.
Requirements
Microsoft Visual Studio.NET Professional or greater.

Windows 2000 or Windows XP.

Notes

SMTP Service must be installed and running. SMTP is an option installed with IIS.
To send mail the SmtpServer property must be set to the IP address or server name where the SMTP Server is running. In most cases this is the "localhost" machine. As such, the SmtpServer property value can either be the friendly name, "localhost", or "127.0.0.1". The latter is the default for this application. If you have any trouble sending mail, check to make sure the SmtpServer property is properly set in the code.
To find the SMTP server name, do the following:

Open the IIS snap-in (Start | Control Panel | Administrative Tools | Internet Information Services). (Note: On Win2K this is the Internet Services Manager)
Expand the Default SMTP Virtual Server node (the exact name of this node may vary by operating system).
Click Domains. In the right pane you will see a list of Domain Names. You may set the SmtpServer property to one of these names or their corresponding IP addresses.
The Default SMTP Virtual Server must also be running. Realize that it is possible for the SMTP Service to be running, but the Default SMTP Server in IIS to be stopped. There is a difference between the service itself and the actual server run by the service. It is akin to having IIS running with a particular Web or FTP site stopped. IIS, the service, is still running. But the particular Web or FTP site is not.
If your Default SMTP Virtual Server is not running, a red X icon will be visible over its node in the IIS snap-in. To start the Server, right-click its node and select Start.

Finally, the Relay Restrictions must be properly set. To do this:
In IIS, right-click Default SMTP Virtual Server and select Properties.
In the Properties dialog click the Access tab.
Click Relay.
In the Relay Restrictions dialog, check "All except the list below". The bottom checkbox can be left checked or unchecked.
Click OK twice to effect the change and clear the properties dialogs.
Running the Sample
There are no special instructions for running the sample other than those listed in the Requirements section.

See also
System.Web.Mail Namespace
 
 Contact Us   |  E-Mail this Page   |  MSDN Flash Newsletter 
 ? 2002 Microsoft Corporation. All rights reserved.   Terms of Use  Privacy Statement   Accessibility 

posted @ 2005-02-03 01:58  Benny Ng  阅读(137)  评论(0)    收藏  举报