展望:如何从收件人字段获取电子邮件?
问题描述:
我试图将电子邮件地址输入到撰写邮件窗口的至字段中。展望:如何从收件人字段获取电子邮件?
我尝试获取收件人的地址属性,根据VS,应该给我的电子邮件。
我不是接收一个字符串,它看起来像这样:
"/c=US/a=att/p=Microsoft/o=Finance/ou=Purchasing/s=Furthur/g=Joe"
我怎样才能在收件人字段中的电子邮件地址?
我迄今为止代码:
List <string> emails = new List<string>();
if (thisMailItem.Recipients.Count > 0)
{
foreach (Recipient rec in thisMailItem.Recipients)
{
emails.Add(rec.Address);
}
}
return emails;
答
你可以试试这个?
emails.Add(rec.AddressEntry.Address);
编辑:
我没有合适的环境来测试,所以我只是猜测这一切,但如何
string email1Address = rec.AddressEntry.GetContact().Email1Address;
或.Email2Adress
或.Email3Address
还有,
rec.AddressEntry.GetExchangeUser().Address
您可能想尝试。
答
AddressEntry
还具有SMTPAddress
属性,该属性公开用户的主要smtp地址。
答
我不知道这是否有助于或如何准确
it is, a sample
private string GetSmtp(Outlook.MailItem item)
{
try
{
if (item == null || item.Recipients == null || item.Recipients[1] == null) return "";
var outlook = new Outlook.Application();
var session = outlook.GetNamespace("MAPI");
session.Logon("", "", false, false);
var entryId = item.Recipients[1].EntryID;
string address = session.GetAddressEntryFromID(entryId).GetExchangeUser().PrimarySmtpAddress;
if (string.IsNullOrEmpty(address))
{
var rec = item.Recipients[1];
var contact = rec.AddressEntry.GetExchangeUser();
if (contact != null)
address = contact.PrimarySmtpAddress;
}
if (string.IsNullOrEmpty(address))
{
var rec = item.Recipients[1];
var contact = rec.AddressEntry.GetContact();
if (contact != null)
address = contact.Email1Address;
}
return address;
}
finally
{
}
}
答
试试这个
private string GetSMTPAddressForRecipients(Recipient recip)
{
const string PR_SMTP_ADDRESS =
"http://schemas.microsoft.com/mapi/proptag/0x39FE001E";
PropertyAccessor pa = recip.PropertyAccessor;
string smtpAddress = pa.GetProperty(PR_SMTP_ADDRESS).ToString();
return smtpAddress;
}
这是MSDN上提供here
我用同样的方式获得的电子邮件地址在我的应用程序和它的工作。
我刚刚试过,rec.Address和rec.AddressEntry.Address返回相同的东西。喵。 – Cat 2011-02-18 03:09:50
还好吗?喵 – 2011-02-18 03:31:08
GetExchangeUser()。PrimarySmtpAddress是答案:) 当有多个“地址”类型注册到用户,地址属性默认为除了电子邮件以外的东西,所以你必须指定哪种类型的地址。谢谢你尝试! – Cat 2011-02-27 22:25:15