使用VBA从Outlook无法发送的电子邮件中检索电子邮件地址

时间:2020-06-09 14:22:53

标签: vba email outlook bounce

我有一系列宏来完成所需的任务,其中包括从Outlook上的1个特定帐户向每个销售专员发送电子邮件。该电子邮件地址是从Excel中说为B的销售助理名称中检索的。(FirstName LastName)我已经拆分了名称并附加了域。在极少数情况下,Sales Associates的电子邮件地址不只是FirstName LastName,因此我必须检测退回的电子邮件。

当电子邮件地址错误时,Outlook将发送“ Undeliverable”(NDR报告)。我正在尝试从此无法发送的邮件中检索电子邮件地址。有时,这些无法投递的电子邮件会更改为“中文”字符。

我不是VBA专家。我尝试了Internet上的多种解决方案,但似乎都没有用。

https://answers.microsoft.com/en-us/office/forum/office_2013_release-customize/problem-in-vba-reading-text-from-body-of-out-of/1d8ca369-a4c0-41da-9d28-3f490de3ed8c

Extract text string from undeliverable email body to excel

Outlook Undeliverable Bounce Report-Item Search Issues, VBA

2 个答案:

答案 0 :(得分:0)

您可以尝试使用ReportItem.Body属性获取电子邮件地址。如果仍然无法按照帖子中提到的页面所述进行操作,则可以:

  1. 使用基于Outlook的低级API-扩展MAPI或围绕该API的任何第三方包装,例如“兑换”。
  2. 将项目保存到磁盘,然后解析它并从此处获取所需的信息。

答案 1 :(得分:0)

对此进行详细说明,主要是因为Outlook中的项目实际上是REPORT.IPM.Note.NDR类ReportItem对象,而不是MailItem对象,因此您无法像通常使用使用VBA的MailItem正文。

对于背景,“主体”变得乱码的原因只是使用VBA访问Outlook ReportItem对象时出现的已知错误-内容​​实际上是由Outlook从各种隐藏的MAPI属性值实时渲染的,因此它们不会如您所见,它们确实存在,因此为什么“ body”值是gobbledygook。

昨天,我对"Extract text string from undeliverable email body to excel"问题的回答进行了更新,您说您已经遇到了这个完全相同的问题,并从各种来源进行了很多挖掘和拼凑而成的解决方案。

假设所有项目均为REPORT.IPM.Note.NDR类(如果使用Exchange,则通常为此类),则可以使用 ReportItem.PropertyAccessor.GetProperty 方法针对ReportItem对象进行访问,以访问许多通常不向VBA / OLE公开的MAPI属性值。

Sidupac中贷记this post,以列出您可能需要的一些更常见的内容,其各自的十六进制ID和Microsoft架构URL如下:

Sub PAValues(olReportItem As Outlook.ReportItem)

Dim PA As Outlook.propertyAccessor
Set PA = olReportItem.propertyAccessor

