繁体   English   中英

如何确定Office365上的退回电子邮件消息?

[英]How to decide Bounce Back EmailMessage on Office365?

我正在使用Exchange Web服务从Office365中读取电子邮件。 C#程序运行正常。 我可以在“收件箱”中获取EmailMessage,并获取其主题和邮件正文等。但是,我不知道检查邮件是否为退回邮件的方法。

我是否必须解析消息正文以查看是否有一些特殊的句子,即。 邮件传递失败? 如果是这样,是否有可能不同的电子邮件服务器回弹带有不同单词的电子邮件? 即有些使用“邮件传递失败”,有些使用“邮件传递不成功”? (仅举一个例子,我不知道这是否是真的)

还是消息对象具有可用于此目的的属性?

谢谢

***刚刚发现,交换Web服务在INBOX中看不到“反弹”消息。 我正在使用以下代码,除“回弹”消息外,所有消息都可以“看到”。 我会错过任何可过滤回弹邮件的内容吗? 它们实际上位于INBOX中,尚未读取,我可以从Office365页中看到它。

private static void ProcessEmailMessages(SearchFolder searchFolder, Folder folderHistory, Folder folderBounceBack)
{
    if (searchFolder == null)
    {
        return;
    }

    const Int32 pageSize = 50;
    ItemView itemView = new ItemView(pageSize);

    PropertySet itempropertyset = new PropertySet(BasePropertySet.FirstClassProperties);
    itempropertyset.RequestedBodyType = BodyType.Text;
    itemView.PropertySet = itempropertyset;

    PropertySet propertySet = new PropertySet(BasePropertySet.IdOnly, FolderSchema.DisplayName);
    folderHistory.Load(propertySet);
    folderBounceBack.Load(propertySet);

    FindItemsResults<Item> findResults = null;
    do
    {
        findResults = searchFolder.FindItems(itemView);
        foreach (Item item in findResults.Items)
        {
            if (item is EmailMessage)
            {
                // load body text
                item.Load(itempropertyset);

                EmailMessage email = item as EmailMessage;
                //email.Move(folder.Id);

                // check email subject to find the bounced emails
                bool subjectContains = Regex.IsMatch(email.Subject, "Mail Delivery Failure", RegexOptions.IgnoreCase);
                bool bodyContains = Regex.IsMatch(email.Subject, "Delivery", RegexOptions.IgnoreCase);
                if (subjectContains || bodyContains)
                {
                    email.Move(folderBounceBack.Id);
                    Console.WriteLine("Move the Bounced email: {0}", email.Subject);

                    ShowMessageInfo(email);
                }
                else
                {
                    email.Move(folderHistory.Id);
                    Console.WriteLine(">>> Keep the email: {0}", email.Subject);
                }
            }
        }

        itemView.Offset += pageSize;
    } while (findResults.MoreAvailable);
}

检查ItemClass属性。 这样的消息应该具有一个包含“ REPORT”的类。

我在O365环境中使用EWS来帮助处理反弹,并使用以下代码从用户的收件箱中获取NDR。

var sf = new SearchFilter.IsEqualTo(ItemSchema.ItemClass, "REPORT.IPM.Note.NDR");
var SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, sf);
var view = new ItemView(1000) {PropertySet = new PropertySet(BasePropertySet.IdOnly)};
var findResults = service.FindItems(WellKnownFolderName.Inbox, searchFilter, view);

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM