我有一个VSTO Outlook 2007加载项。当新邮件进入收件箱时,我正在尝试获取发件人电子邮件地址。
为此,我使用以下代码:
void inboxItems_ItemAdd(object Item)
{
Outlook.MailItem mailItem = Item as Outlook.MailItem;
if (mailItem != null)
string emailAdress = mailItem.SenderEmailAddress;
}
问题是当电子邮件来自同一个域时,emailAdress包含类似
的LDAP地址/ O =第一个组织/ OU =第一个行政组/ CN =收件人/ CN =管理员
虽然我想获得像
这样的SMTP地址administrator@orgname.com
我的问题是如何从内部域获取电子邮件的SMTP发件人地址?
P. S.
在Outlook 2010中,可以使用Sender属性解决此问题。但它在2007年不受支持。
item.Sender.GetExchangeUser().PrimarySmtpAddress
答案 0 :(得分:15)
在Outlook 2007中,您可以这样做:
private string GetSmtpAddress(Outlook.MailItem oItem)
{
Outlook.Recipient recip;
Outlook.ExchangeUser exUser;
string sAddress;
if (oItem.SenderEmailType.ToLower() == "ex")
{
recip = Globals.ThisAddIn.Application.GetNamespace("MAPI").CreateRecipient(oItem.SenderEmailAddress);
exUser = recip.AddressEntry.GetExchangeUser();
sAddress = exUser.PrimarySmtpAddress;
}
else
{
sAddress = oItem.SenderEmailAddress.Replace("'", "");
}
return sAddress;
}
答案 1 :(得分:1)
我在这里介绍一种方法,可以通过传递电子邮件作为参考来获取发件人的电子邮件地址。它自己决定天气的方法是发件人的电子邮件地址类型是SMTP还是Exchange。如果是Exchange,它会将电子邮件地址转换为SMTP。这是代码。
internal static string GetSenderEmailAddress(ref Outlook.MailItem oM)
{
Outlook.PropertyAccessor oPA = null;
Outlook.AddressEntry oSender = null;
Outlook.ExchangeUser oExUser = null;
string SenderID;
string senderEmailAddress;
try
{
if (oM.SenderEmailAddress.Contains("@") && oM.SenderEmailAddress.Contains(".com")) //Handing smtp email addresses
{
senderEmailAddress = oM.SenderEmailAddress;
}
else //Handing exchange email addresses
{
// Create an instance of PropertyAccessor
oPA = oM.PropertyAccessor;
// Obtain PidTagSenderEntryId and convert to string
SenderID = oPA.BinaryToString(oPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0C190102"));
// Obtain AddressEntry Object of the sender
oSender = Globals.ObjNS.GetAddressEntryFromID(SenderID);
oExUser = oSender.GetExchangeUser();
senderEmailAddress = oExUser.PrimarySmtpAddress;
}
Debug.DebugMessage(3, "OutlookHelper : GetSenderEmailAddress() : Completed");
return senderEmailAddress;
}
catch (Exception ex)
{
MessageBox.Show( ex.Message);
return null;
}
finally
{
if (oExUser != null) Marshal.ReleaseComObject(oExUser);
if (oSender != null) Marshal.ReleaseComObject(oSender);
if (oPA != null) Marshal.ReleaseComObject(oPA);
}
}
答案 2 :(得分:0)
您可以使用检查器获取当前电子邮件,如下所示。
Outlook.Inspector inspector = Globals.ThisAddIn.Application.ActiveInspector();
if (inspector != null)
{
Outlook.MailItem mi = inspector.CurrentItem as Outlook.MailItem;
//Then identify whether email sender is exchange user or normal user
string senderEmail=null;
if (mi.SenderEmailType == "EX")
{
senderEmail = mi.Sender.GetExchangeUser().PrimarySmtpAddress;
}
else
{
senderEmail=mi.SenderEmailAddress;
}
}
您将在senderEmail变量中收到发件人电子邮件。
答案 3 :(得分:0)
email.Sender必须具有比Outlook 2007更高的版本