如何确定 Office 365 上的退回电子邮件消息?
How to decide Bounce Back EmailMessage on Office365?
我正在使用 Exchange 网络服务阅读来自 Office365 的电子邮件。 C# 程序运行良好。我可以在收件箱中获取EmailMessage,并获取他们的主题和邮件正文等。但是,我无法弄清楚该邮件是否是退回邮件的方法。
我是不是要解析邮件正文,看看有没有特殊的句子,ie。邮件发送失败?如果是这样,是否有可能不同的电子邮件服务器用不同的词退回电子邮件?即有些使用 'Mail Delivery Failure',有些使用 'Mail Delivery Not Succeeded'? (只是举个例子,不知道是不是这样)
或者,消息对象具有可用于此目的的属性?
谢谢
*** 刚刚发现exchange webservice 看不到INBOX 中的'Bounce back' 消息。我正在使用下面的代码,所有消息都可以是 'seen' 除了反弹消息。我是否遗漏了任何东西来过滤退回消息?居然在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" 的 class。
我在 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);
希望对您有所帮助。
我正在使用 Exchange 网络服务阅读来自 Office365 的电子邮件。 C# 程序运行良好。我可以在收件箱中获取EmailMessage,并获取他们的主题和邮件正文等。但是,我无法弄清楚该邮件是否是退回邮件的方法。
我是不是要解析邮件正文,看看有没有特殊的句子,ie。邮件发送失败?如果是这样,是否有可能不同的电子邮件服务器用不同的词退回电子邮件?即有些使用 'Mail Delivery Failure',有些使用 'Mail Delivery Not Succeeded'? (只是举个例子,不知道是不是这样)
或者,消息对象具有可用于此目的的属性?
谢谢
*** 刚刚发现exchange webservice 看不到INBOX 中的'Bounce back' 消息。我正在使用下面的代码,所有消息都可以是 'seen' 除了反弹消息。我是否遗漏了任何东西来过滤退回消息?居然在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" 的 class。
我在 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);
希望对您有所帮助。