Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

Programmatically attach an embedded resource file to an e-mail message

$
0
0

I have no idea what I'm doing.

I am attempting to programmatically attach an embedded resource file to an e-mail message. Here is the snippet:

Outlook.Application outlookApp = new Outlook.Application();
Outlook.MailItem mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);

Assembly myAssembly = Assembly.GetExecutingAssembly();
Stream myStream = myAssembly.GetManifestResourceStream("MyNameSpace.Resources.filename.pdf");

mailItem.Attachments.Add(myStream, System.Net.Mime.MediaTypeNames.Application.Pdf);

The constructor I am using for an Attachment is Attachment(Stream, ContentType). The last line of the snippet returns the following exception:

System.Runtime.InteropServices.COMException (0x800200005): Type mismatch. (Exception from HRESULT:0x80020005 (DISP_E_TYPEMISMATCH))

myStream is of the type System.IO.UnmanagedMemoryStream.

I've also tried using the Attachment(Stream, String) constructor with the same results: mailItem.Attachments.Add(myStream,"application/pdf").

What am I missing? Am I going about this all wrong?


Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>