I found a similar question but for c# Outlook : How to get email from Recipient field?
In this question there is also no correct answer.
Still, I tried some of the answers from this thread
recipient.AddressEntry.Address <= returns same thing as
recipient.Address what I used till yet and what gievs me following result:
"/o=POST/ou=Zuerich/cn=Recipients/cn=eicherr"
How can I get email adress of recipient?
I tried also .AddressEntry.GetContact().Email1Address and .AddressEntry.GetExchangeUser().Address this all not works for me.
Here is a MSDN docs explanes how to get email adress but I don't undestand how can I use it in my code Obtain the E-mail Address of a Recipient
my code:
Dim Msg As Outlook.MeetingItem
Set Msg = Item
Set recips = Msg.Recipients
Dim recip As Outlook.Recipient
For Each recip In recips
Dim email as String
email = CStr(recip.Address)
Debug.Print email
End For
If Recipient.AddressEntry.Type <> "EX", just use Recipient.Adddress. Otherwise use Recipient.AddressEntry.GetExchangeUser().PrimarySmtpAddress
The documentation on the link I inserted finally helped me solve this problem.
For Each recip In recips
'Obtain the E-mail Address of a Recipient
Dim pa As Outlook.PropertyAccessor
Const PR_SMTP_ADDRESS As String = _
"http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
Set pa = recip.PropertyAccessor
Dim email as String
email = CStr(pa.GetProperty(PR_SMTP_ADDRESS))
Debug.Print email
End For
Related
I'm trying to check outgoing email addresses and to do so I use the following code:
Dim objMail As Outlook.MailItem
Set objMail = Item
Dim objRecipients As Outlook.Recipients
Set objRecipients = objMail.Recipients
and use it like this:
Set objRecipients = objMail.Recipients
For i = objRecipients.Count To 1 Step -1
vntRecipients.Add objRecipients.Item(i).Address
Debug.Print "Recipient"
Debug.Print objRecipients.Item(i).Address
Next
Afterwards I have all addresses in vntRecipients. When I look at the log file I see, that it's not always an email address but very often (all internal email addresses, but also external email addresses) something like this:
/O=EXCHANGELABS/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDXYXYXYXYXYXYXY)/CN=RECIPIENTS/CN=BYXXYXYXYXYXYXYXYXYXYXYXYXYX0B-S.BLALBA
There are different formats, but I guess it's due to the O365 Exchange server.
Is there any way to get the email address to compare the domain to my trusted domain list?
I haven't tested but the docs probably suggest using the following:
Dim pa As Outlook.PropertyAccessor
Const PR_SMTP_ADDRESS As String = "https://schemas.microsoft.com/mapi/proptag/0x39FE001E"
Set pa = objRecipients.Item(i).PropertyAccessor
Debug.Print objRecipients.Item(i).name & " SMTP=" & pa.GetProperty(PR_SMTP_ADDRESS)
Source: https://learn.microsoft.com/en-us/office/vba/outlook/concepts/address-book/obtain-the-e-mail-address-of-a-recipient
We have an Outlook VSTO Add-In that is checking the Outlook.Recipients domain to see if the addressee is going external to our network
Dim objRecipients As Outlook.Recipients
So say there are 2 recipients that we want to loop through and check the domain:
Do While objRecipients.Count >= loopCount
If InStr(1, objRecipients(loopCount).Address, "#") Then
...
End If
Loop +=1
Loop
For an external objRecipient(loopCount).Address we're seeing, say:
gmail.com
Which is good.
However, for internal addresses, we seem to be seeing LDAP/Exchange addresses instead of SMTP addresses like this:
/O=EXCHANGELABS/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHF23SPDLT)/CN=RECIPIENTS/CN=9asd9asd9d9d9asd9asd9asd9asd9asd9asd-Fred Smith"
This seemed to just start happening, but not sure why or how to fix this problem.
Any idea of why this occurs?
Thanks
Following this article helped us fix our issue:
https://learn.microsoft.com/en-us/office/vba/outlook/Concepts/Address-Book/obtain-the-e-mail-address-of-a-recipient
We needed to use the Outlook.PropertyAccessor property to get the correct SMTP Address:
Dim objRecipients As Outlook.Recipients
Dim pa As Outlook.PropertyAccessor
Const PR_SMTP_ADDRESS As String =
"http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
Do While objRecipients.Count >= loopCount
'set propertyaccessor object
pa = objRecipients(loopCount).PropertyAccessor
'set propertyaccessor domain based on smtp address
paDomain = pa.GetProperty(PR_SMTP_ADDRESS)
Loop +=1
Loop
I am having trouble detecting an in-firm (EX type) invalid email address of an outlook recipient using the following code:
The invalid email address has a typographical error say, tes#mail.com for test#mail.com.
How do I detect an in-firm recipient having an invalid email address?
Dim Email as Outlook.Mailitem
Dim Recipients As Outlook.Recipients
Set Recipients = Email.Recipients
Recipients.Resolveall
If Not Recipients.ResolveAll Then
For i = Recipients.Count To 1 Step -1
If Not Recipients(i).Resolved Then
MsgBox Recipients(i).Name
End If
Next i
End if
Have you checked the following code:
Public Function ResolveDisplayNameToSMTP(sFromName, OLApp As Object) As String
Dim oRecip As Object 'Outlook.Recipient
Set oRecip = OLApp.Session.CreateRecipient(sFromName)
oRecip.Resolve
oRecipName = oRecip.Name
If oRecip.Resolved And InStr(oRecipName, "#") = 0 Then
ResolveDisplayNameToSMTP = "Valid"
Else
ResolveDisplayNameToSMTP = "Not Valid"
End If
End Function
For more, please see this: VBA CODE to Verify Email Address Found in Outlook Global Address List
I have a script that runs under Application_ItemSend in Outlook 2010.
It checks the recipient address and if it isn't one of our own domains it will prompt a confirmation message asking if you want to send the email externally.
The complete code for this can be found here:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim recips As Outlook.Recipients
Dim recip As Outlook.Recipient
Dim pa As Outlook.PropertyAccessor
Dim prompt As String
Dim strMsg As String
Const PR_SMTP_ADDRESS As String = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
Set recips = Item.Recipients
For Each recip In recips
Set pa = recip.PropertyAccessor
If InStr(LCase(pa.GetProperty(PR_SMTP_ADDRESS)), "#ourdomain1.com.au") = 0 And InStr(LCase(pa.GetProperty(PR_SMTP_ADDRESS)), "#ourdomain2.com.au") = 0 And InStr(LCase(pa.GetProperty(PR_SMTP_ADDRESS)), "#ourdomain3.com.au") = 0 And InStr(LCase(pa.GetProperty(PR_SMTP_ADDRESS)), "#ourdomain4.com.au") = 0 Then
strMsg = strMsg & " " & pa.GetProperty(PR_SMTP_ADDRESS) & vbNewLine
End If
Next
For Each recip In recips
Set pa = recip.PropertyAccessor
If InStr(LCase(pa.GetProperty(PR_SMTP_ADDRESS)), "#ourdomain1.com.au") = 0 And InStr(LCase(pa.GetProperty(PR_SMTP_ADDRESS)), "#ourdomain2.com.au") = 0 And InStr(LCase(pa.GetProperty(PR_SMTP_ADDRESS)), "#ourdomain3.com.au") = 0 And InStr(LCase(pa.GetProperty(PR_SMTP_ADDRESS)), "#ourdomain4.com.au") = 0 Then
prompt = "This email will be sent outside of ourdomains.com.au to:" & vbNewLine & strMsg & "Do you want to proceed?"
If MsgBox(prompt, vbYesNo + vbExclamation + vbMsgBoxSetForeground, "Check Address") = vbNo Then
Cancel = True
Exit Sub
Else
Exit Sub
End If
End If
Next
End Sub
This works great, except it has started throwing an error when sending to some distribution lists. Hitting 'end' to the error pop-up the email is still sent.
"The Property "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
is unknown or cannot be found.
From what I have google'd, this is because there isn't always a MIME property present so it cannot always be resolved to an SMTP address.
How can I go about changing this so it will not throw the error?
That property may or may not work depending on whether or not the recipient is an Exchange user within your Exchange organization, and whether or not the cached mode is enabled with Exchange.
PR_SMTP_ADDRESS is not available in cached mode. You would use PR_EMS_AB_PROXY_ADDRESSES in cached mode, which is a PT_MV_STRING8 or PT_MV_UNICODE (string array) property.
Finally, you may find the HowTo: Convert Exchange-based email address into SMTP email address article helpful.
PR_SMTP_ADDRESS is not guaranteed to be present. Also if you send to an SMTP recipient, the property will not be present, but PR_EMAIL_ADDRESSS property (exposed by the Recipient.Address property) will contain a regular SMTP address.
Check if PR_ADDRTYPE is "SMTP", and read PR_EMAIL_ADDRESSS. Otherwise try (and handle the error appropriately) to read the PR_SMTP_ADDRESS property.
Goal: Silently (no dialog box) delete one or more email addresses from recipient collection on ItemSend.
Seems I need to remove recipients by name or index. How do I obtain the index of the email or match it to an SMTP address.
I found some code at the bottom of this message that obtains the SMTP address, but can't find code that I need to start with which is to simply remove a recipient. Then looping it all and matching to an SMTP address seems daunting.
What I'd like to happen
Sub DoNotEmailTheseAddresses()
Dim Msg As Outlook.MailItem
Set Msg = Outlook.CreateItem(olMailItem)
With Msg
run through IndexNumber to find recipients in To:, CC:, and BCC: collection
If RecipientIndexNumber.SMTPaddress = "somebody#adomain.com"
RecipientIndexNumber.SMTPaddress.Remove
If RecipientIndexNumber.SMTPaddress = "somebodyELSEtoo#adomain.com"
RecipientIndexNumber.SMTPaddress.Remove
End With
End Sub
....and off goes the email with somebody#adomain.com and somebodyELSEtoo#adomain.com handily removed.
Getting the corresponding SMTP address code is on MSDN site. http://msdn.microsoft.com/en-us/library/office/ff866259(v=office.15).aspx
Sub GetSMTPAddressForRecipients(mail As Outlook.MailItem)
Dim recips As Outlook.Recipients
Dim recip As Outlook.Recipient
Dim pa As Outlook.PropertyAccessor
Const PR_SMTP_ADDRESS As String = _
"http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
Set recips = mail.Recipients
For Each recip In recips
Set pa = recip.PropertyAccessor
Debug.Print recip.name & " SMTP=" _
& pa.GetProperty(PR_SMTP_ADDRESS)
Next
End Sub
you are not that far away....
For each mail-address you want to remove, you have to go through all recipients and check if they have this mail-address. If yes, remove them. therefor I have put this into a separate function, which you can call for each mail-address you want to remove.
Public Sub newItem_Open(Cancel As Boolean)
Call removerecipi(newItem, "somebody#adomain.com")
End Sub
Function removerecipi(mailitem As Outlook.mailitem, rec_mail As String)
Dim i As Long
restart:
For i = 1 To mailitem.Recipients.Count
If i > mailitem.Recipients.Count Then GoTo restart
If mailitem.Recipients(i).SmtpAddress = rec_mail Then mailitem.Recipients.Remove i
Next i
End Function
why is the restart necessary: after removing one recipient, the mailItem.Recipients.Count needs to be reset. There are also other Solutions, however this one works fine.