none
Email id not getting from Redemption.SafeMailItem RRS feed

  • Question

  • Hi Friends,

    From the below code i'm trying to get the email using Redemption.SafeMailItem. And also i'm not familiar with Redemption dll. The code is done by another guy and i'm just modifying the code.

    I 'll explain what actually is doing. I've a CRM. It have contacts with email id. If we click on that email, it will open the new email window from outlook and show the email in "TO" textbox. When user click a button i need to get that email id. But i'm getting the error given below. When i click again that button everything working fine.

    public static string GetSenderEmailAddress(Outlook.MailItem mailItem)
    		{
    			try
    			{
                    MessageBox.Show("Mail ID : " + mailItem.To);
    				int PR_EMAIL = 972947486;
    				int PR_TYPE = 203292702;
    				string senderEmailAddress = string.Empty;
    				Redemption.SafeMailItem safeMailItem = new Redemption.SafeMailItem();
    				safeMailItem.Item = mailItem;
                    string type = string.Empty;
                    try
                    {
                        type = safeMailItem.get_Fields(PR_TYPE).ToString();
     
                    }
                    catch (System.Exception ex) { MessageBox.Show(ex.Message); }
                    if (string.IsNullOrEmpty(type))
                    {
    
                        return senderEmailAddress;
                    }
    
     				if(type == "SMTP")
    				{
    					GlobalConstants.Log(log, "Debug", "Debug: Getting SMTP address", null);
    					try
    					{
    						senderEmailAddress = safeMailItem.Sender.Address;
    					}
    					catch(System.Exception ex)
    					{
    						GlobalConstants.Log(log, "Error", "Getting SMTP address", ex);
    					}
    				}
    				else if(type == "EX")
    				{
    					GlobalConstants.Log(log, "Debug", "Getting Exchange Address", null);
                        try
                        {
                            senderEmailAddress = safeMailItem.Sender.get_Fields(PR_EMAIL).ToString();
                        }
                        catch (System.Exception ex)
                        {
                            GlobalConstants.Log(log, "Error", "Getting Exchange Address", ex);
                        }
    				}
    
    				return senderEmailAddress;
    			}
    			catch(System.Exception ex)
    			{
    				GlobalConstants.Log(log, "Error", "GetSenderEmailAddress", ex);
                    return mailItem.SenderName;
    			}
    
    		}

    This code is showing the email id correctly.

    MessageBox.Show("Mail ID : " + mailItem.To);

    But when the compiler try to execute this code

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

    It giving an error message "Cannot perform runtime binding on a null reference"

    Thanks

    Bobbin

    Thursday, January 23, 2014 12:57 PM

All replies

  • Hello Bobbin,

    Most probably there is no such property defined for the mail item object. 

    Why do you need to use Redemption? Could you please describe your final goal?

    Note, you can use the PropertyAccessor object which comes from the Outlook Object Model. It provides the ability to create, get, set, and delete properties on objects (to get and set item-level properties that are not explicitly exposed in the Outlook object model).


    • Edited by Eugene Astafiev Thursday, January 23, 2014 1:07 PM link is added
    Thursday, January 23, 2014 1:02 PM
  • Hi Eugene 

    Actually the code is already done and published. I dont have much permission to change the code. Only need to fix the bug.

    Thursday, January 23, 2014 1:06 PM
  • Hi Bobbin,

    Change the following line

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

    to

    string type = string.Empty;
    object v = safeMailItem.get_Fields(PR_TYPE);
    
    if (v != null) 
       type = v.ToString();
    Please take a look at the similar forum thread.

    Thursday, January 23, 2014 1:10 PM
  • Hi Eugene

    I applied this code, but first time "V" is always getting null.

    Thursday, January 23, 2014 2:08 PM
  • Hi Bobbin, Redemtion is a commercial component. That I why I'd recommend contacting Redemtion developer at http://www.dimastr.com/redemption/contact.htm .
    Thursday, January 23, 2014 2:24 PM
  • Is PR_SENDER_ADDRTYPE property present for that particular message?

    Where does it come from?


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

    Thursday, January 23, 2014 2:59 PM
  • Hi Dmitry

    1. How can i know "PR_SENDER_ADDRTYPE" property is present for that message. 

    2. Where does it come from?

        I have a CRM (Actually that's a web page listing contacts with email ids). when i click on a email id, it open the new email window and the corresponding email id shows on the "TO" textbox.

    Thursday, January 23, 2014 4:21 PM
  • You can see that property in OutlookSpy: select the message, click IMessage button on the OutlookSpy ribbon.

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

    Thursday, January 23, 2014 4:59 PM
  • Hi Dmitry

    PR_SENDER_ADDRTYPE property is not present for that message. Below i'm attaching the screen short of imessage window

    Friday, January 24, 2014 6:30 AM
  • So this is expected that you get back null.

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

    Friday, January 24, 2014 6:36 AM
  • But why i'm getting value on 2nd click. 
    Friday, January 24, 2014 11:32 AM
  • Hi Bobbin,

    Did you try to call the Save method of the MailItem class before getting the value? Does it help?

    Friday, January 24, 2014 12:15 PM
  • What is the value on the second try? Where does the message come from? Was it sent or received?


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

    Friday, January 24, 2014 1:28 PM
  • Hi

    Sorry to delay the reply. Let me explain my requirement fully.  

    I have a CRM (done in php) and have a outlook plugin. Plugin is always connected with this CRM. If the client click on any email id in CRM (Eg : Email-id from contact module), it will open in outlook new window. 

    If the above client is installed our plugin, In the email new shows our button (Send and Archive). Click on that button, I want to display the email id from "TO box" to my form.

    Now my problems is if client click first time, I dont get the email id. I'm getting this error "Cannot perform runtime binding on a null reference". when i click again, I can get the email id.

    Here my doubt is why its getting first time and not in second time....?

    Thanks

    Bobbin

    Monday, February 10, 2014 1:48 PM
  • Hi Bobbin,

    Outlook items don't have the EntryID property set until you call the Save method. Here is what MSDN states:

    A MAPI store provider assigns a unique ID string when an item is created in its store. Therefore, the EntryID property is not set for an Outlook item until it is saved or sent. The Entry ID changes when an item is moved into another store, for example, from your Inbox to a Microsoft Exchange Server public folder, or from one Personal Folders (.pst) file to another .pst file. Solutions should not depend on the EntryID property to be unique unless items will not be moved.

    Is this the case? Is your code based on the EntryID property to identify Outlook items uniquely?

    Monday, February 10, 2014 2:10 PM
  • So which lines causes the "Cannot perform runtime binding on a null reference". exception?


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

    Monday, February 10, 2014 2:13 PM
  • This line

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

    Monday, February 10, 2014 2:36 PM
  • Again, you must check that you get back a valid object rather than null.

    If you cannot see the property in Outlook, it does not exist, and it only makes sense that null is returned

    object prop = safeMailItem.get_Fields(PR_TYPE);
    if (prop != null)
    {
      type = prop.ToString();
    }


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

    Monday, February 10, 2014 2:46 PM
  • I tried the above code. You are right, the error is gone. But my problem is still there. I mean the first click on the button variable "prop" always giving null value. My question is why safeMailItem.get_Fields(PR_TYPE) not giving the value on first time?
    Monday, February 10, 2014 5:01 PM
  • What value do you get on the second try?

    What changes between first and second tries?

    If you do not see the property in OutlookSpy, how come you can retrieve on the second try?

    Are you sure you are working with the same message?


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

    Monday, February 10, 2014 5:06 PM