简体   繁体   English

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

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

I'm using Exchange web service to read email from Office365. 我正在使用Exchange Web服务从Office365中读取电子邮件。 The C# program works fine. C#程序运行正常。 I can get EmailMessage in Inbox, and get their subject and message body, etc. However, I could not figure out the way to check whether the message is a bounce back message or not. 我可以在“收件箱”中获取EmailMessage,并获取其主题和邮件正文等。但是,我不知道检查邮件是否为退回邮件的方法。

Do I have to parse the message body to see whether there are some special sentence, ie. 我是否必须解析消息正文以查看是否有一些特殊的句子,即。 Mail Delivery Failure? 邮件传递失败? If so, is it possible different email servers bounce back emails with different words? 如果是这样,是否有可能不同的电子邮件服务器回弹带有不同单词的电子邮件? ie some use 'Mail Delivery Failure', some use 'Mail Delivery Not Succeeded'? 即有些使用“邮件传递失败”,有些使用“邮件传递不成功”? (just an example, I do not know whether this is true) (仅举一个例子,我不知道这是否是真的)

Or, the message object has an attribute that can be used for this purpose? 还是消息对象具有可用于此目的的属性?

Thanks 谢谢

*** Just found that the exchange webservice can not see 'Bounce back' messages in INBOX. ***刚刚发现,交换Web服务在INBOX中看不到“反弹”消息。 I'm using below code, all messages can be 'seen' except Bounce Back ones. 我正在使用以下代码,除“回弹”消息外,所有消息都可以“看到”。 Do I miss anything to filter te bounce back messages? 我会错过任何可过滤回弹邮件的内容吗? They are actually in INBOX, unread, and I can see it from Office365 page. 它们实际上位于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);
}

Check the ItemClass attribute. 检查ItemClass属性。 Messages like that should have a class that contains "REPORT" in it. 这样的消息应该具有一个包含“ REPORT”的类。

I use EWS in an O365 environment to help process bounce backs and use the following code to just get the NDRs from a users' inbox. 我在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);

Hope it helps. 希望能帮助到你。

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

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