> Hi,
>
> I have managed to write some code for accessing the email headers and
> retreiving the sende4rs email address using Outlook. However I can not
> seem
> to understand how to access the same info using Outlook Express. I am
> trying
> to use Simple MAPI and The MAPIRead function. However I do not know how to
> get the correct pointer to the email i need. I have heard Simple MAPI and
> OutLook Express can not access COM objects, is this correct. Does any one
> know a way to directly access an email in the Inbox in Outlook Express, so
> i't sender's email and Headers can be read.
>
> Here is the code for Outlook, and some I have started for SImple MAPI, but
> How is Simple MAPI used to access the _objMailItem I select in the
> following,
>
> The code is properly indented at
>
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=352308&SiteID=1>
> Sub CompareEmails()
>
> Dim _strErrorMessage As String = "An error message."
>
> 'Select the currently highlighted explorer
>
> Dim explorer As Outlook._Explorer = OutlookApp.ActiveExplorer()
>
> 'Exit if there is no explorwe highlighted
>
> If explorer Is Nothing Then
>
> MsgBox(_strErrorMessage)
>
> Exit Sub
>
> End If
>
> 'Get the selected item in the explorer
>
> Dim selObject As Outlook.Selection = explorer.Selection
>
> Dim _objMailItem As Outlook.MailItem = Nothing
>
> 'Try to select the object as a mail item
>
> Try
>
> _objMailItem = CType(selObject.Item(1), Outlook.MailItem)
>
> Catch
>
> _objMailItem = Nothing
>
> End Try
>
> 'If the item was not a mail item then exit
>
> If _objMailItem Is Nothing Then
>
> MsgBox(_strErrorMessage)
>
> Exit Sub
>
> End If
>
> Dim mapiObject As Object = _objMailItem.MAPIOBJECT
>
> If Not mapiObject Is Nothing Then
>
> Dim mailHeader As String = String.Empty
>
> Dim sendersEmailAddress As String = String.Empty
>
> Dim receiversEmailAddress As String = String.Empty
>
> 'Dim senderEmail As String = String.Empty
>
> Dim unk As IntPtr = IntPtr.Zero
>
> Dim unkObj As IntPtr = IntPtr.Zero
>
>
>
>
>
> Try
>
> 'Select the IUnknown interface for the email
>
> unkObj = Marshal.GetIUnknownForObject(mapiObject)
>
> 'Create a GUID to reference the COM class MAPIProp
>
> Dim iMapiProp As Guid = New Guid("00020303-0000-0000-C000-000000000046")
>
> 'Check that the mail message has the MAPIProperty Com Interface
>
> Marshal.QueryInterface(unkObj, iMapiProp, unk)
>
> ' If the property Interface exists access the properties of the email
>
> If Not unk = IntPtr.Zero Then
>
> Dim propValue As MAPI.SPropValue
>
> Dim pPropValue As IntPtr = IntPtr.Zero
>
> Try
>
> 'MAPI.HrGetOneProp(unk, MAPI.PR_TRANSPORT_MESSAGE_HEADERS, pPropValue)
>
> 'propValue = CType(Marshal.PtrToStructure(pPropValue,
> GetType(MAPI.SPropValue)), MAPI.SPropValue)
>
> 'mailHeader = Marshal.PtrToStringAnsi(New IntPtr(propValue.Value))
>
> 'Select the sender email address
>
> MAPI.HrGetOneProp(unk, MAPI.PR_SENDER_EMAIL_ADDRESS, pPropValue)
>
> 'Fill Structure with data from the property
>
> propValue = CType(Marshal.PtrToStructure(pPropValue,
> GetType(MAPI.SPropValue)), MAPI.SPropValue)
>
> 'Convert the data to a string
>
> sendersEmailAddress = Marshal.PtrToStringAnsi(New IntPtr(propValue.Value))
>
> 'Select the receivers email address
>
> MAPI.HrGetOneProp(unk, MAPI.PR_RECEIVED_BY_EMAIL_ADDRESS, pPropValue)
>
> propValue = CType(Marshal.PtrToStructure(pPropValue,
> GetType(MAPI.SPropValue)), MAPI.SPropValue)
>
> receiversEmailAddress = Marshal.PtrToStringAnsi(New
> IntPtr(propValue.Value))
>
> 'Free the memory associated with the Property Object
>
> If Not pPropValue = IntPtr.Zero Then
>
> MAPI.MAPIFreeBuffer(pPropValue)
>
> End If
>
> Catch
>
> MsgBox("Try again latter. If this problem persists: It is possible an
> error
> has occurred in the execution of Extended MAPI (MAPI32.dll) 1. Ensure the
> dll
> file is registered and functioning correctly. 2. For extended MAPI to work
> you must use Corporate or Workgroup (CW) mode not Internet Mail Only
> (IMO)")
>
> End Try
>
> End If
>
> Catch
>
> End Try
>
>
>
> Marshal.Release(unkObj)
>
> If Not (unk = IntPtr.Zero) Then
>
> Marshal.Release(unk)
>
> End If
>
> Marshal.ReleaseComObject(mapiObject)
>
> End If
>
>
>
> End Sub
>
>
>
>
>
> Private Class MAPI
>
> Public Const PR_TRANSPORT_MESSAGE_HEADERS As UInt32 = &H7D001E
>
> 'Public Const PR_BODY As UInt32 = &H1000001E
>
> 'Public Const PR_BODY_HTML As UInt32 = &H1013001E
>
> 'Public Const PR_HTML As UInt32 = &H10130102
>
> 'Public Const PT_STRING8 As UInt32 = 30
>
> 'Public Const PR_SENDER_ADDRTYPE As UInt32 = &HC1E001E
>
> Public Const PR_SENDER_EMAIL_ADDRESS As UInt32 = 203358238
>
> Public Const PR_RECEIVED_BY_EMAIL_ADDRESS = &H76001E
>
> 'Public Const PR_SENDER_NAME As UInt32 = (PT_STRING8 Or (&HC1A << 16))
>
> Public Structure SPropValue
>
> Public ulPropTag As UInt32
>
> Public dwAlignPad As UInt32
>
> Public Value As Long
>
> End Structure
>
>
>
>
> Public Declare Sub HrGetOneProp Lib "mapi32" _
>
> Alias "HrGetOneProp[ at ]12" ( _
>
> ByVal lpMapiProp As IntPtr, _
>
> ByVal ulPropTag As UInt32, _
>
> ByRef lppProp As IntPtr)
>
> Public Declare Sub MAPIFreeBuffer Lib "mapi32" ( _
>
> ByVal lppProp As IntPtr)
>
> 'lpMapiProp = IUnknown
>
> End Class
>
>
> 'Needed for Outlook express (can be removed from this project)
>
> Private Class SimpleMAPI
>
> '**************************************************************************
>
> '
>
> '
>
> '
>
> ' Visual Basic declaration for the MAPI functions.
>
> '
>
> ' This file can be loaded into the global module.
>
> '
>
> '
>
> '
>
> '
>
> '**************************************************************************
>
> '
>
> '***************************************************
>
> ' MAPI Message holds information about a message
>
> '***************************************************
>
> Public Structure MAPIMessage
>
> Public Reserved As Long
>
> Public Subject As String
>
> Public NoteText As String
>
> Public MessageType As String
>
> Public DateReceived As String
>
> Public ConversationID As String
>
> Public Flags As Long
>
> Public RecipCount As Long
>
> Public FileCount As Long
>
> End Structure
>
>
>
> '************************************************
>
> ' MAPIRecip holds information about a message
>
> ' originator or recipient
>
> '************************************************
>
> Public Structure MapiRecip
>
> Public Reserved As Long
>
> Public RecipClass As Long
>
> Public Name As String
>
> Public Address As String
>
> Public EIDSize As Long
>
> Public EntryID As String
>
> End Structure
>
>
>
> '******************************************************
>
> ' MapiFile holds information about file attachments
>
> '******************************************************
>
> Public Structure MapiFile
>
> Public Reserved As Long
>
> Public Flags As Long
>
> Public Position As Long
>
> Public PathName As String
>
> Public FileName As String
>
> Public FileType As String
>
> End Structure
>
>
>
> '***************************
>
> ' FUNCTION Declarations
>
> '***************************
>
> Declare Function MAPILogon Lib "MAPI32.DLL" (ByVal UIParam&, ByVal User$,
> _
>
> ByVal Password$, ByVal Flags&, ByVal Reserved&, ByVal Session&) As Long
>
> Declare Function MAPILogoff Lib "MAPI32.DLL" (ByVal Session&, ByVal _
>
> UIParam&, ByVal Flags&, ByVal Reserved&) As Long
>
> Declare Function BMAPIReadMail Lib "MAPI32.DLL" (ByVal lMsg&, ByVal
> nRecipients&, _
>
> ByVal nFiles&, ByVal Session&, ByVal UIParam&, ByVal MessageID$, ByVal
> Flag&, ByVal _
>
> Reserved&) As Long
>
> Declare Function BMAPIGetReadMail Lib "MAPI32.DLL" (ByVal lMsg&, ByVal
> Message As _
>
> MAPIMessage, ByVal Recip() As MapiRecip, ByVal File() As MapiFile, ByVal
> Originator As _
>
> MapiRecip) As Long
>
> Declare Function MAPIFindNext Lib "MAPI32.DLL" Alias "BMAPIFindNext"
> (ByVal _
>
> Session&, ByVal UIParam&, ByVal MsgType$, ByVal SeedMsgID$, ByVal Flag&,
> ByVal _
>
> Reserved&, ByVal MsgID$) As Long
>
> Declare Function MAPISendDocuments Lib "MAPI32.DLL" (ByVal UIParam&, ByVal
> _
>
> DelimStr$, ByVal FilePaths$, ByVal FileNames$, ByVal Reserved&) As Long
>
> Declare Function MAPIDeleteMail Lib "MAPI32.DLL" (ByVal Session&, ByVal _
>
> UIParam&, ByVal MsgID$, ByVal Flags&, ByVal Reserved&) As Long
>
> Declare Function MAPISendMail Lib "MAPI32.DLL" Alias "BMAPISendMail"
> (ByVal _
>
> Session&, ByVal UIParam&, ByVal Message As MAPIMessage, ByVal Recipient()
> As
> MapiRecip, _
>
> ByVal File() As MapiFile, ByVal Flags&, ByVal Reserved&) As Long
>
> Declare Function MAPISaveMail Lib "MAPI32.DLL" Alias "BMAPISaveMail"
> (ByVal _
>
> Session&, ByVal UIParam&, ByVal Message As MAPIMessage, ByVal Recipient()
> As
> MapiRecip, _
>
> ByVal File() As MapiFile, ByVal Flags&, ByVal Reserved&, ByVal MsgID$) As
> Long
>
> Declare Function BMAPIAddress Lib "MAPI32.DLL" (ByVal lInfo&, ByVal
> Session&, _
>
> ByVal UIParam&, ByVal Caption$, ByVal nEditFields&, ByVal Label$, ByVal
> nRecipients&, ByVal Recip() _
>
> As MapiRecip, ByVal Flags&, ByVal Reserved&) As Long
>
> Declare Function BMAPIGetAddress Lib "MAPI32.DLL" (ByVal lInfo&, ByVal _
>
> nRecipients&, ByVal Recipients() As MapiRecip) As Long
>
> Declare Function MAPIDetails Lib "MAPI32.DLL" Alias "BMAPIDetails" (ByVal
> _
>
> Session&, ByVal UIParam&, ByVal Recipient As MapiRecip, ByVal Flags&,
> ByVal _
>
> Reserved&) As Long
>
> Declare Function MAPIResolveName Lib "MAPI32.DLL" Alias "BMAPIResolveName"
> _
>
> (ByVal Session&, ByVal UIParam&, ByVal UserName$, ByVal Flags&, ByVal _
>
> Reserved&, ByVal Recipient As MapiRecip) As Long
>
>
>
>
>
> '**************************
>
> ' CONSTANT Declarations
>
> '**************************
>
> '
>
> Public Const SUCCESS_SUCCESS = 0
>
> Const MAPI_USER_ABORT = 1
>
> Const MAPI_E_USER_ABORT = MAPI_USER_ABORT
>
> Const MAPI_E_FAILURE = 2
>
> Const MAPI_E_LOGIN_FAILURE = 3
>
> Const MAPI_E_LOGON_FAILURE = MAPI_E_LOGIN_FAILURE
>
> Const MAPI_E_DISK_FULL = 4
>
> Public Const MAPI_E_INSUFFICIENT_MEMORY = 5
>
> Public Const MAPI_E_BLK_TOO_SMALL = 6
>
> Public Const MAPI_E_TOO_MANY_SESSIONS = 8
>
> Public Const MAPI_E_TOO_MANY_FILES = 9
>
> Public Const MAPI_E_TOO_MANY_RECIPIENTS = 10
>
> Public Const MAPI_E_ATTACHMENT_NOT_FOUND = 11
>
> Public Const MAPI_E_ATTACHMENT_OPEN_FAILURE = 12
>
> Public Const MAPI_E_ATTACHMENT_WRITE_FAILURE = 13
>
> Public Const MAPI_E_UNKNOWN_RECIPIENT = 14
>
> Public Const MAPI_E_BAD_RECIPTYPE = 15
>
> Public Const MAPI_E_NO_MESSAGES = 16
>
> Public Const MAPI_E_INVALID_MESSAGE = 17
>
> Public Const MAPI_E_TEXT_TOO_LARGE = 18
>
> Public Const MAPI_E_INVALID_SESSION = 19
>
> Public Const MAPI_E_TYPE_NOT_SUPPORTED = 20
>
> Public Const MAPI_E_AMBIGUOUS_RECIPIENT = 21
>
> Public Const MAPI_E_AMBIG_RECIP = MAPI_E_AMBIGUOUS_RECIPIENT
>
> Public Const MAPI_E_MESSAGE_IN_USE = 22
>
> Public Const MAPI_E_NETWORK_FAILURE = 23
>
> Public Const MAPI_E_INVALID_EDITFIELDS = 24
>
> Public Const MAPI_E_INVALID_RECIPS = 25
>
> Public Const MAPI_E_NOT_SUPPORTED = 26
>
> Public Const MAPI_ORIG = 0
>
> Public Const MAPI_TO = 1
>
> Public Const MAPI_CC = 2
>
> Public Const MAPI_BCC = 3
>
>
>
> '***********************
>
> ' FLAG Declarations
>
> '***********************
>
> '* MAPILogon() flags *
>
> Public Const MAPI_LOGON_UI = &H1
>
> Public Const MAPI_NEW_SESSION = &H2
>
> Public Const MAPI_FORCE_DOWNLOAD = &H1000
>
> '* MAPILogoff() flags *
>
> Public Const MAPI_LOGOFF_SHARED = &H1
>
> Public Const MAPI_LOGOFF_UI = &H2
>
> '* MAPISendMail() flags *
>
> Public Const MAPI_DIALOG = &H8
>
> '* MAPIFindNext() flags *
>
> Public Const MAPI_UNREAD_ONLY = &H20
>
> Public Const MAPI_GUARANTEE_FIFO = &H100
>
> '* MAPIReadMail() flags *
>
> Public Const MAPI_ENVELOPE_ONLY = &H40
>
> Public Const MAPI_PEEK = &H80
>
> Public Const MAPI_BODY_AS_FILE = &H200
>
> Public Const MAPI_SUPPRESS_ATTACH = &H800
>
> '* MAPIDetails() flags *
>
> Public Const MAPI_AB_NOMODIFY = &H400
>
> '* Attachment flags *
>
> Public Const MAPI_OLE = &H1
>
> Public Const MAPI_OLE_STATIC = &H2
>
> '* MapiMessage flags *
>
> Public Const MAPI_UNREAD = &H1
>
> Public Const MAPI_RECEIPT_REQUESTED = &H2
>
> Public Const MAPI_SENT = &H4
>
> Function CopyFiles(ByVal MfIn As MapiFile, ByVal MfOut As MapiFile) As
> Long
>
> MfOut.FileName = MfIn.FileName
>
> MfOut.PathName = MfIn.PathName
>
> MfOut.Reserved = MfIn.Reserved
>
> MfOut.Flags = MfIn.Flags
>
> MfOut.Position = MfIn.Position
>
> MfOut.FileType = MfIn.FileType
>
> CopyFiles = 1&
>
> End Function
>
> Function CopyRecipient(ByVal MrIn As MapiRecip, ByVal MrOut As MapiRecip)
> As
> Long
>
> MrOut.Name = MrIn.Name
>
> MrOut.Address = MrIn.Address
>
> MrOut.EIDSize = MrIn.EIDSize
>
> MrOut.EntryID = MrIn.EntryID
>
> MrOut.Reserved = MrIn.Reserved
>
> MrOut.RecipClass = MrIn.RecipClass
>
> CopyRecipient = 1&
>
> End Function
>
> Public Function MAPIAddress(ByVal Session As Long, ByVal UIParam As Long,
> ByVal Caption As String, _
>
> ByVal nEditFields As Long, ByVal Label As String, ByVal nRecipients As
> Long,
> ByVal Recips() As _
>
> MapiRecip, ByVal Flags As Long, ByVal Reserved As Long) As Long
>
>
>
> Dim Info&
>
> Dim rc&, ignore&
>
> Dim nRecips As Long
>
> Dim Rec(nRecipients) As MapiRecip
>
> ' Use local variable since BMAPIAddress changes the passed value
>
> nRecips = nRecipients
>
> '*****************************************************
>
> ' Copy input recipient structure into local
>
> ' recipient structure used as input to BMAPIAddress
>
> '*****************************************************
>
> For i As Integer = 0 To nRecipients - 1
>
> ignore& = CopyRecipient(Recips(i), Rec(i))
>
> Next i
>
> rc& = BMAPIAddress(Info&, Session&, UIParam&, Caption$, nEditFields&, _
>
> Label$, nRecips&, Rec, Flags, 0&)
>
> If (rc& = SUCCESS_SUCCESS) Then
>
> '**************************************************
>
> ' New recipients are now in the memory referenced
>
> ' by Info (HANDLE). nRecipients is the number of
>
> ' new recipients.
>
> '**************************************************
>
> nRecipients = nRecips ' Copy back to parameter
>
> If (nRecipients > 0) Then
>
> ReDim Rec(0 To nRecipients - 1)
>
> rc& = BMAPIGetAddress(Info&, nRecipients&, Rec)
>
> '*********************************************
>
> ' Copy local recipient structure to
>
> ' recipient structure passed as procedure
>
> ' parameter. This is necessary because
>
> ' VB doesn't seem to work properly when
>
> ' the procedure parameter gets passed
>
> ' directory to the BMAPI.DLL Address routine
>
> '*********************************************
>
> ReDim Recips(0 To nRecipients - 1)
>
> For i As Integer = 0 To nRecipients - 1
>
> ignore& = CopyRecipient(Rec(i), Recips(i))
>
> Next i
>
> End If
>
> End If
>
> MAPIAddress = rc&
>
> End Function
>
> Public Function MAPIReadMail(ByVal Session As Long, ByVal UIParam As Long,
> ByVal MessageID As _
>
> String, ByVal Flags As Long, ByVal Reserved As Long, ByVal Message As
> MAPIMessage, ByVal Orig As _
>
> MapiRecip, ByRef RecipsOut() As MapiRecip, ByRef FilesOut() As MapiFile)
> As
> Long
>
> Dim Info&
>
> Dim nFiles&, nRecips&, rc&, ignore&
>
> rc& = BMAPIReadMail(Info&, nRecips, nFiles, Session, 0, MessageID, _
>
> Flags, Reserved)
>
> If (rc& = SUCCESS_SUCCESS) Then
>
> 'Message is now read into the handles array. We have to redim the
>
> 'arrays and read the information in.
>
> If (nRecips = 0) Then nRecips = 1
>
> If (nFiles = 0) Then nFiles = 1
>
> Dim Recips(nRecips - 1) As MapiRecip
>
> Dim Files(nFiles - 1) As MapiFile
>
> rc& = BMAPIGetReadMail(Info&, Message, Recips, Files, Orig)
>
> '*******************************************
>
> ' Copy Recipient and File structures from
>
> ' Local structures to those passed as
>
> ' parameters
>
> '*******************************************
>
> ReDim FilesOut(nFiles - 1)
>
> ReDim RecipsOut(nRecips - 1)
>
> For i As Integer = 0 To nRecips - 1
>
> ignore& = CopyRecipient(Recips(i), RecipsOut(i))
>
> Next i
>
> For i As Integer = 0 To nFiles - 1
>
> ignore& = CopyFiles(Files(i), FilesOut(i))
>
> Next i
>
> End If
>
> MAPIReadMail = rc&
>
> End Function
>
>
>
> End Class
>
>
>
>