Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Net;
- using System.Net.Mail;
- namespace Client.Project.Messaging
- {
- /// <summary>
- /// Provides a message object that sends the email through gmail.
- /// MailgunMessage is inherited from <c>System.Web.Mail.MailMessage</c>, so all the mail message features are available.
- /// </summary>
- public class MailgunMessage : MailMessage
- {
- #region Fields
- private const string _smtpHost = "smtp.mailgun.org";
- private const string _smtpPass = "PUT_PASSWORD_HERE";
- private const int _smtpPort = 587;
- private const string _smtpUser = "postmaster@SUBDOMAIN.mailgun.org";
- private readonly MailAddress _mailAddress = new MailAddress("FROM ADDRESS", "FROM NAME");
- private readonly SmtpClient _smtpClient = new SmtpClient();
- #endregion
- #region .ctor
- public MailgunMessage(string toEmail, string subject, string body, bool isBodyHtml) : this()
- {
- To.Add(toEmail);
- From = _mailAddress;
- Subject = subject;
- Body = body;
- IsBodyHtml = isBodyHtml;
- }
- private MailgunMessage()
- {
- _smtpClient.Host = _smtpHost;
- _smtpClient.Port = _smtpPort;
- _smtpClient.EnableSsl = true;
- _smtpClient.UseDefaultCredentials = false;
- var cred = new NetworkCredential(_smtpUser, _smtpPass);
- _smtpClient.Credentials = cred;
- }
- #endregion
- #region Methods
- public void Send()
- {
- try
- {
- _smtpClient.Send(this);
- }
- catch (SmtpException ex)
- {
- Log<MailgunMessage>.Logger.Fatal("Email sending faliure - SMTP", ex);
- }
- catch (WebException ex)
- {
- Log<MailgunMessage>.Logger.Fatal("Email sending failure - WebException", ex);
- }
- }
- #endregion
- }
- }
Add Comment
Please, Sign In to add comment