none
Reading field "From" in a new email (not yet sent) RRS feed

  • Question

  • Hi to all,

    I need to read the current value select by the user (or automatically by outlook) for the field "From" in a new email (not yet sent). When the user presses send I will check whether that email falls into a specific category and then validate whether he's sending from a specific email account as he should.

    I tried to read the following property when user presses Send button, so, before the email is actually sent:

    MailItem mail = Application.ActiveInspector().CurrentItem as MailItem;
    
    bool senderOk = (mail.SenderName.ToUpper() == "ACCOUNT DISPLAY NAME");

    The problem is that this property is not maintained in a not-yet-sent email, therefore I don't know where to get this information from. I tried all variables available in the propertyAccessor object, but I couldn't find it.

    I believe it must be somewhere, not sure where.

    I also tried to save the email as .MSG to the disk and read it from there, but again, same problem, the property is not maintained in a not-yet-sent email.

    Any ideas would be very much appreciated.

    Thank you in advance.
    Givago.

     


    Givago

    Friday, February 28, 2014 8:11 AM

Answers

  • You could only use an existing Account.

    If you have SendAs permissions with Exchange on a delegate or other mailbox you can use SentOnBehalfOfName and when sent the item would appear as if it was sent from that mailbox.

    If you don't have SendAs permissions it would appears as "Sent on Behalf Of".


    Ken Slovak MVP - Outlook

    • Marked as answer by Givago Tuesday, March 4, 2014 10:06 AM
    Monday, March 3, 2014 2:54 PM

All replies

  • Hello Givago,

    The following properties of the MailItem class may help to identify the sender:

    Sender - an AddressEntry object that corresponds to the user of the account from which the MailItem is sent.

    SenderEmailAddress - a String that represents the e-mail address of the sender of the Outlook  item.

    SenderEmailType -  a String that represents the type of entry for the e-mail address of the sender of the Outlook item, such as 'SMTP' for Internet address, 'EX' for a Microsoft Exchange server address, etc.

    SenderName - a String indicating the display name of the sender for the Outlook item.

    SentOnBehalfOfName - a String indicating the display name for the intended sender of the mail message.

    SendUsingAccount - an Account object that represents the account under which the MailItem is to be sent.

    If the latter property is not set, the NameSpace.CurrentUser property returns the display name of the currently logged-on user as a Recipient object, it will be the sender.

    Sub DisplayCurrentUser()  
     Dim myNamespace As Outlook.NameSpace  
     Set myNameSpace = Application.GetNameSpace("MAPI")  
     MsgBox myNameSpace.CurrentUser  
    End Sub
    

    Friday, February 28, 2014 1:50 PM
  • Many of the Sender* properties aren't set until after the item is actually sent. One trick you can use is that if an alternate email address is being used the SendUsingAccount property isn't null, if the default address is used NameSpace.CurrentUser.Address will give you From.

    Ken Slovak MVP - Outlook

    Friday, February 28, 2014 3:10 PM
  • That's exactly the last sentence from my reply.
    Friday, February 28, 2014 3:20 PM
  • Thank you for your input Ken.

    For me, in both situations (changing or not the From field) it has my own account.

    What I could do is setting the sender to the one I want, so instead of checking whether the user did what he was supposed to do, I just do it for him:

    mail.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x00410102", mail.PropertyAccessor.StringToBinary(Recip.AddressEntry.ID));

    Cheers!



    Givago

    Friday, February 28, 2014 3:34 PM
  • Hi Eugene,

    Thank you very much for your input.

    Unfortunately these properties are not updated before emails is sent.

    Cheers!


    Givago

    Friday, February 28, 2014 3:36 PM
  • You need to check the SendUsingAccount property. If it is not set, then use the  NameSpace.CurrentUser property.
    Friday, February 28, 2014 3:52 PM
  • If you have alternate accounts then setting the From should result in the SendUsingAccount property being updated.

    If you want to set that property in code you can do so, you don't have to set PR_SENT_REPRESENTING_ENTRYID.


    Ken Slovak MVP - Outlook

    Friday, February 28, 2014 5:37 PM
  • And the only relevant part for items not yet sent.

    Ken Slovak MVP - Outlook

    Friday, February 28, 2014 5:38 PM
  • Hi,

    But how do I create a new instance of microsoft.interop.outlook.account?

    I have only one account in Application.Session.Accounts, and that's my default one, not the one I want to use in SendUsingAccount property.

    Thanks!


    Givago

    Monday, March 3, 2014 8:12 AM
  • You could only use an existing Account.

    If you have SendAs permissions with Exchange on a delegate or other mailbox you can use SentOnBehalfOfName and when sent the item would appear as if it was sent from that mailbox.

    If you don't have SendAs permissions it would appears as "Sent on Behalf Of".


    Ken Slovak MVP - Outlook

    • Marked as answer by Givago Tuesday, March 4, 2014 10:06 AM
    Monday, March 3, 2014 2:54 PM
  • Hi Ken,

    Thank you for your answer. I am using SentOnBehalfOfName now to set the mailbox and it works perfectly.

    Cheers!


    Givago

    Tuesday, March 4, 2014 10:06 AM