AccessViolationException reading email sender in Outlook 2007

I got an AccessViolationException when trying to get the email address through MailItem.Sender. Address in Outlook 2007 (Windows XP). I do not get this error when I run the same code in 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 ()

+4
source share
2 answers

This indicates that you are using a property that is not affected by an earlier version of the interface. The MailItem.Sender property was added in Outlook 2010. When you run the code in Outlook 2007, the call passes the v-table object, therefore, an access violation.

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

+2
source

As noted above:

Yes. I have a workaround: email.SenderEmailAddress

References

+1
source

Source: https://habr.com/ru/post/1412846/


All Articles