1
votes

i'm using Outlook Interop and C# to get some mail information to create an excel report. I have email items list from Sent Items folder and i want to get the email address who i sent the mail. I found the "To" property but only return the person name not the address.

Anyone can please help me to return the email addres from Outlook.MailItem object? You can ask me if you need more information. Thanks!!!!

Here is my code where i am setting the properties:

    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

1 Answers

1
votes

MailItem has Recipients property, you can use it to obtain recipient of each type:

  • To
  • Cc
  • Bcc.

Use recipient.Type to recognize recipient type and recipient.Address to obtain its email address. Example:

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
        }
    }