On Error Resume Next
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x001A001E"), , "PR_MESSAGE_CLASS"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0037001E"), , "PR_SUBJECT"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x00390040"), , "PR_CLIENT_SUBMIT_TIME"
MsgBox PA.BinaryToString(PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x003B0102")), , "PR_SENT_REPRESENTING_SEARCH_KEY"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x003D001E"), , "PR_SUBJECT_PREFIX PT_STRING8"
MsgBox PA.BinaryToString(PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x003F0102")), , "PR_RECEIVED_BY_ENTRYID"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0040001E"), , "PR_RECEIVED_BY_NAME"
MsgBox PA.BinaryToString(PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x00410102")), , "PR_SENT_REPRESENTING_ENTRYID"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0042001E"), , "PR_SENT_REPRESENTING_NAME"
MsgBox PA.BinaryToString(PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x004F0102")), , "PR_REPLY_RECIPIENT_ENTRIES"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0050001E"), , "PR_REPLY_RECIPIENT_NAMES"
MsgBox PA.BinaryToString(PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x00510102")), , "PR_RECEIVED_BY_SEARCH_KEY"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0064001E"), , "PR_SENT_REPRESENTING_ADDRTYPE"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0065001E"), , "PR_SENT_REPRESENTING_EMAIL_ADDRESS"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0070001E"), , "PR_CONVERSATION_TOPIC"
MsgBox PA.BinaryToString(PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x00710102")), , "PR_CONVERSATION_INDEX"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0075001E"), , "PR_RECEIVED_BY_ADDRTYPE"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0076001E"), , "PR_RECEIVED_BY_EMAIL_ADDRESS"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E"), , "PR_TRANSPORT_MESSAGE_HEADERS"
MsgBox PA.BinaryToString(PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0C190102")), , "PR_SENDER_ENTRYID"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0C1A001E"), , "PR_SENDER_NAME"
MsgBox PA.BinaryToString(PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0C1D0102")), , "PR_SENDER_SEARCH_KEY"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0C1E001E"), , "PR_SENDER_ADDRTYPE"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0C1F001E"), , "PR_SENDER_EMAIL_ADDRESS"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0E02001E"), , "PR_DISPLAY_BCC"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0E03001E"), , "PR_DISPLAY_CC"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0E04001E"), , "PR_DISPLAY_TO"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0E060040"), , "PR_MESSAGE_DELIVERY_TIME"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0E070003"), , "PR_MESSAGE_FLAGS"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0E080003"), , "PR_MESSAGE_SIZE"
MsgBox PA.BinaryToString(PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0E090102")), , "PR_PARENT_ENTRYID"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0E12000D"), , "PR_MESSAGE_RECIPIENTS"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0E13000D"), , "PR_MESSAGE_ATTACHMENTS"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0E1B000B"), , "PR_HASATTACH"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0E1D001E"), , "PR_NORMALIZED_SUBJECT"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0E1F000B"), , "PR_RTF_IN_SYNC"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0E28001E"), , "PR_PRIMARY_SEND_ACCT"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0E29001E"), , "PR_NEXT_SEND_ACCT"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0FF40003"), , "PR_ACCESS"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0FF70003"), , "PR_ACCESS_LEVEL"
MsgBox PA.BinaryToString(PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0FF80102")), , "PR_MAPPING_SIGNATURE"
MsgBox PA.BinaryToString(PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0FF90102")), , "PR_RECORD_KEY"
MsgBox PA.BinaryToString(PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0FFA0102")), , "PR_STORE_RECORD_KEY"
MsgBox PA.BinaryToString(PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0FFB0102")), , "PR_STORE_ENTRYID"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0FFE0003"), , "PR_OBJECT_TYPE"
MsgBox PA.BinaryToString(PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0FFF0102")), , "PR_ENTRYID"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x1000001E"), , "PR_BODY"
MsgBox PA.BinaryToString(PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x10090102")), , "PR_RTF_COMPRESSED"
MsgBox PA.BinaryToString(PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x10130102")), , "PR_HTML"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x1035001E"), , "PR_INTERNET_MESSAGE_ID"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x1045001E"), , "PR_LIST_UNSUBSCRIBE"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x1046001E"), , "N/A"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x30070040"), , "PR_CREATION_TIME"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x30080040"), , "PR_LAST_MODIFICATION_TIME"
MsgBox PA.BinaryToString(PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x300B0102")), , "PR_SEARCH_KEY"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x340D0003"), , "PR_STORE_SUPPORT_MASK"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x340F0003"), , "N/A"
MsgBox PA.BinaryToString(PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x34140102")), , "PR_MDB_PROVIDER"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3FDE0003"), , "PR_INTERNET_CPID"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x80050003"), , "SideEffects"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x802A001E"), , "InetAcctID"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x804F001E"), , "InetAcctName"
MsgBox PA.BinaryToString(PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x80660102")), , "RemoteEID"
MsgBox PA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x80AD001E"), , "x-rcpt-to"

End Sub

语法只是使用适当的架构URL作为参数的ReportItem.propertyAccessor的GetProperty方法(二进制值略有不同)

ReportItem.propertyAccessor.GetProperty("SCHEMA-URL")

ReportItem.propertyAccessor.BinaryToString(ReportItem.propertyAccessor.GetProperty("SCHEMA-URL")

分别用于标准属性和二进制属性,因此您可以根据需要将返回值处理为变量或与现有代码内联,无论它在做什么。

专门针对NDR中的原始收件人电子邮件地址,所需的架构条目为“ http://schemas.microsoft.com/mapi/proptag/0x0E04001E” “ PR_DISPLAY_TO” 属性值(与NDR相关的传出邮件的原始“收件人”地址)。

这可能仅返回一个电子邮件地址,如果“收件人”字段包含多个失败的收件人,则以分号分隔的列表,或显示名称

如果收件人在您的联系人中,则GetProperty方法返回其DisplayName而不是实际地址,如果所有收件人都是联系人,则可能是一个缺点。另外,如果接收者不止一个,则需要使用诸如Split函数之类的东西将它们分解为一个数组,以遍历它们以单独处理它们。

可以找到所有属性的完整枚举on this Microsoft resource,但是请注意,并非所有MAPI属性都可以通过GetProperty方法访问,只有那些十六进制值最高为0x8000的属性才可以访问,因此我从阅读此问题中了解到。

相关问题