5
votes

I am using below code to read incoming mails from MS Outlook 2010 -

public static void outLookApp_NewMailEx(string EntryIDCollection)
{                
    NameSpace _nameSpace;
    ApplicationClass _app;
    _app = new ApplicationClass();
    _nameSpace = _app.GetNamespace("MAPI");
    object o = _nameSpace.GetItemFromID(EntryIDCollection);
    MailItem Item = (MailItem)o;
    string HTMLbpdyTest = Item.HTMLBody;
    string CreationTime = Convert.ToString(Item.CreationTime);
    string strEmailSenderEmailId = Convert.ToString(Item.SenderEmailAddress);
    string strEmailSenderName = Item.SenderName;
    string Subject = Item.Subject;
} 

How can I get sender's mail id. I tried Item.SenderEmailAddress but its not giving me the sender's email id. It is giving me something like this -

/O=EXG5/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHF23SPDLT)/CN=RECIPIENTS/CN=TEST35345
2
It looks like the sender's address, but not the SMTP address, maybe this will help you - jao
How can I get sender's email id like - [email protected] - user2318170

2 Answers

6
votes

The address returned now is an (X.400) Exchange address. Please take a look at this MSDN article on how to retrieve the corresponding SMTP address.

1
votes

I am using this VBA routine to retrieve the SMTP address of a mailItem object:

(should be easily portable to C#)

Private Function getSmtpMailAddress(sMail As Outlook.mailItem) As String
    Dim strAddress As String
    Dim strEntryId As String
    Dim objRecipient As Outlook.Recipient
    Dim objSession As Outlook.NameSpace
    Dim objAddressentry As Outlook.AddressEntry
    Dim objExchangeUser As Outlook.ExchangeUser
    Dim objReply As Outlook.mailItem

    On Error GoTo ErrHandler

    If sMail.SenderEmailType = "SMTP" Then
        strAddress = sMail.SenderEmailAddress
    Else
        Set objReply = sMail.reply()
        Set objRecipient = objReply.recipients.item(1)

        strEntryId = objRecipient.EntryID

        objReply.Close OlInspectorClose.olDiscard

        Set objSession = getMapiSession

        strEntryId = objRecipient.EntryID

        Set objAddressentry = objSession.GetAddressEntryFromID(strEntryId)
        Set objExchangeUser = objAddressentry.GetExchangeUser()

        strAddress = objExchangeUser.PrimarySmtpAddress()
    End If

    getSmtpMailAddress = strAddress

    Exit Function

ErrHandler:
    Err.Clear
    On Error GoTo 0
    getSmtpMailAddress = "???"
End Function

This works for Outlook 2007. The MSDN solution for Outlook 2010 as pointed out above, looks a bit nicer.