DevDave
DevDave

Reputation: 6908

SmtpClient sending raw Html

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.

Upvotes: 2

Views: 5072

Answers (2)

DevDave
DevDave

Reputation: 6908

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.

Upvotes: 1

Rollins999
Rollins999

Reputation: 688

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

Upvotes: 2

Related Questions