2
votes

I am trying to attach PDF document to MailMessage Send method in Windows Form Application. I am using below code to attach document to email.

mail.Attachments.Add(new Attachment(@"c:\Files\churchapplication.pdf"));

Email is not generated and no exception is raised either so i am unable to troubleshoot the problem. Same code works if I attach text file instead of pdf. So what could be the issue? Any ideas? PDF file size 291 KB. Is there some restrictions with attaching PDF documents in .NET?

Thanks.

1
Did you already try to debug with some local SMTP debugging server (like Papercut)? If it works with .txt but not with .pdf, I'd suspect some server side problem. - grek40

1 Answers

0
votes

You can try this multi-step approach.

//First create FileContent
FileContentResult fileContent = File(fileName, "application/pdf", "file.pdf");

MemoryStream ms = new MemoryStream(fileContent.FileContents); 

// Create an in-memory System.IO.Stream
ContentType ct = new ContentType(fileContent.ContentType);

Attachment a = new Attachment(ms, ct);

sender.SendMail("email", "email", "subject", "Body", a);