Created
January 11, 2012 13:39
-
-
Save kylewest/1594684 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "[email protected]"; | |
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 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void SendEmail(string toEmail, string subject, string body, bool isBodyHtml) | |
{ | |
var message = new MailgunMessage(toEmail, subject, body, isBodyHtml); | |
message.Send(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment