broadband
broadband

Reputation: 3498

Send html email message and plainText as alternative

    using (MailMessage message = new MailMessage()) // using System.Net.Mail;
    {
      string mailFrom = "[email protected]";
      string smtpServer = "smtp.server.net";

      message.From = new MailAddress(mailFrom);
      message.To.Add("[email protected]");
      message.Subject = "subject";
      message.SubjectEncoding = Encoding.UTF8;

      message.IsBodyHtml = true;
      message.Body = "<h1>VODA</h1>";
      message.BodyEncoding = Encoding.UTF8;

      AlternateView plainView = AlternateView.CreateAlternateViewFromString("test content", Encoding.UTF8, "text/plain");
      message.AlternateViews.Add(plainView);

      SmtpClient smtpClient = new SmtpClient(smtpServer);

      smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
      smtpClient.UseDefaultCredentials = false;
      smtpClient.Credentials = new System.Net.NetworkCredential("user", "pass");
      smtpClient.EnableSsl = true;
      smtpClient.Port = 587;
      smtpClient.Send(message);
    }

When looking what was received at gmail I see next:

Subject: subject
Content-Type: multipart/alternative; boundary=--boundary_0_989afdbb-5fe4-4155-ba59-3d5ffdbb909e
Message-Id: <[email protected]>

----boundary_0_989afdbb-5fe4-4155-ba59-3d5ffdbb909e
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: base64

PGgxPlZPREE8L2gxPg==
----boundary_0_989afdbb-5fe4-4155-ba59-3d5ffdbb909e
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: base64

dGVzdCBjb250ZW50
----boundary_0_989afdbb-5fe4-4155-ba59-3d5ffdbb909e--

Why System.Net.Mail.MailMessage doesn't set text/html contect type?

Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: base64

PGgxPlZPREE8L2gxPg==

Is there any additional MailMessage property I missed?

Upvotes: 2

Views: 3900

Answers (2)

tpeczek
tpeczek

Reputation: 24125

When you are using AlternateViews the Body is expected to be text/plain and the AlternateViews to deliver different content types. From the documentation (https://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.alternateviews.aspx):

To add an alternate view to a MailMessage object, create an Attachment for the view, and then add it to the collection returned by AlternateViews. Use the Body property to specify the text version and use the AlternateViews collection to specify views with other MIME types.

Upvotes: 6

broadband
broadband

Reputation: 3498

Answer is written in this post: GMail displays plain text email instead HTML

Try switching the order of the parts of the message, putting the HTML part after the plain-text part. It might work :).

NOTE: I cannot remember now where I read this (or if I for sure even did), but the reason switching might help is because I think the preferred part of the message may be the last part.

Update: I found a place where it says that parts in a multipart MIME message should be in order of increasing preference -- here, in section 7.2.3, starting with the third to last paragraph.

After adding text/html after text/plain gmail shows html content. Microsoft Exchange although does show html version of the message and it doesn't mind the order in which version are added.

AlternateView plainView = AlternateView.CreateAlternateViewFromString("test content", Encoding.UTF8, "text/plain");
message.AlternateViews.Add(plainView);

AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlContent, Encoding.UTF8, "text/html");
message.AlternateViews.Add(htmlView);

Upvotes: 0

Related Questions