سؤال

I have an application that occasionally sends out an e-mail with three attachments.

One attachment is the file initially sent to my mailbox by a user, so I'm replying with the same file they sent me. I do this by MemoryStream, and it works just fine.

The other two files are a .PDF and an .XLSX file.

    var excelTemplatePath = GetFilePathToDefaultExcelFile();

    if (excelTemplatePath != null)
        mailMessage.Attachments.Add(new System.Net.Mail.Attachment(excelTemplatePath));

    var pdfPath = GetFilePathToPDFInstructions();

    if (pdfPath != null)
        mailMessage.Attachments.Add(new System.Net.Mail.Attachment(pdfPath));

Here's the weird part. They attach just fine when I debug the application within Visual Studio. Perfect every time.

When I run the compiled .exe, whether from /BIN/ or wherever I choose to deploy it, it mangles the e-mail. It attaches no files, and simply sends something that looks like it serialized the file(s) into the e-mail text body:

cHMvYXBwLnhtbFBLAQItABQABgAIAAAAIQAkRr56YQEAAG0DAAAYAAAAAAAAAAAA
AAAAAKblAABjdXN0b21YbWwvaXRlbVByb3BzMy54bWxQSwECLQAUAAYACAAAACEA
te/KYaMAAADVAAAAEwAAAAAAAAAAAAAAAABl5wAAY3VzdG9tWG1sL2l0ZW0xLnht
bFBLAQItABQABgAIAAAAIQB/jqJ4TAEAAOYCAAAYAAAAAAAAAAAAAAAAAGHoAABj
dXN0b21YbWwvaXRlbVByb3BzMS54bWxQSwECLQAUAAYACAAAACEAvYRiI5AAAADb
AAAAEwAAAAAAAAAAAAAAAAAL6gAAY3VzdG9tWG1sL2l0ZW0yLnhtbFBLAQItABQA
BgAIAAAAIQCQ9oX78gAAAE8BAAAYAAAAAAAAAAAAAAAAAPTqAABjdXN0b21YbWwv
aXRlbVByb3BzMi54bWxQSwECLQAUAAYACAAAACEAZaNvWToEAACtDQAAEwAAAAAA
AAAAAAAAAABE7AAAY3VzdG9tWG1sL2l0ZW0zLnhtbFBLAQItABQABgAIAAAAIQDc
SoJLbAEAAJ4CAAARAAAAAAAAAAAAAAAAANfwAABkb2NQcm9wcy9jb3JlLnhtbFBL
BQYAAAAAGwAbADoHAAB68wAAAAA=


----boundary_0_61985b34-0fc6-4f91-99b8-464d8f613491

Content-Type: application/octet-stream; name="Microsoft Office 2003 Compatibility
 Installation Instructions.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment

----boundary_0_61985b34-0fc6-4f91-99b8-464d8f613491--

(That goobeldy goop is just a small portion of all the junk contained in the e-mail, but it looks the same throughout.)

I've tried using a different constructor when creating the attachment, to include a MIME-TYPE of "application/pdf" and what not. When I did, it changed the Content-Type in the e-mail body to application/pdf, but the e-mail still contained all the garbage and did not attach the file.

Why would it work within the IDE and not from the .EXE? This is odd behavior to me. Thanks.

SOLUTION

The specific solution was setting the TransferEncoding to QuotedPrintable using System.Net.Mime.

 var attachment = new System.Net.Mail.Attachment(filePath);
 attachment.TransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;
 mailMessage.Attachments.Add(attachment);

I now do this for each attachment and the files attach as desired.

هل كانت مفيدة؟

المحلول

The specific solution to my problem was setting the TransferEncoding to QuotedPrintable using System.Net.Mime.

var attachment = new System.Net.Mail.Attachment(filePath);  
attachment.TransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;
mailMessage.Attachments.Add(attachment); 

I now do this for each attachment and the files attach as desired.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top