如何获取 Outlook 邮件的发件人 [英] How to get the sender of an outlook message
问题描述
我有一些代码可以部分填充电子邮件作为回复.但是除非他们在(一个/我们的)上,否则我无法得到发件人?交换服务器.
I have some code that partially populates an email message as a reply. But I can not get the sender unless they are on (an / our)? exchange server.
Public Sub CreateMessage()
Dim EmailFrom As String
Dim NewMessage As Outlook.MailItem
Dim OldMessage As Outlook.MailItem
Set OldMessage = Application.ActiveInspector.CurrentItem
Set NewMessage = Application.CreateItem(olMailItem)
EmailFrom = OldMessage.Sender.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x39FE001E")
NewMessage.Body = Body(EmailFrom)
NewMessage.HTMLBody = HTMLBody(EmailFrom)
NewMessage.Recipients.Add (EmailFrom)
NewMessage.Display
Set NewMessage = Nothing
End Sub
我收到的消息是属性"http://schemas.microsoft.com/mapi/proptag/0x39FE001E未知或无法找到."它似乎只适用于内部消息.
The message I receive is "The property "http://schemas.microsoft.com/mapi/proptag/0x39FE001E" is unknown or cannot be found." and it only appears to work with internal messages.
有人知道在 Outlook VBA 中获取适用于所有人的邮件的发件人的方法吗?
Anyone know of a way in Outlook VBA to get the sender of a mail message that works for all of them?
根据 Dimitry 的评论修正:
Fixed based on Dimitry's comments:
Public Sub CreateMessage()
Dim EmailFrom As String
Dim NewMessage As Outlook.MailItem
Dim OldMessage As Outlook.MailItem
Set OldMessage = Application.ActiveInspector.CurrentItem
Set NewMessage = Application.CreateItem(olMailItem)
Select Case OldMessage.SenderEmailType
Case "EX"
EmailFrom = OldMessage.Sender.GetExchangeUser.PrimarySmtpAddress
Case Else
EmailFrom = OldMessage.SenderEmailAddress
End Select
NewMessage.Body = Body(EmailFrom)
NewMessage.HTMLBody = HTMLBody(EmailFrom)
NewMessage.Recipients.Add (EmailFrom)
NewMessage.Display
Set NewMessage = Nothing
End Sub
推荐答案
您正在请求 PR_SMTP_ADDRESS
属性,这是 Exchange 特定的.检查 SenderEmailType
是否为EX",然后才读取 PR_SMTP_ADDRESS
属性.否则只需阅读 MailItem.SenderEmailAddress
属性.
You are requesting the PR_SMTP_ADDRESS
property, which is Exchange specific. Check if SenderEmailType
is "EX" and only then read the PR_SMTP_ADDRESS
property. Otherwise just read the MailItem.SenderEmailAddress
property.
使用 OutlookSpy 查看消息(单击 IMessage)以查看可用属性.
Take a look at the message with OutlookSpy (click IMessage) to see the available properties.
这篇关于如何获取 Outlook 邮件的发件人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!