2

Could anyone tell me why the following code is sending out emails in raw Html? As in, the email looks like when you view a page source.

I have cut down the code so as not to include attachments and from addresses.

If I disable the line with the alternate view the email renders correctly but I also want to send out a plain text version.

using (SmtpClient client = GetSmtpClient(settings)) {
    using (MailMessage message = new MailMessage()) {
        message.IsBodyHtml = true;
        message.BodyEncoding = System.Text.Encoding.GetEncoding("iso-8859-1");
        message.To.Add(toList);
        message.Subject = subject;
        message.Body = htmlTemplate;
        message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(textTemplate, new ContentType("text/plain")));
        client.Send(message);
     }
}

Edit: The message was originally sending text as the main body and html as the alternative view but I have run into a problem with accented and foreign characters as described here and wanted to set IsBodyHtml to true, which forces me to set html to the main view.

Community
  • 1
  • 1
DevDave
  • 6,700
  • 12
  • 65
  • 99

2 Answers2

2

I had problems with this also but here's a very much cutdown version of code that worked for me...

   private MailMessage CreateEmailMessage(string emailAddress) {

        MailMessage msg = new MailMessage();

        msg.From = new MailAddress(FromEmailAddress, FromName);
        msg.To.Add(new MailAddress(emailAddress));
        msg.Subject = "Msg Subject here";

        string textBody = File.ReadAllText(TextTemplateFile);


        string htmlBody = "";
        if (EmailFormat == "html") {
            htmlBody = File.ReadAllText(HtmlTemplateFile);

            foreach (Attachment inline in InlineAttachments) {
                inline.ContentDisposition.Inline = true;
                msg.Attachments.Add(inline);
            }

            AlternateView alternateHtml = AlternateView.CreateAlternateViewFromString(htmlBody,
                                                                                      new ContentType("text/html"));
            msg.AlternateViews.Add(alternateHtml);

            AlternateView alternateText = AlternateView.CreateAlternateViewFromString(textBody,
                                                                                      new ContentType("text/plain"));
            msg.AlternateViews.Add(alternateText);

        }
        else {
            msg.Body = textBody;
        }

        return msg;
    }
Vicki
  • 668
  • 7
  • 21
  • Assuming you only want the `EmailFormat=="html"` option, might this also resolve your problem with accented characters? – Vicki Nov 08 '12 at 11:42
1

In the end I realised that the 'htmlTemplate' string being passed into the method was defining charset=ISO-8859-1 in the head of the email and therefore overriding any changes I was making in the code.

I changed the charset to UTF-8, and restored my code to this:

using (SmtpClient client = GetSmtpClient(settings)) {
  using (MailMessage message = new MailMessage()) {
    message.To.Add(toList);
    message.Subject = subject;
    message.Body = textTemplate;          
    message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(htmlTemplate, new ContentType("text/html")));
    client.Send(message);
  }
}

and can now send both text and html templates as well as cover the accented characters problem.

DevDave
  • 6,700
  • 12
  • 65
  • 99