【问题标题】:can't get the body/attachments of new mails in outlook无法在 Outlook 中获取新邮件的正文/附件
【发布时间】:2014-04-01 20:36:36
【问题描述】:

我使用 C# 开发了一个 Outlook 插件,对于收到的每封新邮件,我都会获取(并保存)发件人/主题/电子邮件正文和附件。好吧,最后2让我头疼。我可以看到新邮件的发件人和主题,但对于正文和附件,这似乎是个问题。我使用 NewMailEx 来获取收件箱中的新邮件。函数如下所示:

private void Application_NewMailEx(string EntryIDCollection)
    {

        string[] entryIdArray = EntryIDCollection.Split(',');
        foreach (string entryId in entryIdArray)
        {


            try
            {
                Outlook.MailItem item =   (Outlook.MailItem)Application.Session.GetItemFromID(EntryIDCollection, null);

                string subj = item.Subject; //works
                string to = item.To; //works
                string bec = item.BCC; //does not work but dont care
                string body = item.Body; //DOES NOT SAVE THE BODY OF THE NEW MAIL RECEIVED


                string final = "Sender: " + item.SenderEmailAddress + "\r\n" + "Subject: " + subj + "\r\n" + "BCC: " + bec + "\r\n" + "TO: " + to + "\r\n\n" + "Body: " + body + "\r\n\n";
                System.IO.File.AppendAllText(@"D:\tmp\atr.txt", final);
                 //the result of item.attachments.count is always 0 , even though I've 
                 //sent mails with a different number of attachments. So the if 
                 //statement is false                                 
                if (item.Attachments.Count > 0) 
                {
                    for (int i = 1; i <= item.Attachments.Count; i++)
                    {
                        item.Attachments[i].SaveAsFile(@"D:\tmp\" + item.Attachments[i].FileName);
                    }
                }





               Marshal.ReleaseComObject(item);

            }
            catch (System.Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
    }

【问题讨论】:

    标签: c# com outlook add-in


    【解决方案1】:

    Item 变量从何而来?您需要使用 Application.Session.getItemfromID() 对其进行初始化。

    【讨论】:

    • 抱歉,我在编辑邮件时不小心删除了该部分。现在看代码
    • 那么你收到错误了吗?还是只是 Body 是 "" 而 Attachments.Count 是 0?
    • body 为 NULL 且 attachments.count 为 0,是的
    • 这是什么消息存储?太平洋标准时间/交易所? IMAP4?
    • 我有一个 Outlook 2010 并且邮件存储是 IMAP
    【解决方案2】:

    我正在使用 VBA 编写代码,所以我觉得在这里发布我的代码是一种失礼,但我已经使用 Outlook 应用程序中的类似对象库找到了一个解决方案,我认为它可以很好地转移到您的 C++ 意图。

    首先,切换到 POP3 固然可以解决这个问题,但是你会被 POP3 卡住,这只是从编程的角度来看是理想的。

    我找到的解决方案遵循这个算法:

     //Generate Outlook MailItem Object, "item"
        //If item.DownloadState is not 1,
            //item.Display
            //item.Close(1)
            //Perform end of code operations
            //Call Function that is identical to Application_NewMailEx but is not Application_NewMailEx because Application_NewMailEx is a function that is thrown during the incoming mail event.
        //Else,
            //Perform Intended Code
    

    您看到调用与 Application_NewMailEx 相同的函数是如何创建一种循环的,因为如果 item.DownloadState 不是 1,您将再次调用该函数?我知道这不是最理想的编码实践,但我已经搜索了互联网,Outlook 应用程序和 Outlook 对象库专家不知道如何以任何其他方式解决这个问题(事实上,甚至没有人提出这个解决方案)

    查看我的完整 VBA 解决方案:

    https://superuser.com/questions/894972/outlook-strange-item-attachments-error/990968#990968

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-14
      • 1970-01-01
      • 2016-05-28
      • 1970-01-01
      • 2023-03-27
      • 2017-05-13
      • 2013-04-24
      相关资源
      最近更新 更多