Guest User

Untitled

a guest
Sep 3rd, 2018
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. using System.Net;
  2. using System.Net.Mail;
  3.  
  4. namespace Client.Project.Messaging
  5. {
  6. /// <summary>
  7. /// Provides a message object that sends the email through gmail.
  8. /// MailgunMessage is inherited from <c>System.Web.Mail.MailMessage</c>, so all the mail message features are available.
  9. /// </summary>
  10. public class MailgunMessage : MailMessage
  11. {
  12. #region Fields
  13.  
  14. private const string _smtpHost = "smtp.mailgun.org";
  15. private const string _smtpPass = "PUT_PASSWORD_HERE";
  16. private const int _smtpPort = 587;
  17. private const string _smtpUser = "postmaster@SUBDOMAIN.mailgun.org";
  18. private readonly MailAddress _mailAddress = new MailAddress("FROM ADDRESS", "FROM NAME");
  19. private readonly SmtpClient _smtpClient = new SmtpClient();
  20.  
  21. #endregion
  22.  
  23. #region .ctor
  24.  
  25. public MailgunMessage(string toEmail, string subject, string body, bool isBodyHtml) : this()
  26. {
  27. To.Add(toEmail);
  28. From = _mailAddress;
  29. Subject = subject;
  30. Body = body;
  31. IsBodyHtml = isBodyHtml;
  32. }
  33.  
  34. private MailgunMessage()
  35. {
  36. _smtpClient.Host = _smtpHost;
  37. _smtpClient.Port = _smtpPort;
  38. _smtpClient.EnableSsl = true;
  39. _smtpClient.UseDefaultCredentials = false;
  40. var cred = new NetworkCredential(_smtpUser, _smtpPass);
  41. _smtpClient.Credentials = cred;
  42. }
  43.  
  44. #endregion
  45.  
  46. #region Methods
  47.  
  48. public void Send()
  49. {
  50. try
  51. {
  52. _smtpClient.Send(this);
  53. }
  54. catch (SmtpException ex)
  55. {
  56. Log<MailgunMessage>.Logger.Fatal("Email sending faliure - SMTP", ex);
  57. }
  58. catch (WebException ex)
  59. {
  60. Log<MailgunMessage>.Logger.Fatal("Email sending failure - WebException", ex);
  61. }
  62. }
  63.  
  64. #endregion
  65. }
  66. }
Add Comment
Please, Sign In to add comment