从Outlook Interop C#中的已发送邮件文件夹中获取目标电子邮件地址

时间:2017-05-23 19:22:31

标签: c# email outlook interop

我正在使用Outlook Interop和C#获取一些邮件信息来创建Excel报告。我有已发送邮件文件夹中的电子邮件项目列表,我想获取我发送邮件的电子邮件地址。我找到了“To”属性但只返回了人名而​​不是地址。

任何人都可以帮我从Outlook.MailItem对象返回电子邮件地址吗? 你可以问我是否需要更多信息。感谢!!!!

这是我设置属性的代码:

    foreach (object mail in mails)
    //mails is a list from Sent Items folder
        {
            if (mail is Outlook.MailItem)
            {
                var item = (Outlook.MailItem)mail;
                //i need the address in provider email
                var providerEmail = someProperty(????);
                var name = item.To;
                var request= "Other Request";
                var emailDate= item.ReceivedTime;
                var status = "Closed";
                var responseDate= item.CreationTime;
                var reportObject = new ReportModel
                {
                    Email = providerEmail ,
                    Name = name,
                    Solicitud = request,
                    EmailDate = emailDate,
                    Status = status,
                    ResponseDate = responseDate
                };
            }
        }

1 个答案:

答案 0 :(得分:1)

MailItem具有“收件人”属性,您可以使用它来获取每种类型的收件人:

  • 抄送
  • 密送。

使用recipient.Type识别收件人类型和recipient.Address以获取其电子邮件地址。 例如:

protected override void getRecepients(MailItem OLitem, StringBuilder toStringBuilder,
        StringBuilder ccStringBuilder, StringBuilder bccStringBuilder)
    {
        try
        {
            var recipients = OLitem.Recipients;
            string parent = string.Empty;
            foreach (Microsoft.Office.Interop.Outlook.Recipient recipient in recipients)
            {
                switch (recipient.Type)
                {
                    case (int)Microsoft.Office.Interop.Outlook.OlMailRecipientType.olTo:
                        toStringBuilder.Append(recipient.Address + ", ");
                        if (parent == string.Empty)
                        {
                            parent = recipient.Address;
                        }
                        break;
                    case (int)Microsoft.Office.Interop.Outlook.OlMailRecipientType.olCC:
                        ccStringBuilder.Append(recipient.Address + ", ");
                        break;
                    case (int)Microsoft.Office.Interop.Outlook.OlMailRecipientType.olBCC:
                        bccStringBuilder.Append(recipient.Address + ", ");
                        break;
                    default:
                        break;
                }
            }
        }
        catch (Exception ex)
        {
            // do something with error
        }
    }