+1 vote

How can I retrieve the FROM address ?

This code doesn't work:

string s = eMail.From;

The From property is an IList. How can I get only the FROM sender ?

Thanks

by (960 points)
Could you please be more specific? What is the problem? Are you sending or receiving/parsing emails?

1 Answer

+1 vote
 
Best answer

As you noticed IMail.From is a list, it's not a string. It can't be, as a message may by sent by several people, each having a name and an email address.

Use forech loop over From collection:

foreach (MailBox m in email.From)
{
    string address = m.Address;
    string name = m.Name;
}

In case if From header contains more than one entry, Sender header should be present. It should contain information about the actual sender.

You can use IMail.Sender to get it:

    string senderAddress = email.Sender.Address;
    string senderName = email.Sender.Name;

If Sender header is not there, Mail.dll gets the first entry from the From collection.

You can also get the raw header using IMail.Headers collection:

string header = email.Document.Root.Headers["From"];
by (297k points)
selected by
...