简体   繁体   English

获取电子邮件地址而不是名称?

[英]Getting an email address instead of a name?

When I am trying to printout the different properties in a MailItem , I am seeing some behavior that I don't understand. 当我尝试打印出MailItem的不同属性时,我看到了一些我不理解的行为。 Instead of email addresses, I see names. 我看到的是电子邮件地址,而不是电子邮件地址。

static void ReadMail()
{
     Microsoft.Office.Interop.Outlook.Application app = null;
     Microsoft.Office.Interop.Outlook._NameSpace ns = null;
     Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;

     app = new Microsoft.Office.Interop.Outlook.Application();
     ns = app.GetNamespace("MAPI");

     inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);

      for (int counter = 1; counter <= inboxFolder.Items.Count; counter++)
      {
           dynamic item = inboxFolder.Items[counter];
           Console.WriteLine("Sendername: {0}", item.SenderName);
           Console.WriteLine("Sender: {0}", item.Sender);
           Console.WriteLine("To: {0}", item.To);
       }
 }

What I mean is instead of getting "john.doe@email.com", I am getting "John Doe". 我的意思是不是得到“ john.doe@email.com”,而是得到“ John Doe”。 Any particular reason this might be happening? 有什么特殊原因可能会发生这种情况吗? Is there a way to obtain the email address of the sender and the recipients (To, CC, BCC) instead of names? 有没有一种方法可以获取发件人和收件人的电子邮件地址(“收件人”,“抄送”,“密件抄送”)而不是姓名?

Instead of using the To / CC / BCC properties, loop through all recipients in the MailItem.Recipients collection and read the Recipient.Address property. 而不使用To / CC / BCC属性,而是遍历MailItem.Recipients集合中的所有收件人并读取Recipient.Address属性。 You might also want to use the Recipient.Type (olTo / olCC / OlBCC) property. 您可能还需要使用Recipient.Type(olTo / olCC / OlBCC)属性。

foreach (Outlook.Recipient recip in item.Recipients)
{
   if (recip.Type == (int)OlMailRecipientType.olTo)
   {
      Console.WriteLine(string.Format("Name: {0}, address: {1})", recip.Name, recip.Address));
   } 
}

Then you should use item.SenderEmailAddress instead of item.SenderName . 然后,您应该使用item.SenderEmailAddress而不是item.SenderName

Also you can iterate collection item.Recipients to determine Sender/TO/CC/BCC addreses(type is stored in Type property of each Recipient object of that collection) - it has one of Outlook.OlMailRecipientType enumeration values(olOriginator, olTo, olCC, olBCC). 您也可以迭代收集项。 item.Recipients以确定发件人/ TO / CC / BCC地址(类型存储在该集合的每个收件人对象的Type属性中)-它具有Outlook.OlMailRecipientType枚举值之一(olOriginator,olTo,olCC, olBCC)。

Since I can't comment the answer, 由于我无法评论答案,

You need to cast one of the if member because otherwise it will says that it can not compare an int type with an OlMailRecipientType and the compilation will fail. 您需要OlMailRecipientType if成员之一,因为否则它会说它无法将int类型与OlMailRecipientType进行比较,并且编译将失败。

here I cast the OlMailRecipientType.OlTo to an int 在这里,我将OlMailRecipientType.OlTo转换为int

if (Recip.Type == (int)OlMailRecipientType.olTo)

foreach (Outlook.Recipient recip in item.Recipients)
{
   if (Recip.Type == (int)OlMailRecipientType.olTo)
   {
      Console.WriteLIne(string.Format("Name: {0}, address: {1)", recip.Name, recip.Address));
   } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM