title: Email via .NET MailMessage drops PDF attachment in Windows 10 Mail client
body: <p>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.</p>
  <p>The email consists of:</p>
  <ol>
  <li>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).</li>
  <li>Plain text version.</li>
  <li>PDF file attachment.</li>
  </ol>
  <p>We've determined that the issue does not occur if the email account being viewed in the Windows Mail application is a:</p>
  <ul>
  <li>Outlook/Hotmail account</li>
  <li>Gmail account</li>
  </ul>
  <p>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:</p>
  <pre><code>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, &quot;Test Sender&quot;);
      email.Subject = &quot;Test&quot;;
      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(
          &quot;&lt;img src=\&quot;cid:logoImage\&quot; /&gt;&lt;p&gt;HTML VERSION&lt;/p&gt;&quot;, null,
          System.Net.Mime.MediaTypeNames.Text.Html);
  
      // Create the plain text version of the e-mail.
      var plainView = AlternateView.CreateAlternateViewFromString(&quot;PLAIN TEXT VERSION&quot;, 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, &quot;image/png&quot;);
      logoImage.ContentId = &quot;logoImage&quot;;
      htmlView.LinkedResources.Add(logoImage);
  
      // Create the PDF file attachment.
      var attachment = new Attachment(pdfAttachmentFilePath, 
          System.Net.Mime.MediaTypeNames.Application.Pdf);
      attachment.Name = &quot;test.pdf&quot;;
  
      // 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();
  }
  </code></pre>
  <p>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:</p>
  <pre><code>htmlView.LinkedResources.Add(logoImage);
  </code></pre>
  <p>Looking into the source of the generated emails, the non-working example has the following MIME boundaries:</p>
  <pre><code>multipart/mixed
      multipart/alternative
          text/plain
          multipart/related
              text/html
              image/png
      multipart/mixed
          application/pdf
  </code></pre>
  <p>The working example has the following MIME boundaries:</p>
  <pre><code>multipart/mixed
      multipart/alternative
          text/plain
          text/html
      multipart/mixed
          application/pdf
  </code></pre>
  <p>What I'd appreciate is if the community could help me answer:</p>
  <ol>
  <li>Can you see anything wrong with my code that would trigger this behavior?</li>
  <li>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).</li>
  </ol>