1

Summary: essentially I ask how to do this in .NET Core 3.1. I have this hierarchy:

multipart/related
   ├── multipart/alternative
   │   ├── text/plain
   │   └── text/html
   └── image/png

Full version: I build an email:

using (var mailMessage = new MailMessage(new MailAddress("from@from.com"), new MailAddress("to@to.com")))
{
    mailMessage.Subject = mailContent.Subject;
    var htmlView = AlternateView.CreateAlternateViewFromString(mailContent.Html, Encoding.UTF8, "text/html");

    htmlView.TransferEncoding = System.Net.Mime.TransferEncoding.SevenBit;

    htmlView.LinkedResources.Add(new LinkedResource("image.png")
    {
        ContentId = "imagecid",
    });

    var plainView = AlternateView.CreateAlternateViewFromString(mailContent.Plain, Encoding.UTF8, "text/plain");
    plainView.TransferEncoding = System.Net.Mime.TransferEncoding.SevenBit;
    mailMessage.AlternateViews.Add(plainView);
    mailMessage.AlternateViews.Add(htmlView);

    using (var smtpServiceClient = ...))
    smtpServiceClient.SendMessage(mailMessage);
}

The structure I get is:

Content-Type: multipart/alternative; boundary=--boundary_0_fe9da24c-3c5d-468a-bffb-388a6002a995
Date: 1 Dec 2021 21:51:56 +0100
From: "xxx" <xxx.com>
MIME-Version: 1.0
Message-ID: MQaSQxMTAs-VLocCm64ESW0i6DCc73eLuc9GUY9Fpbs=@mailhog.example
Received: from xxxx by mailhog.example (MailHog)
          id MQaSQxMTAs-VLocCm64ESW0i6DCc73eLuc9GUY9Fpbs=@mailhog.example; Wed, 01 Dec 2021 20:51:59 +0000
Return-Path: <xxx.com>
Subject: xxx
To: test1@test12.com


----boundary_0_fe9da24c-3c5d-468a-bffb-388a6002a995
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit

PLAIN_TEXT_REPRESENTATION_HERE
----boundary_0_fe9da24c-3c5d-468a-bffb-388a6002a995
Content-Type: multipart/related; type="text/html";
 boundary=--boundary_1_4199b4eb-25b8-4c45-b11e-15b97d6ca815


----boundary_1_4199b4eb-25b8-4c45-b11e-15b97d6ca815
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 7bit

HTML_REPRESENATION_HERE
----boundary_1_4199b4eb-25b8-4c45-b11e-15b97d6ca815
Content-Type: image/png
Content-Transfer-Encoding: base64
Content-ID: <imagecid>

base64_REPRESENTATION_HERE
----boundary_1_4199b4eb-25b8-4c45-b11e-15b97d6ca815--

----boundary_0_fe9da24c-3c5d-468a-bffb-388a6002a995--

which causes some clients fail to render embedded images.

What I should have is:

multipart/related
   ├── multipart/alternative
   │   ├── text/plain
   │   └── text/html
   └── image/png

but I have totally no control over it using MailMessage class.

How to fix this? Here there is someone who has exactly the same structure: https://github.com/mailhog/MailHog/issues/206#issuecomment-596502425

halfer
  • 19,824
  • 17
  • 99
  • 186
Yoda
  • 17,363
  • 67
  • 204
  • 344
  • 1
    According to [the documentation](https://learn.microsoft.com/en-gb/dotnet/api/system.net.mail.smtpclient?view=net-6.0#remarks), `System.Net.Mail` is no longer recommended. Microsoft recommend that you use [MailKit](https://github.com/jstedfast/MailKit) instead. – Richard Deeming Dec 02 '21 at 15:30
  • 1
    @RichardDeeming And this is what I did 1.5 hour ago :). – Yoda Dec 02 '21 at 16:33

0 Answers0