Home > database >  Grab actual sender email instead of just the Name
Grab actual sender email instead of just the Name

Time:01-31

I have looked through multiple posts on this issue and for some reason i just cant get my code to work.

I have code that opens a specific email, grabs the reply to email address, closes that email, then creates a new email with the To as the sender email of the previous email.

Sometimes the address only comes over as the name and not the full [email protected].

Please help, i am sure it is something simple. 'Filename' below is the email that is on my share drive. Address is the variable where i want to store all of the email addresses, the 'Reply All'.

Set OutApp = GetObject(, "Outlook.Application")
        Set outEmail = OutApp.Session.OpenSharedItem(Filename)
        With outEmail.ReplyAll
        .display
        Address = .to
        .Close olDiscard
        Set outEmail = Nothing
        End With

CodePudding user response:

Use the Recipients property instead of the string To field which may contain the name of the sender, not the actual email address. Use Recipients(index), where index is the name or index number, to return a single Recipient object. The name can be a string representing the display name, the alias, or the full SMTP email address of the recipient.

The Recipient.Type returns or sets a long representing the type of recipient. For the MailItem a recipient can have one of the following OlMailRecipientType constants: olBCC, olCC, olOriginator, or olTo. So, iteraring over all recipients and checking the Type property value you can find the To recipients.

Finally, you can use all properties of the Recipient object to get the email address such as Address or just set up the new email with the same object by using the Recipients.Add method which creates a new recipient in the Recipients collection. It accepts a string which represents the name of the recipient representing the display name, the alias, or the full SMTP email address of the recipient.

CodePudding user response:

There is no reason to create / display / close a reply just to figure out who the message sender is. Use MailItem.SenderEmailAddress or MailItem.Sender.GetExchangeUser() (in case of an Exchange address):

Set outEmail = OutApp.Session.OpenSharedItem(Filename)
if outEmail.SenderEmailType = "EX" Then
  Address = outEmail.Sender.GetExchangeUser.PrimarySMTPAddress
Else
  Address = outEmail.SenderEmailAddress 
End If
  • Related