WDalrymple
WDalrymple

Reputation: 1

Adding a dynamic attachment to an email in c#

Folks,
Using the System.Diagnostics.Process.Start("mailto: method of creating an email, is there a way to add a dynamic attachment (not a saved file) to the email?

I'm pretty much doing the same as this the person in this question but no-one has answered using the mailto: method. Im just wondering if its possible, and how to do it.

I've tried this but to no avail:

System.IO.MemoryStream ms = new System.IO.MemoryStream(generatedReport.DocumentBytes);
System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Application.Pdf);
Attachment attachment = new Attachment(ms, ct);
attachment.ContentDisposition.FileName = "output.pdf";                            
System.Diagnostics.Process.Start("mailto:myemail &SUBJECT=Test Subject BODY=Body Text&Attachment=" + attachment);
ms.Close();

Any and all help is appreciated

Upvotes: 0

Views: 982

Answers (1)

Heinzi
Heinzi

Reputation: 172448

In general, the mailto: URL scheme does not support attachments. Thus, you should not use it at all if you need it to work reliably with attachments.

Apparently, some mail clients still support passing Attachment=..., but they expect the ... part to be the path of a local file. Thus, in your case, you need to

  • save the file to disk (you can use a temporary file name in a temporary folder) and then
  • pass the path of the file to your mailto: link.

Note that you will have to keep the file around until the user has actually sent the mail, so you might have to think about "cleaning up" those temporary files at a later time.

Upvotes: 3

Related Questions