【问题标题】:Attaching an email as an attachment to another email将电子邮件作为附件附加到另一封电子邮件
【发布时间】:2014-03-04 09:09:59
【问题描述】:

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

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

    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#


【解决方案1】:

在做了更多的阅读/搜索之后,我找到了一些有用的东西。可能最困难的部分是第一行。

                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 为我指明了正确的方向

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 2014-06-25
    • 1970-01-01
    相关资源
    最近更新 更多