简体   繁体   English

将生成的附件添加到Outlook电子邮件

[英]Add generated attachment to Outlook email

I got some working code to add attachement to Outlook. 我有一些工作代码可将附件添加到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. 在我的方案中,我将数据表导出到Word文档,并且需要将该文档附加到电子邮件,而无需将文档保存在文件夹中。 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. Outlook中的Attachments.Add使用带有附件文件名的字符串或另一个Outlook项目(MailItem等)作为参数。

If using Redemption is an option, its RDOAttachments .Add method also takes IStream COM interface as a parameter. 如果使用赎回是一个选项,则其RDOAttachments .Add方法还将IStream COM接口作为参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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