views:

437

answers:

3

I'm testing sending out some emails via C#, but I can't tell what effect setting IsBodyHtml to true has. Regardless of the value, whatever I send in my Body shows up with a content type of "text/plain", and my HTML shows up tags and all in my email client (gmail). What is that flag actually supposed to do?

NOTE: I can send an HTML email just fine by creating an AlternateView with a content type of "text/html", I just want to understand how setting the body is supposed to work.

A: 

Have you tried using a different client to see if the problem persists? Setting this to true should cause it to show up as HTML in the client, and it does in mine, BUT each client is different.

On top of that, some users specify their email to show everything as plain text regardless of how the email is sent.

Compounding the matter, some clients will display HTML only from known sources, but will show plain text only for an email from an unknown sender/domain, etc.

You're best off always crafting your email as a Multi-Part mime message anyway, just to ensure you're providing a readable email for everyone. (And it sounds like you're already doing that to get it to work properly.)

David Stratton
+1  A: 

Here is an excerpt for my SMTP helper I use everyday....

public bool SendMail(string strTo, string strFrom, string strCc, string strBcc, string strBody, string strSubject)
{

    bool isComplete = true;

    SmtpClient smtpClient = new SmtpClient();
    MailMessage message = new MailMessage();

    try
    {
        //Default port will be 25
        smtpClient.Port = 25;

        message.From = new MailAddress(smtpEmailSource);
        message.To.Add(strTo);
        message.Subject = strSubject;

        if (strCc != "") { message.Bcc.Add(new MailAddress(strCc)); }
        if (strBcc != "") { message.Bcc.Add(new MailAddress(strBcc)); }

        message.IsBodyHtml = true;

        string html = strBody;  //I usually use .HTML files with tags (e.g. {firstName}) I replace with content.  This allows me to edit the emails in VS by opening a .HTML file and it's easy to do string replacements.

        AlternateView htmlView = AlternateView.CreateAlternateViewFromString(html, new ContentType("text/html"));

        message.AlternateViews.Add(htmlView);


        // Send SMTP mail
        smtpClient.Send(message);
    }
    catch
    {
        isComplete = false;
    }

    return isComplete;
}

[UPDATE]

The key points as I originally left off...

  1. IsBodyHtml states that your message is HTML formatted. If you were only sending a single view of HTML, this is all you need.

  2. AlternateView is used to store my HTML, this is not required for sending a HTML message but it's required if you want to send a message that includes HTML and Plain Text, in case the receiver is unable to render the HTML.

I took out my plainView above so this isn't obvious, sorry...

The key here is that if you want to send a HTML formatted message you need to use IsBodyHtml = true (default is false) to have your content rendered as HTML.

Zachary
That's actually what I do as well. I'm just curious what should be happening if I assign HTML to the body with IsBodyHtml set to true rather than using an AlternateView.
Eddie Deyo
A: 

I just wrestled with this same problem. My best solution was to avoid setting the Body property of the MailMessage object at all. Instead just add two AlternateViews, first a plain text then an HTML. Make sure to add the plain text version first because the MIME standard says that:

The formats are ordered by how faithful they are to the original, with the least faithful first and the most faithful last.

That means, that you put the plain text version first, so the clients should use the HTML version if possible.

JerSchneid