When an attachment is added to an email: multipart message parsing is not working


In the following sample:
     SmtpClient client = new SmtpClient("localhost", _Server.Port);
        var mailMessage = new MailMessage("cfm@mendible.com", "kbm@mendible.com", "test", "this is the body");
        mailMessage.IsBodyHtml = isBodyHtml;
        if (smtpAuth)
            NetworkCredential credentials = new NetworkCredential("user", "pwd");
            client.Credentials = credentials;
            client.EnableSsl = false;
        if (attachment != null)
            mailMessage.Attachments.Add(new Attachment(new MemoryStream(attachment), "image/jpeg"));
_Server.ReceivedEmail[0].MessageParts.Length should be greater than cero.
Closed Sep 8, 2011 at 8:55 AM by cmendible
Fix and latest source code can be found here: http://goo.gl/nRUZA


steviehailey wrote Sep 6, 2011 at 3:10 PM

The problem looks like line 142 of SmtpMessage.cs
Match match1 = Regex.Match(input2, "boundary=\"(?<boundry>\S+)\"", RegexOptions.IgnoreCase);

It expects the boundary value to be in quotes, which doesn't appear to be the case for me. Removing the both quotes (i.e. \") from the regex seems to fix it e.g. update line to read
Match match1 = Regex.Match(input2, "boundary=(?<boundry>\S+)", RegexOptions.IgnoreCase);

cmendible wrote Sep 8, 2011 at 8:53 AM

Hi! Thanks for the fix!

I recently moved the code to github, so you can find the fix here: http://goo.gl/nRUZA

wrote Sep 8, 2011 at 8:53 AM

wrote Sep 8, 2011 at 8:55 AM

wrote Feb 13, 2013 at 10:46 PM

wrote May 16, 2013 at 3:52 AM