Posted By

ecavazos on 01/18/08


Tagged

mail Net


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

ejosullivan
umang_nine
uday1302
rodrigojuarez
mheinrich
Beppoi


SendMail


 / Published in: C#
 

A simple function to send an email in .NET. You can add this to a utility class and use as needed. A more complicated example can embed HTML templates for various communications.

  1. // Make mail easy to send.
  2. public static string SendMail(string from, string to, string subject, string body, string smtp)
  3. {
  4. // Create new MailMessage Object
  5. MailMessage message = new MailMessage(from, to, subject, body);
  6.  
  7. //message.Bcc.Add("[email protected]");
  8.  
  9. message.IsBodyHtml = true;
  10.  
  11. // Create new SmtpClient Object
  12. SmtpClient mailClient = new SmtpClient(smtp, 25);
  13.  
  14. try
  15. {
  16. // Send eMail and return 1 if successful
  17. // otherwise return error message
  18. mailClient.Send(message);
  19. return "1";
  20. }
  21. catch (System.Net.Mail.SmtpException e)
  22. {
  23. // create or append errors to a log file
  24. CreateLogFiles.CreateLogFiles Err = new CreateLogFiles.CreateLogFiles();
  25. Err.ErrorLog(System.Web.HttpContext.Current.Server.MapPath("/logs/ErrorLog.log"), e.InnerException.Message);
  26. return e.InnerException.Message;
  27. }
  28. }

Report this snippet  

You need to login to post a comment.