从MS Outlook邮件中读取发件人的电子邮件地址

时间:2013-05-31 09:26:30

标签: c# outlook

我使用以下代码从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;
} 

如何获取发件人的邮件ID。我试过Item.SenderEmailAddress,但它没有给我发件人的电子邮件ID。它给了我这样的东西 -

/O=EXG5/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHF23SPDLT)/CN=RECIPIENTS/CN=TEST35345

2 个答案:

答案 0 :(得分:6)

现在返回的地址是(X.400)Exchange地址。 请查看this MSDN article有关如何检索相应SMTP地址的信息。

答案 1 :(得分:1)

我正在使用此VBA例程来检索SMTP对象的mailItem地址:

(应该可以轻松移植到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

适用于Outlook 2007。如上所述,MSDN solution for Outlook 2010看起来更好一些。