SmtpMail.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // System.Web.Mail.SmtpMail.cs
  3. //
  4. // Author:
  5. // Lawrence Pit ([email protected])
  6. // Per Arneng ([email protected]) (SmtpMail.Send)
  7. //
  8. using System;
  9. using System.Net;
  10. using System.Net.Sockets;
  11. using System.Text;
  12. using System.IO;
  13. using System.Reflection;
  14. namespace System.Web.Mail
  15. {
  16. /// <remarks>
  17. /// </remarks>
  18. public class SmtpMail
  19. {
  20. private static string smtpServer = "localhost";
  21. // Constructor
  22. private SmtpMail ()
  23. {
  24. /* empty */
  25. }
  26. // Properties
  27. public static string SmtpServer {
  28. get { return smtpServer; }
  29. set { smtpServer = value; }
  30. }
  31. public static void Send (MailMessage message)
  32. {
  33. try {
  34. // wrap the MailMessage in a MailMessage wrapper for easier
  35. // access to properties and to add some functionality
  36. MailMessageWrapper messageWrapper = new MailMessageWrapper( message );
  37. SmtpClient smtp = new SmtpClient (smtpServer);
  38. smtp.Send (messageWrapper);
  39. smtp.Close ();
  40. } catch (SmtpException ex) {
  41. // LAMESPEC:
  42. // .NET sdk throws HttpException
  43. // for some reason so to be compatible
  44. // we have to do it to :(
  45. throw new HttpException (ex.Message, ex);
  46. } catch (IOException ex) {
  47. throw new HttpException (ex.Message, ex);
  48. } catch (FormatException ex) {
  49. throw new HttpException (ex.Message, ex);
  50. } catch (SocketException ex) {
  51. throw new HttpException (ex.Message, ex);
  52. }
  53. }
  54. public static void Send (string from, string to, string subject, string messageText)
  55. {
  56. MailMessage message = new MailMessage ();
  57. message.From = from;
  58. message.To = to;
  59. message.Subject = subject;
  60. message.Body = messageText;
  61. Send (message);
  62. }
  63. }
  64. } //namespace System.Web.Mail