通过Outlook通过电子邮件发送固定文档

时间:2010-03-12 11:10:51

标签: c# wpf email

我为应用程序添加了一些功能,它将一堆信息打印到FixedDOcument并将其发送给打印机。这工作得很好,但请求是有一个应用程序功能,使用OUtlook通过电子邮件发送文档,我在这里解开它。我非常希望重复使用固定文档进行打印的类来生成电子邮件的文本,但我很难做到这一点。

我尝试了以下内容......

Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
MailItem email = (MailItem)(oApp.CreateItem(OlItemType.olMailItem));
email.Recipients.Add("someone@somewhere.com");
email.Subject = "Hello";
email.Body = "TEST";
FixedDocument doc = CreateReport(); //make my fixed document

//this doesn't work, and the parameters it takes suggest it never will
email.Attachments.Add(doc, OlAttachmentType.olByValue, 1, null); 
email.Send();

我不禁想到我在这里完全错了,但我真的不想写一堆新的文本格式(因为email.Body只需要一个字符串)当我'我已经按照我想要的格式获取了内容。

请注意,内容都是文字内容,因此我并不关心它是作为附件发送还是作为电子邮件正文中的文本发送。理想情况下,如果它作为附件发送,它将不会永久保存在任何地方。

任何指针?

1 个答案:

答案 0 :(得分:1)

将固定文档保存到用户临时文件夹,而不是将文档的完整路径名放在附件中,发送它,然后从临时文件夹中删除文档。

附件签名

Attachment Add (
    [InAttribute] Object Source,
    [OptionalAttribute] [InAttribute] Object Type,
    [OptionalAttribute] [InAttribute] Object Position,
    [OptionalAttribute] [InAttribute] Object DisplayName
)

来源参数:

Source
The source of the attachment. This can be a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment.

保存文件:

String TempFoder = System.Environment.GetEnvironmentVariable("TEMP");
String tempFileName = TempFoder + @"\TempFixedDocument.doc";
FixedDocument.Save(tempFileName);

发送电子邮件:

Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
MailItem email = (MailItem)(oApp.CreateItem(OlItemType.olMailItem));
email.Recipients.Add("someone@somewhere.com");
email.Subject = "Hello";
email.Body = "TEST";
FixedDocument doc = CreateReport(); //make my fixed document

//this doesn't work, and the parameters it takes suggest it never will
email.Attachments.Add(tempFileName, OlAttachmentType.olByValue, 1, null); 
email.Send();

删除临时文件:

System.IO.File.Delete(tempFileName);