1
Vote

Embedded MessageParts

description

I send a simple email-message using two views (text and html) and three text-file attachments. I guess the sender wraps this in two parts each containing more embedded parts, schematically something like this:
[1: [text] [html] ] [2: [att1] [att2] [att3] ]

This email with embedded boundaries will cause netDumbster to tell me I only have two MessageParts, both having very little BodyData.

Actually, I am really only interested in the number of attachments, but I can make do with the number of parts in total. But this present count and content of two does not help me in any way.
(I can imagine it would be hard to count every part when people attach emails, or are those just Mime'd? But still, I'd rather have too many parts with a plausible explanation for users, than just these 2, regardless of the number of attachments.)

Simplified my send-code looks like this:
using System.Net.Mail;
using System.Net.Mime;
// ...
var lEmail = new MailMessage(lFrom, lTo);
lEmail.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(lMailPlainText, new ContentType("text/plain"));
lEmail.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(lMailHtmlText, new ContentType("text/html"));
lEmail.Attachments.Add(new Attachment(lFileName1));
lEmail.Attachments.Add(new Attachment(lFileName2));
The resulting email looks like this:
Content-Type: multipart/mixed;
 boundary=--boundary_1_18a616dd-eeed-48df-9b9b-c844d1822211


----boundary_1_18a616dd-eeed-48df-9b9b-c844d1822211
Content-Type: multipart/alternative;
 boundary=--boundary_0_f827b934-3b81-4527-9e57-7a08419b9287


----boundary_0_f827b934-3b81-4527-9e57-7a08419b9287
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable

Dit is een test-email
----boundary_0_f827b934-3b81-4527-9e57-7a08419b9287
Content-Type: text/html
Content-Transfer-Encoding: quoted-printable

<html><body>Dit is een <b>test</b>-email</body></html>
----boundary_0_f827b934-3b81-4527-9e57-7a08419b9287--

----boundary_1_18a616dd-eeed-48df-9b9b-c844d1822211
Content-Type: multipart/mixed;
 boundary=--boundary_2_f2424112-d034-43cb-9e70-fbb4390f67ae


----boundary_2_f2424112-d034-43cb-9e70-fbb4390f67ae
Content-Type: application/octet-stream; name=AssemblyInfo.cs
Content-Transfer-Encoding: base64
Content-Disposition: attachment

77u/dXNpbmcgU3lzdGVtLlJlZmxlY3Rpb247DQp1c2luZyBTeXN0ZW0uUnVudGltZS5D
...
----boundary_2_f2424112-d034-43cb-9e70-fbb4390f67ae
Content-Type: application/octet-stream; name=App.config
Content-Transfer-Encoding: base64
Content-Disposition: attachment

77u/PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiID8+DQo8Y29uZmln
...
----boundary_2_f2424112-d034-43cb-9e70-fbb4390f67ae--
Content-Type: application/octet-stream; name=App.config
Content-Transfer-Encoding: base64
Content-Disposition: attachment

77u/PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiID8+DQo8Y29uZmln
...
----boundary_2_f2424112-d034-43cb-9e70-fbb4390f67ae--

----boundary_1_18a616dd-eeed-48df-9b9b-c844d1822211--
And the resulting BodyData (sorry, different test-run, thus different test-email, but you get the picture) one:
----boundary_13_9adc51b1-a431-4c0b-a42c-e13e02419aff
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable
and two:
----boundary_15_c911cede-4a1c-4e3c-8385-19de61080029
Content-Type: application/octet-stream; name=AssemblyInfo.cs
Content-Transfer-Encoding: base64
Content-Disposition: attachment

comments

cmendible wrote Jun 20, 2013 at 11:29 AM

Hi,

what version of netDumbster are you using? A month a go we added support for AlternateViews: https://github.com/cmendible/netDumbster/commit/0832d0c4bb45ef8e012091ce6368641648eab9c5 try version 1.3.0.6 from https://nuget.org/packages/netDumbster and let me know.

