【发布时间】:2014-03-04 09:09:59
【问题描述】:
我想知道如何在 C# 中将电子邮件作为附件附加到另一封电子邮件。 详情:
- 我正在为 Outlook 编写插件
- 我在这一行收到错误消息:Attachment attach = new Attachment(mailItem, new System.Net.Mime.ContentType("text/html; charset=us-ascii"));
- 错误消息是:无法创建抽象类或接口“Microsoft.Office.Interop.Outlook.Attachment”的实例
-
下面的示例代码
private void button1_Click(object sender, RibbonControlEventArgs e) { Explorer explorer = Globals.ThisAddIn.Application.ActiveExplorer(); Selection selection = explorer.Selection; if (selection.Count > 0) // Check that selection is not empty. { object selectedItem = selection[1]; // Index is one-based. MailItem mailItem = selectedItem as MailItem; if (mailItem != null) // Check that selected item is a message. { System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); message.To.Add("blah@blah.com"); message.Subject = "blah"; message.From = new System.Net.Mail.MailAddress("test@test.com"); message.Body = "This is the message body"; Attachment attach = new Attachment(mailItem, new System.Net.Mime.ContentType("text/html; charset=us-ascii")); message.Attachments.Add(attach); System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smlsmtp"); smtp.Send(message); } } }
【问题讨论】:
-
如果您正在与 Outlook 集成,从中读取 MailItems,为什么要使用 SMTP 发送邮件?为什么不在 Outlook 中创建和发送?
-
完全没有理由。这是我第一次尝试用 Outlook 做任何事情。如果有更简单的方法可以使用 Outlook 发送邮件,那么我想那会更好。从现在开始我去看看。如果您知道任何可以帮助的资源,请告诉我。
标签: c#