简体   繁体   中英

Add generated attachment to Outlook email

I got some working code to add attachement to Outlook. But we have to specify the physical location of file while adding attachment. In my scenario, i am exporting a datatable to word document and need to attach that document to email, without saving the document in a file folder. Code follows..

Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook._MailItem oMailItem = (Microsoft.Office.Interop.Outlook._MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
oMailItem.To = "test@gmail.com";
oMailItem.Subject = "Outlook Email test Subject";
StringWriter strwritter = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(strwritter);
GridView gv = new GridView();
DataTable dtWord = GetDataItem();
gv.DataSource = dtWord;
gv.DataBind();
gv.RenderControl(htmlWrite);
MemoryStream ms = null;
StreamWriter sw = new StreamWriter(ms);
sw.WriteLine(sb.ToString());
sw.Flush();
ms.Seek(0, SeekOrigin.Begin);

string fileName = @"D:\test.doc";
oMailItem.Attachments.Add(fileName, Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing);
oMailItem.Display(true);

So, i need to add memory stream to email as attachment. Please help. Thanks alot!

You cannot do that. Attachments.Add in Outlook takes either a string with the attachment file name or another Outlook item (MailItem, etc.) as a parameter.

If using Redemption is an option, its RDOAttachments .Add method also takes IStream COM interface as a parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM