将所有电子邮件数据从 Outlook 插件发送到服务

Send all email data to a service from an outlook addin

我是 office 插件的新手。我是一名 MVC 程序员,但这个项目已被甩给了我,因为没有其他人愿意这样做。我需要创建一个 outlook 插件,它将所有电子邮件数据转发到招聘系统可以跟踪通信的服务。
我正在使用

Application.ItemSend += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemSendEventHandler(saveEmail); 

然后我将电子邮件转换为 Outlook.MailItem。问题是我看不到获取发件人和收件人电子邮件地址的方法。它给我的只是人民的名字。我错过了什么吗?

到目前为止,我能想到的最佳解决方案是将 msg 保存为 .msg 文件。将其转发给我的服务,然后使用我发现的解析器将其转换为 HTML.

有什么建议吗?

要访问收件人,循环遍历 MailItem.Recipients 集合并访问 Recipient.Name 和 Recipient.Address 属性。

发件人相关属性在 ItemSend 事件触发时尚未设置 - 您最早可以访问发件人属性的时间是 Items.ItemAdd 事件在“已发送邮件”文件夹上触发(使用 Namespace.GetDefaultFolder 检索) .

您可以阅读MailItem.SendUsingAccount。如果为空,则使用 Namespace.Acounts 集合中的第一个帐户。然后你可以使用 Account.Recipient object.

请记住,您不应盲目地将外发项目投射到 MailItem 对象 - 您也可以拥有 MeetingItem 和 TaskRequestItem 对象。

好的,使用 Dmitry Streblechenko 给我的信息和我刚刚在这里查找的其他一些信息是我目前的解决方案。

在 ItemSend 事件中,我首先确保将已发送的电子邮件移至默认的已发送邮件文件夹。我正在使用 gmail 测试 outlook,所以通常这些会去其他地方。 sentMailItems 是作为一个 class 字段制作的,因为如果它只是在 Startup 函数中声明它显然会被垃圾收集(这对我来说很奇怪,一个 MVC 程序员 :))。

当我回到办公室时,我会在交易所测试这个,希望一切顺利。

public partial class ThisAddIn
{

    public Outlook.Items sentMailItems;

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        Application.ItemSend += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemSendEventHandler(ItemSend);
        sentMailItems = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail).Items;
        sentMailItems.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);
    }

    void Items_ItemAdd(object item)
    {
        MessageBox.Show(((Outlook.MailItem)item).Subject);

        var msg = Item as Outlook.MailItem;


        string from = msg.SenderEmailAddress;

        string allRecip = "";
        foreach (Outlook.Recipient recip in msg.Recipients)
        {
            allRecip += "," + recip.Address;
        }
    }


    private void ItemSend(object Item, ref bool Cancel)
    {
        if (!(Item is Outlook.MailItem))
            return;

        var msg = Item as Outlook.MailItem;

        msg.DeleteAfterSubmit = false; // force storage to sent items folder (ignore user options)
        Outlook.Folder sentFolder = this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail) as Outlook.Folder;
        if (sentFolder != null)
            msg.SaveSentMessageFolder = sentFolder; // override the default sent items location
        msg.Save();            

    }
    //Other auto gen code here....
}