Click here to Skip to main content
15,867,851 members
Articles / Web Development / ASP.NET
Article

Sending SMTP mail from ASP.NET and codebehind as C#

Rate me:
Please Sign up or sign in to vote.
4.45/5 (22 votes)
4 Apr 20061 min read 124.7K   470   45   16
Sends authenticated mail

Introduction

.NET namespace system.web.mail allows users to send smtp mails. SMTP server runs on port 25. Now a day's spam messages are coming a lot. So all leading smtp servers requires proper authentication to send mails. Some SMTP servers can send messages without authentication. But that messages will be delivered to bulk folder in recepients mail box.

System.Web.Mail

This namespace has a class called MailMessage. First we have to create an object for this mail message class.

MailMessage mail = new MailMessage()

Mailmessage has some properties to specify mail attributes. Users can specify From name, to name, subject, body and attachments etc...

mail.To = "<a href="mailto:friendname@websitename.com">friendname@websitename.com</a>";
mail.From = "<a href="mailto:yourname@websitename.com">yourname@websitename.com</a>";
mail.Subject = "Mail Subject";
mail.Body = "Mail Body";

Adding attachments

For adding attchments create an object for MailAttchment class.

MailAttachment att = new MailAttachment(filename). 

Then add this mail attachment object to MailMessage object. For that use mail.AddAttchment(att). Now the mail is ready to send. Before that we have to authenticate the SMTP server.

Authenticating SMTP server

All leading mail servers requires authentication to send mails successfully. For that the sender email address and password should be given to server. Server will validate the address and password. For this we use microsoft schemas. Use MailMessage class's fileds property to add schemas.

mail.Fields["<a href="http://schemas.microsoft.com/cdo/configuration/smtpserverport">http://schemas.microsoft.com/cdo/configuration/smtpserverport</a>"] = 25;
mail.Fields["<a href="http://schemas.microsoft.com/cdo/configuration/sendusing">http://schemas.microsoft.com/cdo/configuration/sendusing</a>"]  = 2;
mail.Fields["<a href="http://schemas.microsoft.com/cdo/configuration/smtpauthenticate">http://schemas.microsoft.com/cdo/configuration/smtpauthenticate</a>"] = 1;
mail.Fields["<a href="http://schemas.microsoft.com/cdo/configuration/sendusername">http://schemas.microsoft.com/cdo/configuration/sendusername</a>"] = "<a href="mailto:yourname@websitename.com">yourname@websitename.com</a>";
mail.Fields["<a href="http://schemas.microsoft.com/cdo/configuration/sendpassword">http://schemas.microsoft.com/cdo/configuration/sendpassword</a>"] = "yourpassword";
mail.Fields["<a href="http://schemas.microsoft.com/cdo/configuration/smtpusessl">http://schemas.microsoft.com/cdo/configuration/smtpusessl</a>"]= "true";
this will send user name and password to server and you will be logged on to the server.

Finishing Up !!!

Next step is specifying the SMTP server you are going to use and sending the mail using MailMessageObject.Send property.

SmtpMail.SmtpServer = "smtp.gmail.com";  
SmtpMail.Send( mail );

Speed of mail delivery depends on bandwidth,size of attached file and speed of smtp server. I tried this on Gmail and got very good performance.


Happy programming
Cheers

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer ThoughtWorks
India India
Call me Navaneeth Some years ago--never mind how long precisely, I was doing programs with C and C++. When .NET came, I started with C#,ASP.NET and SQL Server.

Comments and Discussions

 
GeneralI can't download the source code Pin
xiaoqiang.he30-May-15 0:40
xiaoqiang.he30-May-15 0:40 
GeneralMy vote of 4 Pin
Ho0otan27-Jun-13 21:31
Ho0otan27-Jun-13 21:31 
QuestionSend Email from asp.net using vb Pin
Rakesh Sinha18-Jun-13 3:05
Rakesh Sinha18-Jun-13 3:05 
Questionthanks for giving this Pin
tejag23-Feb-13 2:09
tejag23-Feb-13 2:09 
QuestionI cant download this code Pin
ateeb17-Jan-12 20:32
ateeb17-Jan-12 20:32 
AnswerRe: I cant download this code Pin
amir ahmari12-Mar-12 21:34
amir ahmari12-Mar-12 21:34 
GeneralSimple & Nice Pin
thatraja15-Jan-10 11:15
professionalthatraja15-Jan-10 11:15 
Generali have an error Pin
druli8-Jul-09 7:06
druli8-Jul-09 7:06 
GeneralThanks Pin
Kamran Hazari10-Apr-09 18:10
Kamran Hazari10-Apr-09 18:10 
GeneralRe: Thanks Pin
N a v a n e e t h15-Apr-09 17:41
N a v a n e e t h15-Apr-09 17:41 
GeneralThis code is excellent Pin
Habib Ahmed Bhutto21-Nov-08 10:24
Habib Ahmed Bhutto21-Nov-08 10:24 
GeneralRe: This code is excellent Pin
N a v a n e e t h21-Nov-08 16:50
N a v a n e e t h21-Nov-08 16:50 
GeneralThanks A Lot Pin
vitali thl25-Jul-07 15:28
vitali thl25-Jul-07 15:28 
GeneralAlready posted by another author... Pin
Mike Lang5-Apr-06 6:14
Mike Lang5-Apr-06 6:14 
GeneralRe: Already posted by another author... Pin
JccrRules10-Mar-07 9:32
JccrRules10-Mar-07 9:32 
GeneralRe: Already posted by another author... Pin
chris_ashford12-Jun-07 2:03
chris_ashford12-Jun-07 2:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.