+1 vote

I’m again having an issue with a legacy application.

Creating an E-Mail using this code:

    var b = new MailBuilder {Subject = "Hallo"};
    b.From.Add(new MailBox("dagobert.duck@entenhausen.at"));
    b.To.Add(new MailBox("donald.duck@entenhausen.at"));

    b.AddAttachment(Encoding.UTF8.GetBytes("hello world"));

    var mail = b.Create();

results in the following EML:

Content-Type: multipart/mixed;
boundary="----=_NextPart_46379612.848537642257"
MIME-Version: 1.0
Subject: Hallo
From: <dagobert.duck@entenhausen.at>
To: <donald.duck@entenhausen.at>
Date: Tue, 18 Nov 2014 10:38:35 +0100
Message-ID: <2f796a56-32a7-4f41-b1f1-5bc881842479@mail.dll>

------=_NextPart_46379612.848537642257
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: 7bit


------=_NextPart_46379612.848537642257
Content-Type: application/octet-stream
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment

hello world
------=_NextPart_46379612.848537642257--

The problem is the empty Mime Part that was created for the empty body. Is there a way to create that mail without the empty mime part, so that the resulting mail looks like this:

Content-Type: multipart/mixed;
boundary="----=_NextPart_46379612.848537642257"
MIME-Version: 1.0
Subject: Hallo
From: <dagobert.duck@entenhausen.at>
To: <donald.duck@entenhausen.at>
Date: Tue, 18 Nov 2014 10:38:35 +0100
Message-ID: <2f796a56-32a7-4f41-b1f1-5bc881842479@mail.dll>

------=_NextPart_46379612.848537642257
Content-Type: application/octet-stream
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment

hello world
------=_NextPart_46379612.848537642257--

Thanks and best regards

by

1 Answer

0 votes
 
Best answer

1)

You can simply remove this part from newly created email:

IMail mail = builder.Create();
((MimeMixed)mail.Document.Root).Parts.RemoveAt(0);

2)

You can also use MailBuilder.AddAlternative method:

MailBuilder builder = new MailBuilder { Subject = "Hallo" };
builder.From.Add(new MailBox("dagobert.duck@entenhausen.at"));
builder.To.Add(new MailBox("donald.duck@entenhausen.at"));
builder.AddAlternative(Encoding.UTF8.GetBytes("hello world"));
IMail mail = builder.Create();

This result in attachment as root of the email's MIME structure:

Content-Type: application/octet-stream
Content-Transfer-Encoding: 7bit
MIME-Version: 1.0
Subject: Hallo
From: <dagobert.duck@entenhausen.at>
To: <donald.duck@entenhausen.at>
Date: Tue, 18 Nov 2014 11:20:27 +0100
Message-ID: <97033398-5e81-490e-be31-70205a19a85f@mail.dll>

hello world
by (301k points)
...