Monday, February 13, 2012

Using JangoSMTP in ASP.Net

Microsoft provides classes for sending SMTP emails using the .Net Framework. This example uses System.Net.Mail to assemble then send an email using JangoSMTP.

Download the source code for this example, insert your own JangoSMTP username/password/etc and start sending tracked transactional emails in your application.

MailMessage mailMsg = new MailMessage();

// set the from address
// this must be stored in your from addresses
mailMsg.From = new MailAddress(
    "", "Your Name");

// recipient
mailMsg.To.Add(new MailAddress(
    "", "Recipient Name"));

// set the subject and message portions of the email
mailMsg.Subject = "Test Email Subject";
string text = 
    "This plain text email was sent from an ASP.Net script.";
string html = 
    "<p>This html email was sent from an ASP.Net script.</p>";
        text, null, MediaTypeNames.Text.Plain));
        html, null, MediaTypeNames.Text.Html));

// set up the SMTP client with
SmtpClient smtpClient 
    = new SmtpClient("", 25);
System.Net.NetworkCredential credentials = 
    new System.Net.NetworkCredential(
         "Your JangoSMTP Username", "Your JangoSMTP Password");
smtpClient.Credentials = credentials;