Dave Mateer Dave Mateer - 1 year ago 198
ASP.NET (C#) Question

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

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


// 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 =
plainTextBody, null, MediaTypeNames.Text.Plain);


// 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 =
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",
sampleImage.ContentId = "SampleImage";



// 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")

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

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);

Answer Source

I would say that, in today's world, the "best-practice" approach would be to ensure that you send your message as both plain text and HTML (if you really want to send HTML email messages).

Oh, and make sure you do actually send the content in the plain text view, rather than a single sentence saying "You must use an email client that supports HTML messages". Google Mail takes this approach, and it seems to work perfectly, allowing "rich" views on full-fledged PC clients, whilst also allowing "minimal" views on more restricted devices (i.e. Mobile/Cell phones).

If you want to take a purist's view, you wouldn't be sending HTML emails at all, nor would you ever "attach" a binary file to an email. Both corruptions of the original email standard, which was only ever originally intended for plain text. (See some people's opinions of this here and here)

However, in the pragmatic modern-day real world, HTML email is very real, and very acceptable. The primary downside to sending HTML email is whether the recipient will see the email in the way that you intended them to see it. This is much the same problem that web designers have battled with for years; getting their websites to look "just right" in all possible browsers (although it's significantly easier today than it was many years ago).

Similar to ensuring that a website functions correctly without requiring Javascript, by sending your emails as both HTML and Plain Text, you'll ensure that your emails degrade gracefully so that people reading their emails on (for example) small mobile devices (something that's becoming more and more prevalent these days - and which may or may not be capable of rendering a complete HTML email) can still read your email content without issue.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download