Hope it helps...

wrote Jun 20, 2013 at 11:32 AM

YahooSerious wrote Jun 20, 2013 at 1:57 PM

I tried the new version, but MessageParts.Length returns 2, and the next test (MessageParts[0].HeaderData.Contains("text/plain"))) also fails :
    [Test]
    public void Send_Email_With_AlternateViews_And_Attachments()
    {
        using (var client = new SmtpClient("localhost", this.server.Port)) {
            var mailMessage = new MailMessage("carlos@mendible.com", "karina@mendible.com", "test", "this is the body");
            mailMessage.AlternateViews.Add(AlternateView.CreateAlternateViewFromString("FooBar", new ContentType("text/html")));
            mailMessage.Attachments.Add(Attachment.CreateAttachmentFromString("Attachment1", new ContentType("application/octet-stream")));
            mailMessage.Attachments.Add(Attachment.CreateAttachmentFromString("Attachment2", new ContentType("application/octet-stream")));
            mailMessage.Attachments.Add(Attachment.CreateAttachmentFromString("Attachment3", new ContentType("application/octet-stream")));
            client.Send(mailMessage);
        }
        Assert.AreEqual(1, server.ReceivedEmailCount);
        var smtpMessage = server.ReceivedEmail[0];

        Assert.AreEqual(5, smtpMessage.MessageParts.Length);
        Assert.IsTrue(smtpMessage.MessageParts[0].HeaderData.Contains("text/plain"));
        Assert.AreEqual("this is the body\r\n", smtpMessage.MessageParts[0].BodyData);
        Assert.IsTrue(smtpMessage.MessageParts[1].HeaderData.Contains("text/html"));
        Assert.AreEqual("FooBar\r\n", smtpMessage.MessageParts[1].BodyData);
    }

cmendible wrote Jun 24, 2013 at 7:34 PM

Hi,

I've just released netDumbster v 1.4.0.0-alpha http://nuget.org/packages/netDumbster/ with anew email parser.

The API is not changed but the results are getting closer to what you need.

Some changes that can break your current tests:
1.- priority is now returned as string an not an int
2.- no \r\n is added a the end of the lines while parsing

Some changes you are interested in:
1.- attachments and alternateviews are returned, complete.

Check it out and let me know.

Hope it helps...

YahooSerious wrote Jun 25, 2013 at 12:51 PM

Hmm, actually I did some adaptations to SmtpContext a.o. to add some extra transport-properties to SmtpMessage for my own needs. (The properties being RemoteIpAddress, RemoteDomain, LocalIpAddress, LocalPort, ReceivedDate.) So this is going to require some non-trivial merging on my side, which will probably have to wait for a while.

But, one thing I noticed already, is that you do not store the recipientAddresses anymore, but use the mailMessage.To-list. This list does not contain contain CC and BCC, and it originates from parsing the headers. Even if you add the CC and BCC, they are still parsed from the header, and a good email-header should not contain the bcc-list (what would be the point of bcc if it is not completely hidden!). So I think netDumbster still needs to keep track of those recipient-addresses in its own code, because I definitely do need to test/check those bcc-addressees!

I hope to check the attachment-code, but it may be a while before I have time to manually fix my merge.
(But maybe, if you recover the recipientAddresses, you might add my transport-properties? :-))

cmendible wrote Jun 25, 2013 at 2:20 PM

Ok I just managed to make recipientAddresses to work as expected (See this commit: http://goo.gl/Usvbv).

Do you have the code for the extra properties you are using? If you send it to me I'll be glad to add it!

Thanks in advance,

YahooSerious wrote Jun 27, 2013 at 11:37 AM

I sent it to your gmail-account (as seen in Git).

cmendible wrote Jul 16, 2013 at 12:58 PM

Sorry for the delay I just released version 1.4.0.0-beta to the nuget gallery. This version has the extra properties and fixes the issue with the cc and bcc fields

https://nuget.org/packages/netDumbster

Hope you can test it.