We Recommend

Accelerated C# 2008 Accelerated C# 2008
This book is both a rapid tutorial and a permanent reference. You’ll quickly master C# syntax while learning how the CLR simplifies many programming tasks. You’ll also learn best practices that ensure your code will be efficient, reusable, and robust. Why spend months or years discovering the best ways to design and code C# when this book will show you how to do things the right way, right from the start?


Posted By

ecavazos on 01/18/08


Tagged

mail Net


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

ejosullivan


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("someone@tobcc.com");
  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.