简体   繁体   English

使用 VBA 从 Outlook 的无法投递 email 中检索 email 地址

[英]Retrieve email address from Outlook's Undeliverable email using VBA

I have a chain of macros to achieve a desired task which involves sending an email from 1 particular account on Outlook to each individual Sales Associates.我有一个宏链来实现所需的任务,其中包括从 Outlook 上的一个特定帐户向每个销售助理发送 email。 The email address is retrieved from the Sales Associates name in Excel say Col B. (FirstName LastName) I have split the name and attached the domain. email 地址是从 Excel 中的 Sales Associates 名称中检索到的,例如 Col B。(FirstName LastName)我已经拆分了名称并附加了域。 There are rare cases when Sales Associates email address is not just FirstName LastName, so I have to detect the bounced email.销售助理 email 地址不仅仅是 FirstName LastName 的情况很少见,因此我必须检测退回的 email。

When email address is wrong, outlook sends "Undeliverable" (NDR report).当 email 地址错误时,outlook 发送“无法投递”(NDR 报告)。 I am trying to retrieve the email address from this Undeliverable message.我正在尝试从此无法投递的消息中检索 email 地址。 Sometimes these undeliverable emails are changed to "Chinese" characters.有时这些无法投递的邮件会被改成“中文”字符。

I am no expert in VBA.我不是 VBA 方面的专家。 I have tried multiple solutions from Internet and none seems to work.我已经尝试了来自 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 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 从无法投递的 email 正文中提取文本字符串到 excel

Outlook Undeliverable Bounce Report-Item Search Issues, VBA Outlook 无法投递的退回报告 - 项目搜索问题,VBA

You can try to use the ReportItem.Body property to get the email addresses.您可以尝试使用ReportItem.Body属性来获取 email 地址。 If it still doesn't work as stated on the pages mentioned in your post you can:如果它仍然无法按照您帖子中提到的页面上的说明工作,您可以:

  1. Use a low-level API on which Outlook is based on - Extended MAPI or just any third-party wrappers around this API such as Redemption.使用 Outlook 所基于的低级 API - 扩展 MAPI 或围绕此 API 的任何第三方包装器,例如 Redemption。
  2. Save the item to the disk, then parse it and get the required information from here.将项目保存到磁盘,然后对其进行解析并从此处获取所需信息。

Elaborating a bit on this, essentially because the item in Outlook is actually REPORT.IPM.Note.NDR class ReportItem object, and not a MailItem object, you can't pull info from it in the way you normally would with a MailItem Body using VBA. Elaborating a bit on this, essentially because the item in Outlook is actually REPORT.IPM.Note.NDR class ReportItem object, and not a MailItem object, you can't pull info from it in the way you normally would with a MailItem Body using VBA。

For background, the reason the 'body' becomes garbled is just a known bug with Outlook ReportItem objects when accessed with VBA - the contents are actually rendered by Outlook on the fly from various hidden MAPI property values, so they don't really exist as you see them, hence why the 'body' value is gobbledygook. For background, the reason the 'body' becomes garbled is just a known bug with Outlook ReportItem objects when accessed with VBA - the contents are actually rendered by Outlook on the fly from various hidden MAPI property values, so they don't really exist as您会看到它们,因此为什么“body”值是 gobbledygook。

I made an update to my answer yesterday on the "Extract text string from undeliverable email body to excel" question you said you already looked at after having this exact same problem, doing lots of digging and piecing together a solution from various sources.我昨天对“从无法交付的 email 正文中提取文本字符串到 excel”问题的答案进行了更新,您说您在遇到同样的问题后已经看过,做了很多挖掘和拼凑来自各种来源的解决方案。

Assuming the items are all REPORT.IPM.Note.NDR class (which they are usually if using Exchange) then you can use the ReportItem.PropertyAccessor.GetProperty method against the ReportItem object to access many of the MAPI property values that aren't normally exposed to VBA/OLE.假设这些项目都是 REPORT.IPM.Note.NDR class(如果使用 Exchange,它们通常是),那么您可以对 ReportItem object 使用ReportItem.PropertyAccessor.GetProperty方法来访问许多不正常的 MAPI 属性值暴露于 VBA/OLE。

Credit to Sidupac in this post for listing out some of the more common ones you might want with their respective Hex ID and their Microsoft Schema URLs as below:感谢 Sidupac这篇文章中列出了一些您可能想要的更常见的,以及它们各自的 Hex ID 和它们的 Microsoft Schema 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

The syntax is simply the GetProperty method of the ReportItem.propertyAccessor with the appropriate Schema URL as the argument (slightly different for binary values)语法只是 ReportItem.propertyAccessor 的 GetProperty 方法,使用适当的 Schema URL 作为参数(二进制值略有不同)

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

or或者

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

for standard and binary properties respectively, so you can handle the return values as you wish, into variables or in-line with your existing code, whatever it's doing.分别用于标准和二进制属性,因此您可以根据需要将返回值处理到变量中或与现有代码内联,无论它在做什么。

Specifically for the original recipient email address(es) from the NDR, the schema entry you want is "http://schemas.microsoft.com/mapi/proptag/0x0E04001E" which gives you "PR_DISPLAY_TO" property value (the original To address(es) of the outgoing message to which the NDR relates).特别是对于来自 NDR 的原始收件人 email 地址,您想要的架构条目是“http://schemas.microsoft.com/mapi/proptag/0x0E04001E” ,它为您提供“PR_DISPLAY_TO”属性值(原始收件人地址(es) 与 NDR 相关的传出消息)。

This could return just an email address, a semicolon delimited list if the 'To' field contained more than one recipient which failed, or a DisplayName这可能只返回一个 email 地址,如果“收件人”字段包含多个失败的收件人,则返回一个分号分隔的列表,或者一个 DisplayName

If the recipient is in your contacts, the GetProperty method returns their DisplayName as opposed to the actual address, which could be a shortcoming if all your recipients are contacts.如果收件人在您的联系人中,则 GetProperty 方法返回他们的 DisplayName 而不是实际地址,如果您的所有收件人都是联系人,这可能是一个缺点。 Also, if there was more than one recipient, you would need to use something like the Split function to break those down into an array to iterate through them to handle them individually.此外,如果有多个收件人,您将需要使用 Split function 之类的东西将它们分解为一个数组以遍历它们以单独处理它们。

Full enumeration of all properties can be found on this Microsoft resource but be aware that not all MAPI properties are accessible to the GetProperty method, only those with a hex value up to 0x8000 so I understand from my reading around this issue.可以在此 Microsoft 资源上找到所有属性的完整枚举,但请注意并非所有 MAPI 属性都可以通过 GetProperty 方法访问,只有那些十六进制值高达 0x8000 的属性才能访问,所以我从我对这个问题的阅读中了解到。

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

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