Makah
Makah

Reputation: 4513

AccessViolationException reading email sender in Outlook 2007

I got a AccessViolationException when i tried to get email address via MailItem.Sender. Address on Outlook 2007 (Windows XP). I don't get this error when i'm running the same code on Outlook 2010 (Windows 7).

Outlook.MailItem email = inbox.Items[i] as Microsoft.Office.Interop.Outlook.MailItem;
Outlook.MailItem email
Console.WriteLine("Subject: " + email.Subject);
Console.WriteLine("Sender: " + email.Sender); <-- Exception Here!
Console.WriteLine("Addr: " + email.Sender.Address);

Console: Error: System.AccessViolationException: Attempted to read or write protected memory. method: get_Sender()

Upvotes: 4

Views: 906

Answers (2)

Paul Sweatte
Paul Sweatte

Reputation: 24617

As noted above:

Yes. I got an workaround: email.SenderEmailAddress

References

Upvotes: 1

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66215

This is an indication that you are using a property not exposed by an earlier version of the interface - MailItem.Sender property was added in Outlook 2010. When you run the code against Outlook 2007, the call goes past the object's v-table, hence the access violation.

The workaround is to read the PR_SENDER_ENTRYID MAPI property (DASL name http://schemas.microsoft.com/mapi/proptag/0x0C190102) using MailItem.PorpertyAccessor.GetProperty and use it to call Namespace.GetAddressEntryFromID.

Upvotes: 2

Related Questions