我想知道如何将电子邮件作为附件附加到C#中的另一封电子邮件中。 详细说明:
以下示例代码
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);
}
}
}
答案 0 :(得分:0)
在做了一些阅读/搜索后,我找到了有用的东西。可能最困难的部分是第一行。
try
{
Outlook.MailItem tosend = (Outlook.MailItem)Globals.ThisAddIn.Application.CreateItem(Outlook.OlItemType.olMailItem);
tosend.Attachments.Add(mailItem);
tosend.To = "blah@blah.com";
tosend.Subject = "test";
tosend.Body = "blah";
tosend.Save();
tosend.Send();
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught: ", ex);
}
感谢@Kris Vandermotten指出我正确的方向