Click here to Skip to main content
13,091,554 members (65,616 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 have an error Pin
druli8-Jul-09 7:06
memberdruli8-Jul-09 7:06 

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
Web02 | 2.8.170813.1 | 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