SmtpMail.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.IO;
  11. using System.Reflection;
  12. namespace System.Web.Mail
  13. {
  14. /// <remarks>
  15. /// </remarks>
  16. public class SmtpMail
  17. {
  18. private static string smtpServer = "localhost";
  19. // Constructor
  20. private SmtpMail ()
  21. {
  22. /* empty */
  23. }
  24. // Properties
  25. public static string SmtpServer {
  26. get { return smtpServer; }
  27. set { smtpServer = value; }
  28. }
  29. public static void Send (MailMessage message)
  30. {
  31. SmtpMessage msg = new SmtpMessage ();
  32. try {
  33. if( message.From != null ) msg.From = MailAddress.Parse (message.From);
  34. if( message.To != null ) msg.To = MailAddressCollection.Parse (message.To);
  35. if( message.Cc != null ) msg.Cc = MailAddressCollection.Parse (message.Cc);
  36. if( message.Bcc != null ) msg.Bcc = MailAddressCollection.Parse (message.Bcc);
  37. msg.Headers = message.Headers;
  38. msg.UrlContentBase = message.UrlContentBase;
  39. msg.UrlContentLocation = message.UrlContentLocation;
  40. msg.Priority = message.Priority;
  41. msg.Subject = message.Subject;
  42. msg.Body = message.Body;
  43. msg.BodyEncoding = message.BodyEncoding;
  44. msg.BodyFormat = message.BodyFormat;
  45. msg.Attachments = message.Attachments;
  46. } catch (FormatException ex) {
  47. throw new HttpException (ex.Message);
  48. }
  49. try {
  50. SmtpClient smtp = new SmtpClient (smtpServer);
  51. smtp.Send (msg);
  52. smtp.Close ();
  53. } catch (SmtpException ex) {
  54. // LAMESPEC:
  55. // .NET sdk throws HttpException
  56. // for some reason so to be compatible
  57. // we have to do it to :(
  58. throw new HttpException (ex.Message);
  59. } catch (IOException ex) {
  60. throw new HttpException (ex.Message);
  61. }
  62. }
  63. public static void Send (string from, string to, string subject, string messageText)
  64. {
  65. MailMessage message = new MailMessage ();
  66. message.From = from;
  67. message.To = to;
  68. message.Subject = subject;
  69. message.Body = messageText;
  70. Send (message);
  71. }
  72. }
  73. } //namespace System.Web.Mail