Answered Redemption.SafeMailItem error in outlook

  • Friday, May 04, 2012 4:52 PM
     
      Has Code

    Hi 

    I'm using the below code to get the sender email id.

    public static string GetSenderEmailAddress(Outlook.MailItem mailItem)
    		{
    			try
    			{
     				int PR_EMAIL = 972947486;
    				int PR_TYPE = 203292702;
    				string senderEmailAddress = string.Empty;
    Redemption.SafeMailItem safeMailItem = new Redemption.SafeMailItemClass();
    safeMailItem.Item = mailItem;
    string type = safeMailItem.get_Fields(PR_TYPE).ToString();
    System.Windows.Forms.MessageBox.Show("type : " + type.ToString());
    }
    }

    I'm getting an in this line "string type = safeMailItem.get_Fields(PR_TYPE).ToString()"  

    Error : Object reference not set to an instance of an object. 

    i'm sending the mailItem like this

    Outlook.Inspector inspector = SingleOutlookRef.Instance.OutlookApp.ActiveInspector();
    Outlook.MailItem mail = (Outlook.MailItem)inspector.CurrentItem;
    string email = GetSenderEmailAddress(mail);

    If i send the mail item like this it will work fine

    Outlook.Explorer _explorer = SingleOutlookRef.Instance.OutlookApp.ActiveExplorer();
    
    for (int i = 1; i <= this._explorer.Selection.Count; i++)
                        {
                            if (this._explorer.Selection[i] is Outlook.MailItem)
                            {
                                string email = GetSenderEmailAddress(mail);
                            }
    }

    Thanks

    Bobbin

All Replies

  • Friday, May 04, 2012 6:40 PM
     
     
    

    PR_SENDER_ADDRTYPE_A property is only present on the sent or received messages.

    No MAPI property is guaranteed to be available, you need to test that you get back a valid value before you call ToString().


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!

  • Saturday, May 05, 2012 9:54 AM
     
     

    Hi

    How can i solve my problem here....?

    Thanks

  • Sunday, May 06, 2012 4:44 PM
     
     Answered

    Change the line

    string type = safeMailItem.get_Fields(PR_TYPE).ToString()

    to

    string type = "";

    object v = safeMailItem.get_Fields(PR_TYPE);

    if (v != null) type = v.ToString();


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.3 is now available!