Email via .NET MailMessage drops PDF attachment in Windows 10 Mail client

Viewed 134

We've received reports of customers getting our emails, but with the attached PDF file missing. It seems this only happens with the Windows 10 Mail application. When viewed in the email provider's web interface, or another client like Thunderbird, the PDF file can be found. We use C#, .NET 4.7.2 and the MailMessage class to setup our emails.

The email consists of:

  1. HTML version with short text and PNG logo. The logo is embedded and referenced using an IMG tag and the image's content ID (CID).
  2. Plain text version.
  3. PDF file attachment.

We've determined that the issue does not occur if the email account being viewed in the Windows Mail application is a:

  • Outlook/Hotmail account
  • Gmail account

The problem does occur if you manually add an ISP or hosting provider email account using Windows Mail's advanced settings to manually input your account details, IMAP/SMTP server and port settings, etc. I was able to produce this minimal function for sending an email which will result in a missing attachment when opened in Windows Mail:

using System.IO;
using System.Net.Mail;

public void SendEmail(SmtpClient smtpClient, string toEmailAddress, string fromEmailAddress, 
    string pngLogoImageFilePath, string pdfAttachmentFilePath)
{
    var email = new MailMessage();
    email.To.Add(new MailAddress(toEmailAddress));
    email.From = new MailAddress(fromEmailAddress, "Test Sender");
    email.Subject = "Test";
    email.IsBodyHtml = true;
    email.BodyEncoding = Encoding.UTF8;

    // Create the HTML version of the e-mail, including the IMG tag for showing the
    // embedded image.
    var htmlView = AlternateView.CreateAlternateViewFromString(
        "<img src=\"cid:logoImage\" /><p>HTML VERSION</p>", null,
        System.Net.Mime.MediaTypeNames.Text.Html);

    // Create the plain text version of the e-mail.
    var plainView = AlternateView.CreateAlternateViewFromString("PLAIN TEXT VERSION", null, 
        System.Net.Mime.MediaTypeNames.Text.Plain);

    // Add the HTML and plain text versions as alternate views.
    email.AlternateViews.Add(plainView);
    email.AlternateViews.Add(htmlView);

    // Create a linked resource for the logo image to be shown in the HTML version.
    var logoImage = new LinkedResource(pngLogoImageFilePath, "image/png");
    logoImage.ContentId = "logoImage";
    htmlView.LinkedResources.Add(logoImage);

    // Create the PDF file attachment.
    var attachment = new Attachment(pdfAttachmentFilePath, 
        System.Net.Mime.MediaTypeNames.Application.Pdf);
    attachment.Name = "test.pdf";

    // Add attachment file information.
    System.Net.Mime.ContentDisposition disposition = attachment.ContentDisposition;
    disposition.FileName = attachment.Name;
    disposition.Size = new FileInfo(pdfAttachmentFilePath).Length;
    disposition.CreationDate = File.GetCreationTime(pdfAttachmentFilePath);
    disposition.ModificationDate = File.GetLastWriteTime(pdfAttachmentFilePath);

    email.Attachments.Add(attachment);
    
    smtpClient.Send(email);
    
    email.Dispose();
}

Surprisingly, simply removing the LinkedResource for the PNG image makes Windows Mail show the PDF attachment. Comment the following line in the code and retry:

htmlView.LinkedResources.Add(logoImage);

Looking into the source of the generated emails, the non-working example has the following MIME boundaries:

multipart/mixed
    multipart/alternative
        text/plain
        multipart/related
            text/html
            image/png
    multipart/mixed
        application/pdf

The working example has the following MIME boundaries:

multipart/mixed
    multipart/alternative
        text/plain
        text/html
    multipart/mixed
        application/pdf

What I'd appreciate is if the community could help me answer:

  1. Can you see anything wrong with my code that would trigger this behavior?
  2. Can you find any way of getting this example to work as expected with Windows Mail, without breaking other clients? For example, I was able to make Windows Mail show both the PNG and PDF, but that involved making the PNG an inline attachment which broke Thunderbird (logo not shown in HTML, but included as a regular attachment).
0 Answers
Related