Menachem
Menachem

Reputation: 297

Getting new email senders address

I composed a new email. Before sending this email, I would like (using VBA) to get the senders email address.

I wrote the following example code. When I run this code, the first message box displays the email subject correctly, however the second message box shows nothing (the message box is empty).

Sub email_test()

    Dim eSubject As String
    Dim eSender As String

    eSubject = Application.ActiveInspector.currentItem.subject
    MsgBox eSubject

    eSubject = Application.ActiveInspector.currentItem.SenderEmailAddress
    MsgBox eSender

End Sub

Upvotes: 0

Views: 268

Answers (2)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66215

Use MailItem.SendUsingAccount.SmtpAddress. If MailItem.SendUsingAccount is null, you can asssume the default account will be used - the address can be accessed from Application.Session.CurrentUser.Address. In case of an Exchange mailbox, use Application.Session.CurrentUser.AdderssEntry.GetExchangeUser.PrimarySmtpAddress

Upvotes: 1

Eugene Astafiev
Eugene Astafiev

Reputation: 49397

A new mail item doesn't have the Sender* related properties set. They are set right after the message is processed by the transport provider and can be get, for example, from the Sent Items folder. You can handle the ItemAdd event of the Items class which comes from the Sent Items folder. Be aware, the SaveSentMessageFolder property of the MailItem class can be used to set a Folder object that represents the folder in which a copy of the e-mail message will be saved after being sent. Also the DeleteAfterSubmit property can be set, in that case a copy of the mail message is not saved after being sent.

You may be interested in the SendUsingAccount property which allows to get or set set an Account object that represents the account under which the MailItem is to be sent.

Upvotes: 1

Related Questions