我正在使用加载项快速开发Outlook加载项。当我收到一个发件箱邮件项目时,我可以在“mailItem.SenderEmailAddress”下看到我的电子邮件地址,如x500格式。有没有办法将其转换为SMTP电子邮件地址。
这是我的代码:
Outlook.Explorer expl = null;
Outlook.Selection selection = null;
Outlook.MailItem mailItem = null;
Outlook.Recipient recipient = null;
string recipientsEmail = "";
try
{
expl = Globals.ObjOutlook.ActiveExplorer() as Outlook.Explorer; ;
if (expl != null)
{
selection = expl.Selection;
if (selection.Count == 1)
{
if (selection[1] is Outlook.MailItem)
{
mailItem = (selection[1] as Outlook.MailItem);
if (mailItem != null)
{
string senderEmailAddress = mailItem.SenderEmailAddress;
我尝试过并成功: - 我知道如何使用Outlook.Recipient对象将x500类型转换为SMTP。在那里,我可以获得收件人的“addressEntry”并获取ExhangeUser,然后转到ExhangeUser的“PrimarySmtpAddress”。但我不确定如何处理SenderEmailAddress并将其转换为SMTP。
一些有趣的实验: - 由于“Sender”的属性在当前的mailitem对象中不可用,我设法使用反射来获取“Sender”的属性。但是当我运行以下代码时,“propertyInfo”对象值变为null。我不明白为什么。
这是代码
//...
if (mailItem != null)
{
var mailItem2 = GetNewObject(mailItem, "Sender", intArray);
//...
public static object GetNewObject(Outlook.MailItem o, string popertyName, object[] parameters)
{
try
{
PropertyInfo propertyInfo = o.GetType().GetProperty(popertyName); // This object is getting null
return propertyInfo.GetType().GetProperty(popertyName).GetValue(o,parameters) as Outlook.MailItem;
}
catch (MissingMethodException ex)
{
// discard or do something
Debug.DebugMessage(2, "AddinModule : Error in GetNewObject() : " + ex.Message);
return null;
}
}
请指教。谢谢。
答案 0 :(得分:1)
您可以使用AddressEntry
属性获取发件人的Sender
,如下所示:
Outlook.AddressEntry senderAddressEntry = mailItem.Sender;
答案 1 :(得分:1)
在阅读“Dmitry Streblechenko”的评论后,我能够为自己开发出一个完全可行的解决方案。这是代码
private void adxRibBtnAddEmailAddress_OnClick(object sender, IRibbonControl control, bool pressed)
{
Outlook.MailItem mailItem = null;
Outlook.Recipient recipient = null;
string recipientsEmail = "";
string sendersEmail = "";
try
{
mailItem = OutlookHelper.GetSelectedMailItem();
if (mailItem != null)
{
if (mailItem.SenderEmailType == "EX")
sendersEmail = GetSenderEmailAddress(mailItem);
else
sendersEmail = mailItem.SenderEmailAddress;
if (!string.IsNullOrEmpty(sendersEmail))
{
if (sendersEmail == Globals.OLCurrentUserEmail) // Sent mail
{
recipient = mailItem.Recipients[1];
if (recipient != null)
{
recipientsEmail = OutlookHelper.GetAddress(ref recipient);
if (!string.IsNullOrEmpty(recipientsEmail))
{
// Do Something
}
}
}
else // inbox mail
{
// Do Something
}
}
}
}
catch (Exception ex)
{
Debug.DebugMessage(2, "AddinModule : Error in adxRibBtnsddemailaddress_OnClick() : " + ex.Message);
}
finally
{
if (recipient != null) Marshal.ReleaseComObject(recipient);
if (mailItem != null) Marshal.ReleaseComObject(mailItem);
Cursor.Current = Cursors.Default;
}
}
private string GetSenderEmailAddress(Outlook.MailItem oM)
{
Outlook.PropertyAccessor oPA = null;
Outlook.AddressEntry oSender = null;
Outlook.ExchangeUser oExUser = null;
string SenderID;
string senderEmailAddress;
try
{
// 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;
return senderEmailAddress;
}
catch (Exception ex)
{
Debug.DebugMessage(2, "AddinModule : Error in adxRibBtnAddGenInteraction_OnClick() : " + ex.Message);
return null;
}
finally
{
if (oExUser != null) Marshal.ReleaseComObject(oExUser);
if (oSender != null) Marshal.ReleaseComObject(oSender);
if (oPA != null) Marshal.ReleaseComObject(oPA);
}
}