none
How to get sender name of signed/encrypted email item?

    Question

  • Hi All,

    I am trying to read sender email address from all the emails in my inbox. I am able to read the email address of all the mail items, except the emails which are signed by the sender.

    Can you please help me how to read the sender email address of the secure email? I am pasting the code I have below.

    for (int count = currentFolder.Items.Count; count > 0; count--)
    {
    	var item = currentFolder.Items[count];
    	
    	MailItem mailItem = item as MailItem;
    	if (mailItem != null)
    	{
    		if (mailItem.MessageClass == "IPM.Note" || mailItem.MessageClass == "IPM.Note.SMIME" || mailItem.MessageClass == "IPM.Note.SMIME.MultipartSigned" || mailItem.MessageClass == "IPM.Note.Rules.OofTemplate.Microsoft")
    		{
    			Debug.Print(string.Format("From:{0}, To: {1}, Subject: '{2}'", GetSMTPEmailAddress(mailItem), mailItem.To, mailItem.Subject));
    			string emailSender = GetSMTPAddress(mailItem);
    			if(emailSender.Contains("@kpn.com"))
    			{
    				mailItem.Move(destinationFolder);
    				break;
    			}
    		}
    		else
    	        {
    			Debug.Print(mailItem.MessageClass + "*********");
    		}
    	}
    	else
    	{
                    // I need to find out for which email item type I will not be able to get the sender email address.
    		Debug.Print(item.GetType().ToString());
    	}
    }
    
    public string GetSMTPAddress(MailItem item)
    {
       ...
    }


    Regards, Utkarsh http://www.utkarshonline.com

    Monday, December 24, 2012 4:05 PM

All replies

  • What is the implementation of the GetSMTPAddress() function?

    What happens when you call it? Do you get an error? What is it?


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

    Monday, December 24, 2012 4:32 PM
  • Hi Dmitry,

    Here it is - I guess I have found this somewhere in internet.

    private string GetSMTPEmailAddress(MailItem item)
            {
                string email = string.Empty;
    
                if (GetOutlookVersion() < 14)
                {
                    if (item.SenderEmailType == "EX")
                    {
                        email = GetSMTPAddress(item);
                    }
                }
                else
                {
                    AddressEntry sender = item.Sender;
                    if (sender.AddressEntryUserType == OlAddressEntryUserType.olExchangeUserAddressEntry)
                    {
                        ExchangeUser exchangeUser = sender.GetExchangeUser();
                        email = exchangeUser.PrimarySmtpAddress;
                    }
                }
                if (string.IsNullOrEmpty(email))
                {
                    email = item.SenderEmailAddress;
                }
                return email;
            }
    
            private string GetSMTPAddress(MailItem item)
            {
                PropertyAccessor outlookPropertyAccessor = item.PropertyAccessor;
                return outlookPropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E") as string;
            }
    
            public int GetOutlookVersion()
            {
                string[] version = Application.Version.Split('.');
                return Convert.ToInt32(version[0]);
            }


    Regards, Utkarsh http://www.utkarshonline.com

    Monday, December 24, 2012 5:07 PM
  • So what happens when you step through that code?

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

    Monday, December 24, 2012 6:43 PM
  • Hi Utakrsh,

    Thanks for posting in the MSDN Forum.

    Would you please could your please clarify your issue more detailed especially Dmitry's question?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, December 27, 2012 1:16 AM
    Moderator