.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Code Snippets >> Email >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

How to send mail using C#.net

Posted By:Maverick       Posted Date: July 29, 2011    Points: 40    Category: Email    URL: http://www.dotnetspark.com  

How to send mail using C#.net using MailMessage class in .NET Framework

Hello All,

The article describes how can we send an email using C#.net

Consider the following function

private void SendMail()
 MailMessage message = new MailMessage();
  message.Subject = Subject; // Specify subject , here it is variable
  message.From = new MailAddress("test@test.com");
  message.Body = HttpUtility.HtmlDecode(strMsg); // strMsg is a variable that has message in html format
  message.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
  System.Net.NetworkCredential AuthInfo = new System.Net.NetworkCredential("test@test.com", "secret");
  smtp.Host = strSMTPServer; // smtp.mydomain.com
  smtp.UseDefaultCredentials = false;
  smtp.Credentials = AuthInfo;
  smtp.Timeout = 20;
  smtp.Port = 25;
  smtp.EnableSsl = false;
  smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

The above function has all the things by which mail can be send.

User needs to specify following things.

1) Email Id of the recipient
2) Subject
3) Email of From address
4) The message body
5) SMTP details.

Hope it helps.



Further Readings:

Author: pervej         Company URL: http://www.dotnetspark.com
Posted Date: October 03, 2011

Below url provide you a code to send mail


Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend