it:ad:mandrill:howto:send_email_using_.net_mailmessage

IT:AD:Mandrill:HowTo:Send Email using .NET MailMessage

The recipe is:

Config File

Setup your IIS application's config file:

  <system.net>
    <mailSettings>
      <smtp from="skysigal@xact-solutions.com">
        <!-- use  25/587, or 465 or SSL -->
        <network host="smtp.mandrillapp.com" 
                 port="587" 
                 userName="foo" 
                 password="bar"  />
      </smtp>
    </mailSettings>
  </system.net>

Nuget the C# Client

Install-Package Mandrill

MailMessage

Create a .NET MailMessage object, and provision it with your message as you normally would.

Send it off

THere are two ways of sending it off. SMTP or HTML.

SendGrid recommends that you stick with the SMTP one unless your ISP is limiting your outgoing port 25 and other SMTP ports – in which case its better to use the HTTP ports which they won't be restricting.

Sending using SMTP

Sending using SMTP is more or less the same as it has always been, except that you give the information you would find on your SendGrid account details (https://sendgrid.com/user/account)

string userName = ConfigurationManager.AppSettings["SendGridUserId"].ToString();
string password = ConfigurationManager.AppSettings["SendGridPassword"].ToString();

MailMessage mailMsg = new MailMessage();

mailMsg.To.Add(new MailAddress("skysigal@xact-solutions.com", "Sky Sigal"));
mailMsg.From = new MailAddress("skysigal@xact-solutions.com", "Test");
mailMsg.Subject = "subject";
mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString("text body", null, MediaTypeNames.Text.Plain));
mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(@"<p>html body</p>", null, MediaTypeNames.Text.Html));
// Init SmtpClient and send
SmtpClient smtpClient = new SmtpClient("smtp.mandrill.com", 25);
smtpClient.Credentials = new System.Net.NetworkCredential(userName,password);
smtpClient.Send(mailMsg);

  • /home/skysigal/public_html/data/pages/it/ad/mandrill/howto/send_email_using_.net_mailmessage.txt
  • Last modified: 2023/11/04 01:48
  • by 127.0.0.1