Click here to Skip to main content
Click here to Skip to main content
Go to top

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

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

Share

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.

Comments and Discussions

 
Generali have an error Pinmemberdruli8-Jul-09 7:06 

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

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

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