Get The Outlook Sender address

J

James

Hi there, I'm using c# to retrieve mail messages from outlook. However, i
have a problem of getting the sender's e-mail address. At present i can get
the sender's name but not e-mail address.
below is the code i'm using. can somebody please explain how to get the
sender's address?

Thanks

James

private void button1_Click(object sender, System.EventArgs e)

{

Outlook.Application oApp = new Outlook.Application();

// Get NameSpace and Logon.

Outlook.NameSpace oNS = oApp.GetNamespace("mapi");

oNS.Logon("Outlook", Missing.Value, false, true);

// Get the first contact from the Contacts folder.

Outlook.MAPIFolder cContacts =
oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

Outlook.Items oItems = cContacts.Items;

Outlook.MailItem oCt;

try

{

for(int i = 1; i <= oItems.Count; i++)

{

oCt = (Outlook.MailItem)oItems.Item(i);

//oCt = (Outlook.MailItem)oItems.GetFirst();

// Display some common properties.

ListViewItem tmp = new ListViewItem(oCt.SenderName);

tmp.SubItems.Add(oCt.Subject);


this.listView1.Items.Add(tmp);

}

}

catch(Exception ex)

{

Console.WriteLine(ex.Message);

}

finally

{

//Display

//oCt.Display(True)

//Log off.

oNS.Logoff();

//Clean up.

oApp = null;

oNS = null;

oItems = null;

oCt = null;

}

}
 
N

Nicole Calinoiu

James,

Have you tried using the MailItem's SenderEmailAddress property rather than
its SenderName property? The user will probably be prompted to allow the
property use (see the Outlook VBA help), but this is the appropriate
property for examining the sender address.

HTH,
Nicole
 
J

James

Hi there Nicole,
The MailItem's SenderEmailAddress property is only use when working with
outlook2003. However, I'm using outlook2002.
Do you have any suggestions?

Thanks

James
 
J

Jay B. Harlow [MVP - Outlook]

James,
In addition to Nicole's comments.

Here is a list of articles for using Outlook from .NET (includes both C# &
VB.NET).

http://www.microeye.com/resources/res_outlookvsnet.htm

//Clean up.

oApp = null;
oNS = null;
oItems = null;
oCt = null;

Rather then setting the variables to null, which only sets the variables not
null. You should call
System.Runtime.InteropServices.Marshal.ReleaseCOMObject (in a loop) on each
COM variable to ensure the COM reference is cleaned up.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top