Click here to Skip to main content
13,192,426 members (46,400 online)
Click here to Skip to main content
Add your own
alternative version


46 bookmarked
Posted 4 Apr 2006

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

, 4 Apr 2006
Rate this:
Please Sign up or sign in to vote.
Sends authenticated mail


.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.


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=""></a>";
mail.From = "<a href=""></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=""></a>"] = 25;
mail.Fields["<a href=""></a>"]  = 2;
mail.Fields["<a href=""></a>"] = 1;
mail.Fields["<a href=""></a>"] = "<a href=""></a>";
mail.Fields["<a href=""></a>"] = "yourpassword";
mail.Fields["<a href=""></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 = "";  
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


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


About the Author

N a v a n e e t h
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.

You may also be interested in...

Comments and Discussions

GeneralI can't download the source code Pin
xiaoqiang.he30-May-15 0:40
memberxiaoqiang.he30-May-15 0:40 
GeneralMy vote of 4 Pin
Ho0otan27-Jun-13 21:31
memberHo0otan27-Jun-13 21:31 
QuestionSend Email from using vb Pin
Rakesh Sinha18-Jun-13 3:05
memberRakesh Sinha18-Jun-13 3:05 
Questionthanks for giving this Pin
tejag23-Feb-13 2:09
membertejag23-Feb-13 2:09 
QuestionI cant download this code Pin
ateeb17-Jan-12 20:32
memberateeb17-Jan-12 20:32 
AnswerRe: I cant download this code Pin
amir ahmari12-Mar-12 21:34
memberamir ahmari12-Mar-12 21:34 
GeneralSimple & Nice Pin
thatraja15-Jan-10 11:15
memberthatraja15-Jan-10 11:15 
Generali have an error Pin
druli8-Jul-09 7:06
memberdruli8-Jul-09 7:06 
GeneralThanks Pin
KamiDalton10-Apr-09 18:10
memberKamiDalton10-Apr-09 18:10 
GeneralRe: Thanks Pin
N a v a n e e t h15-Apr-09 17:41
mvpN a v a n e e t h15-Apr-09 17:41 
GeneralThis code is excellent Pin
habib bhutto21-Nov-08 10:24
memberhabib bhutto21-Nov-08 10:24 
GeneralRe: This code is excellent Pin
N a v a n e e t h21-Nov-08 16:50
memberN a v a n e e t h21-Nov-08 16:50 
GeneralThanks A Lot Pin
vitali thl25-Jul-07 15:28
membervitali thl25-Jul-07 15:28 
GeneralAlready posted by another author... Pin
versat14745-Apr-06 6:14
memberversat14745-Apr-06 6:14 
GeneralRe: Already posted by another author... Pin
JccrRules10-Mar-07 9:32
memberJccrRules10-Mar-07 9:32 
GeneralRe: Already posted by another author... Pin
chris_other200412-Jun-07 2:03
memberchris_other200412-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171017.2 | Last Updated 5 Apr 2006
Article Copyright 2006 by N a v a n e e t h
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid