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

4

我想知道如何在C#中将电子邮件作为附件附加到另一个电子邮件上。

细节:

  1. I am writing a plugin for outlook
  2. I am getting the error on this line : Attachment attach = new Attachment(mailItem, new System.Net.Mime.ContentType("text/html; charset=us-ascii"));
  3. Error message is : Cannot create an instance of the abstract class or interface 'Microsoft.Office.Interop.Outlook.Attachment'
  4. Sample code below

    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中创建并发送呢? - Kris Vandermotten
没有任何原因。这是我第一次尝试使用Outlook做任何事情。如果有更简单的方法来使用Outlook发送邮件,那么我想那会更好。我现在会去看看。如果您知道任何可以帮助我的资源,请告诉我。 - SKR
1个回答

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 指引我正确的方向。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接