none
Getting SMTP Address of the Sender of a Mail Item produce exception RRS feed

  • Question

  • I have plugin for Outlook 2016. (VSTO)

    To get the sender's SMTP address, I am following How to: Get the SMTP Address of the Sender of a Mail Item

    Which used to work fine, but recently start break when it goes to below line

    if (sender.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry || sender.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeRemoteUserAddressEntry)

    Message = "The operation failed." StackTrace = "at Microsoft.Office.Interop.Outlook.AddressEntry.get_AddressEntryUserType()\r\n at ....." ErrorCode = -1802485755

    I am not sure how to get around this

    Thanks

    Thursday, December 15, 2016 10:12 AM

All replies

  • Hello,

    Do you have an Exchange account configured in Outlook? If so, do you use the cached mode?

    Where is your complete code? Where did you get the sender object?


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Thursday, December 15, 2016 2:01 PM
  • Hi,

    Yes, we have exchange configured. on workstation we use cached mode, on remote desktop servers we use non-cached mode.

    I am using Application.ItemSend event and capturing Outlook.MailItem from there and using the same method as previous link.

     public static string GetEmailAddress(Outlook.MailItem mail)
            {
          
                string PR_SMTP_ADDRESS = @"http://schemas.microsoft.com/mapi/proptag/0x39FE001E";
    
                try
                {
                    if (mail == null)
                    {
                        throw new ArgumentNullException();
                    }
    
                    if (mail.SenderEmailType == "EX")
                    {
                        Outlook.AddressEntry sender = mail.Sender;
    
                        if (sender != null)
                        {
                            // Now we have an AddressEntry representing the Sender
                            if (sender.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry
                                || sender.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeRemoteUserAddressEntry)
                            {
                                // Use the ExchangeUser object PrimarySMTPAddress
                                Outlook.ExchangeUser exchUser = sender.GetExchangeUser();
    
                                if (exchUser != null)
                                {
                                    return exchUser.PrimarySmtpAddress;
                                }
                                else
                                {
                                    return null;
                                }
                            }
                            else
                            {
                                return sender.PropertyAccessor.GetProperty(PR_SMTP_ADDRESS) as string;
                            }
                        }
                        else
                        {
                            return null;
                        }
                    }
                    else
                    {
                        return mail.SenderEmailAddress;
                    }
                }
                catch (Exception ex)
                {
                    FormManager.ShowExceptionDialog("Error while resolving Outlook address to SMTP address", ex);
                    return null;
                }
            }


    Thursday, December 15, 2016 4:32 PM
  • Hi huMptyduMptys,

    I can see that you are using the same code as mentioned in the MSDN Example and it was working correctly before sometime.

    so I want to confirm with you that did you change anything in your environment that can affect this code?

    also I suggest you to check if any updates are installed.

    sometimes Office and windows related updates made some changes and after that working code start to generate exceptions.

    so please confirm above mentioned things and let us know about that so that we try to suggest you further.

    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.

    Friday, December 16, 2016 4:56 AM
    Moderator
  • Hi Deepak,

    Just want to let you know that this is not something happening always. It started happening continuously for about 30 minutes and got resolved it self. This happened few days

    Thanks 

    Friday, December 16, 2016 12:27 PM
  • Hi huMptyduMptys,

    you had mentioned that the issue is occurred by itself and also get solved by itself.

    I did not met this type of issue before.

    did you notice anything strange when this error occur?

    it can help us to narrow down this issue.

    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, December 19, 2016 8:38 AM
    Moderator
  • Hi Deepak,

    No nothing else seem to act strange apart from this

    Thanks

    Monday, December 19, 2016 11:07 AM
  • Hi huMptyduMptys,

    at this stage we don't have any clue regarding error. so I am not sure what exactly causing this issue.

    you can try to reset everything , try to reinstall the addin and check whether the issue is still there or not.

    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.

    Tuesday, December 20, 2016 3:02 AM
    Moderator