Friday, April 24, 2009

Sending Web Email in ASP.NET C#




Please visit my new Web Site WWW.Codedisplay.com



E-mail is one of the most common popular methods of communication for both personal and business purposes. It also plays an important role in each and every Web site specially for registration page or sales order page of an e-commerce site & also for online invoicing. The .NET framework makes the task of sending email from a Web page unbelievably easy and I'll show you how to do it in a matter of minutes.

Here i am using gmail smtp to send email using asp.net cause i think most of the reader has a gmail id so that he can just copy the below method & can send email. Just need to change the from & to email address. Better first try with gmail cause its easy to test. Then go for your own mail server.

Send an email using Visual Studio 2005:
At first import the namespace: using System.Net.Mail;
Now copy the below code under Send button:
C#:

MailMessage mailMsg = new MailMessage();
// if you want to send HTML BODY then true for plain text body then false
mailMsg.IsBodyHtml = true;
mailMsg.Subject =
"Asp.net articles";
mailMsg.Body =
"<a href='http://shawpnendu.blogspot.com'>GO TO SHAWPNENDU'S BLOG</a>";
mailMsg.From =
new MailAddress(shawpnendu@gmail.com);
mailMsg.To.Add(
mxxion@yahoo.com);
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
smtp.Credentials =
new System.Net.NetworkCredential("shawpnendu@gmail.com", "password here");
// If you use your smtp server then try first with blocking the below line.
smtp.EnableSsl = true;
smtp.Send(mailMsg);




VB.NET:
At first import the name space System.Net.Mail

Dim mailMsg As New MailMessage
mailMsg.IsBodyHtml =
True
mailMsg.Subject = "Asp.net articles"
mailMsg.Body = "<a href='http://shawpnendu.blogspot.com'>GO TO SHAWPNENDU'S BLOG</a>"
mailMsg.From = New MailAddress(shawpnendu@gmail.com)
mailMsg.To.Add(
mxxion@yahoo.com)
Dim smtp As New SmtpClient("smtp.gmail.com")
smtp.Credentials =
New System.Net.NetworkCredential("shawpnendu@gmail.com", "password here")
smtp.EnableSsl =
True
smtp.Send(mailMsg)



Send email with Attachment:
Just copy the below line before smtp.Send(mailMsg).
You can also add more than one attachment at a time.
C#:

mailMsg.Attachments.Add(new Attachment("c:\\abc.txt"));
//mailMsg.Attachments.Add(new Attachment("Add another file here"));



VB.NET:
Just copy the below line before smtp.Send(mailMsg).
You can also add more than one attachment at a time.


mailMsg.Attachments.Add(New Attachment("c:\\abc.txt"))
'mailMsg.Attachments.Add(New Attachment("second file path"))



NOTES:
If you are going to host your web site then ask administrator the below questions:
1. Does mail server configured? If yes then collect smtp server name & replace with smtp.gmail.com
2. Does the mail server require authentication before sending an email? If yes then modify credentials else remove credential line from above code segment.
3. Does the mail server relay is on? If no then make it yes.

4. Does the mail server require SSL validation. If no then remove EnableSSL line.

Output Like:


If mail has an attacment:


Common error message:
Mailbox unavailable. The server response was: 5.7.1 Unable to relay for..............
Need to configure the SMTP server to allow relaying for either your IP, "FROM" address, or credentials. Notify hosting server administrator to allow the relay aganist your from email address. If you are using localhost then right click on mycomputer then manage then services & applications then IIS then right click on default smtp virtual server then properties then click on access tab then click on relay button then select only the list below then click on ADD & set the IP 127.0.0.1


To read how to validate entered email address using javascript click on the below link:
http://shawpnendu.blogspot.com/2009/04/cross-browser-javascript-aspnetc-email.html

6 comments:

Anonymous said...

dude ..
you are great .. i was stuck for the past two days .. your tutorial really helped me understand and successfully send a mail .. thnx a ton ..

also while sending a mail using gmail i had to also use the code
"smtp.Port = 587;"

thnx again ..

Rodolpho said...

Do you have the same code for those sending messages using Windows 2003 SMTP virtual server in the same machine where IIS is running?

unlike the genleman above, I'm still stuck in my problem.

thanks!

Saion Roy said...

Hi rodolpho, sorry i didn't get you. Could you please post the error message? Also you can read common error section to configure smtp server localy.

ds r4 said...

Do you know any way for connecting my application with Hotmail server with SSL. I want to aquire ticket for its CMS for addressbook.

Saion Roy said...

HI ds r4,
Yes there is a open source project in sourceforge named OpenContacts.NET can resolve your problem. For more you can read the below link:

gnillydev blogspot 2007/12 opencontacts-10-release-with post.

Anonymous said...

I come from sending mail through PHP which is pretty simple but you have to setup your server to work with it. This was even easier because you don't have to chnage server setting to make it work.

What a great post!

Want to say something?
I WOULD BE DELIGHTED TO HEAR FROM YOU

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
     
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash