October 2017 : Online course in ASP.NET MVC / Core. Conducted by Bipin Joshi. Read more...
Registration for October 2017 batch of ASP.NET MVC / Core online course has already started. Conducted by Bipin Joshi. Book your seat today ! Click here for more details.

<%@ Page %>

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!

Bipin Joshi is a software consultant, an author and a yoga mentor having 22+ years of experience in software development. He also conducts online courses in ASP.NET MVC / Core and Design Patterns. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Meditation and Mindfulness to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 07 May 2002


Tags : ASP.NET .NET Framework VB.NET C#