none
Get sender email in email composition on Office add-in RRS feed

  • Question

  • Hello,

    I build a Office add-in for Outllook.

    I would like to get sender email like recipient in composition.

    For get recipient i use this function : item.to.getAsync. However, "from" attribute is empty.

    Thanks


    Sunday, January 28, 2018 8:57 PM

All replies

  • Hi Bastien33,

    I suggest you to refer example below may help you to solve your issue.

    var item;
    
    Office.initialize = function () {
        item = Office.context.mailbox.item;
        // Checks for the DOM to load using the jQuery ready function.
        $(document).ready(function () {
            // After the DOM is loaded, app-specific code can run.
            // Get all the recipients of the composed item.
            getAllRecipients();
        });
    }
    
    // Get the email addresses of all the recipients of the composed item.
    function getAllRecipients() {
        // Local objects to point to recipients of either
        // the appointment or message that is being composed.
        // bccRecipients applies to only messages, not appointments.
        var toRecipients, ccRecipients, bccRecipients;
        // Verify if the composed item is an appointment or message.
        if (item.itemType == Office.MailboxEnums.ItemType.Appointment) {
            toRecipients = item.requiredAttendees;
            ccRecipients = item.optionalAttendees;
        }
        else {
            toRecipients = item.to;
            ccRecipients = item.cc;
            bccRecipients = item.bcc;
        }
    
        // Use asynchronous method getAsync to get each type of recipients
        // of the composed item. Each time, this example passes an anonymous 
        // callback function that doesn't take any parameters.
        toRecipients.getAsync(function (asyncResult) {
            if (asyncResult.status == Office.AsyncResultStatus.Failed){
                write(asyncResult.error.message);
            }
            else {
                // Async call to get to-recipients of the item completed.
                // Display the email addresses of the to-recipients. 
                write ('To-recipients of the item:');
                displayAddresses(asyncResult);
            }    
        }); // End getAsync for to-recipients.
    
        // Get any cc-recipients.
        ccRecipients.getAsync(function (asyncResult) {
            if (asyncResult.status == Office.AsyncResultStatus.Failed){
                write(asyncResult.error.message);
            }
            else {
                // Async call to get cc-recipients of the item completed.
                // Display the email addresses of the cc-recipients.
                write ('Cc-recipients of the item:');
                displayAddresses(asyncResult);
            }
        }); // End getAsync for cc-recipients.
    
        // If the item has the bcc field, i.e., item is message,
        // get any bcc-recipients.
        if (bccRecipients) {
            bccRecipients.getAsync(function (asyncResult) {
            if (asyncResult.status == Office.AsyncResultStatus.Failed){
                write(asyncResult.error.message);
            }
            else {
                // Async call to get bcc-recipients of the item completed.
                // Display the email addresses of the bcc-recipients.
                write ('Bcc-recipients of the item:');
                displayAddresses(asyncResult);
            }
    
            }); // End getAsync for bcc-recipients.
         }
    }
    
    // Recipients are in an array of EmailAddressDetails
    // objects passed in asyncResult.value.
    function displayAddresses (asyncResult) {
        for (var i=0; i<asyncResult.value.length; i++)
            write (asyncResult.value[i].emailAddress);
    }
    
    // Writes to a div with id='message' on the page.
    function write(message){
        document.getElementById('message').innerText += message; 
    }

    Reference:

    Get, set, or add recipients when composing an appointment or message in Outlook

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 29, 2018 8:29 AM
    Moderator
  • Hello,

    Thanks. It's only for "Recipients". Not for "Sender" ?

    "From" attribut is only filled when email is read ?

    Thanks

    Monday, January 29, 2018 1:18 PM
  • I just want the email user account to use our add-in.
    Monday, January 29, 2018 1:19 PM
  • Hi Bastien,

    >>I just want the email user account to use our add-in.

    If you want to access the from property of the message, I suggest you use Office.context.mailbox. userProfile.

    Here is a simple code.

      function getUserSender() {
          showNotification("Sender", Office.context.mailbox.userProfile.emailAddress);
      }
    

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, February 22, 2018 2:21 AM