C# Send both HTML and Text email - most elegant?

Viewed 21439

Is is best practice to send both HTML and Text email?

If I only send HTML what are the dangers?

I'm thinking something like this below from

http://johnnycoder.com/blog/2009/04/15/net-mailmessage-linkedresources-alternateviews-and-exceptions/

try
{
    // Assign a sender, recipient and subject to new mail message
    MailAddress sender =
        new MailAddress("sender@johnnycoder.com", "Sender");

    MailAddress recipient =
        new MailAddress("recipient@johnnycoder.com", "Recipient");

    MailMessage m = new MailMessage(sender, recipient);
    m.Subject = "Test Message";

    // Define the plain text alternate view and add to message
    string plainTextBody =
        "You must use an email client that supports HTML messages";

    AlternateView plainTextView =
        AlternateView.CreateAlternateViewFromString(
            plainTextBody, null, MediaTypeNames.Text.Plain);

    m.AlternateViews.Add(plainTextView);

    // Define the html alternate view with embedded image and
    // add to message. To reference images attached as linked
    // resources from your HTML message body, use "cid:contentID"
    // in the <img> tag...
    string htmlBody =
        "<html><body><h1>Picture</h1><br>" +
        "<img src=\"cid:SampleImage\"></body></html>";

    AlternateView htmlView =
        AlternateView.CreateAlternateViewFromString(
            htmlBody, null, MediaTypeNames.Text.Html);

    // ...and then define the actual LinkedResource matching the
    // ContentID property as found in the image tag. In this case,
    // the HTML message includes the tag
    // <img src=\"cid:SampleImage\"> and the following
    // LinkedResource.ContentId is set to "SampleImage"
    LinkedResource sampleImage =
        new LinkedResource("sample.jpg",
            MediaTypeNames.Image.Jpeg);
    sampleImage.ContentId = "SampleImage";

    htmlView.LinkedResources.Add(sampleImage);

    m.AlternateViews.Add(htmlView);

    // Finally, configure smtp or alternatively use the
    // system.net mailSettings
    SmtpClient smtp = new SmtpClient
          {
              Host = "smtp.example.com",
              UseDefaultCredentials = false,
              Credentials =
                  new NetworkCredential("username", "password")
          };

    //<system.net>
    //    <mailSettings>
    //        <smtp deliveryMethod="Network">
    //            <network host="smtp.example.com"
    //              port="25" defaultCredentials="true"/>
    //        </smtp>
    //    </mailSettings>
    //</system.net>

    smtp.Send(m);
}
catch (ArgumentException)
{
    throw new
        ArgumentException("Undefined sender and/or recipient.");
}
catch (FormatException)
{
    throw new
        FormatException("Invalid sender and/or recipient.");
}
catch (InvalidOperationException)
{
    throw new
        InvalidOperationException("Undefined SMTP server.");
}
catch (SmtpFailedRecipientException)
{
    throw new SmtpFailedRecipientException(
        "The mail server says that there is no mailbox for recipient");
}
catch (SmtpException ex)
{
    // Invalid hostnames result in a WebException InnerException that
    // provides a more descriptive error, so get the base exception
    Exception inner = ex.GetBaseException();
    throw new SmtpException("Could not send message: " + inner.Message);
}
6 Answers

Sharing my experience with sending both HTML and text in one email: I have created an email message that has 2 views: text and html using C# AlternateView classes.

What did I get?

On Mac, tested on High Sierra: Apple Mail app was showing the Html. If the order of messages is reversed: Html - text then Apple Mail will show the text view. The conclusion: Apple Mail is using the last view as default.

In Windows, Outlook 2010: Microsoft Outlook by default is using the Html view. The order of views in the email doesn't matter: html,text; text,html;

If for some reason you selected a setting to show incoming messages as a text then the Html version of your email will be converted into the text by Outlook. Even so you send the text version of your email (which might be slightly different from the HTML version and was formatted to look pretty) it won't be used.

So you don't need to send the text version of your email if you know that your clients use Outlook and Html version is selected as default.

Mozilla Thunderbird respects your settings and shows the correct Html or text version of your email. It works correctly on Mac and in Windows

Hope it helps

Several email clients will use the last AlternateView that was added to the AlternateViews.

So if you prefer to have your mail displayed as HTML, be sure to add that last.

I have notice this for IOS mail and OSX mail, while Android Email seems to prefer HTML if it is available. I am not sure for which versions this holds, and the behaviour is often user-configurable, but in my experience these were the defaults.

Related