将电子邮件作为附件附加到另一封电子邮件

时间:2014-03-03 02:39:25

标签: c#

我想知道如何将电子邮件作为附件附加到C#中的另一封电子邮件中。 详细说明:

  1. 我正在为outlook
  2. 编写插件
  3. 我在这一行收到错误:附件attach = new Attachment(mailItem,new System.Net.Mime.ContentType(“text / html; charset = us-ascii”));
  4. 错误消息是:无法创建抽象类或接口的实例'Microsoft.Office.Interop.Outlook.Attachment'
  5. 以下示例代码

    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);
    
            }
        }
    }
    

1 个答案:

答案 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指出我正确的方向