Obtaining SMTP headers

H

Howard Kaikow

I modified the code in MSFT KB article 194870 to choose all messages. I give
the code below.
I placed the code in a class and added a macro to call the code.

However, this causes two messages to appear:

1. AA mesage to select the Profile to be used. This occurs once.
2. The message caused by Outlook SEcurity that could be bypassed if I were
using the EntryID. This occurs for each message.

How can I avoid these messages?

Dim oSession As MAPI.Session
Dim oFolder As Folder
Dim oMsgColl As Messages
Dim oMessage As Message

' Logon to the MAPI session
Set oSession = New MAPI.Session
oSession.Logon

' Get the Inbox folder and its message collection.
Set oFolder = oSession.GetDefaultFolder(CdoDefaultFolderInbox)
Set oMsgColl = oFolder.Messages

' Search through the messages in the Inbox for the Internet
' message. Then use the CdoPR_TRANSPORT_MESSAGE_HEADERS
' (&H7D001E) property tag to retrieve the Internet header.
' If the property doesn't exist(Not a Internet message) you will
' receive a MAPI_E_NOT_FOUND error.

For Each oMessage In oMsgColl
' My modification is the removal of the If and the addition of the On Error
On Error Resume Next
MsgBox oMessage.Fields(&H7D001E) 'Display the header
Err.Clear
Next

' Logoff and cleanup
oSession.Logoff
Set oSession = Nothing
Set oMessage = Nothing
Set oMsgColl = Nothing
Set oFolder = Nothing
 
D

Dan Mitchell

Howard Kaikow said:
1. AA mesage to select the Profile to be used. This occurs once.

That's because you're calling oSession.Logon and not passing in the name
of a profile to use. See the docs for more, but what you probably want to
do is set newSession to 'false' (rather than the default of true) so it'll
use your existing Outlook session.
2. The message caused by Outlook SEcurity that could be bypassed if I
were using the EntryID. This occurs for each message.

See http://www.outlookcode.com/d/sec.htm

-- dan
 
H

Howard Kaikow

Dan Mitchell said:
That's because you're calling oSession.Logon and not passing in the name
of a profile to use. See the docs for more, but what you probably want to
do is set newSession to 'false' (rather than the default of true) so it'll
use your existing Outlook session.

Thanx.
I just figured that one out.

I've seen that.

I am using code in a Run As Script with EntryID.
My problem is modifying that to grab the headers.
 
H

Howard Kaikow

Thanx.

I am currently using the code below in a rule.

Is there some way to use olMail to access the headers and avoid the security
warnings?

Public Sub ProcessRulesList(objMsg As Outlook.MailItem)
Dim intFile As Integer
Dim objFields As MAPI.Fields
Dim objItems As Outlook.ItemProperties
Dim objItem As Outlook.ItemProperty
Dim objMessage As MAPI.Message
Dim olNS As Outlook.NameSpace
Dim olMail As Outlook.MailItem
Dim oSession As MAPI.Session
Dim strID As String
Dim strInternetHeaders As String
Dim strTemp As String

intFile = FreeFile
Open strFilterFileDirectory & "Incoming.txt" For Output As #intFile

strID = objMsg.EntryID
Set olNS = Application.GetNamespace("MAPI")
Set olMail = olNS.GetItemFromID(strID)

On Error Resume Next
Set oSession = CreateObject("MAPI.Session")
oSession.MAPIOBJECT = olNS.MAPIOBJECT
' oSession.Logon "", "", False, False

Set objMessage = oSession.GetMessage(strID)

'Get the headers from the message
Set objFields = objMessage.Fields
strInternetHeaders =
objFields.Item(CdoPR_TRANSPORT_MESSAGE_HEADERS).Value
MsgBox strInternetHeaders

oSession.Logoff
On Error GoTo 0

With olMail
strSenderEmailAddress = .SenderEmailAddress
strSenderName = .SenderName
strSubject = .Subject
strTo = .To
strCc = .CC
lngImportance = .Importance
strBcc = .BCC
strMailBody = .Body
Print #intFile, "Incoming:"; vbCrLf _
& "SenderEmailAddress:" & .SenderEmailAddress & vbCrLf _
& "SenderName:" & .SenderName & vbCrLf _
& "Subject:" & .Subject & vbCrLf _
& "To:" & .To & vbCrLf _
& "Cc:" & .CC & vbCrLf _
& "Importance:" & .Importance & vbCrLf _
& "Bcc:" & .BCC & vbCrLf _
& "Body: " & vbCrLf _
& .Body

On Error Resume Next
For Each objItem In .ItemProperties
strTemp = ""
With objItem
strTemp = strTemp & "(1):" & .Name & vbCrLf
strTemp = strTemp & "(2):" & .Type & vbCrLf
strTemp = strTemp & "(3):" & .Value & vbCrLf
End With
Print #intFile, strTemp
Next objItem
On Error GoTo 0
End With

Set objFields = Nothing
Set objMessage = Nothing
Set oSession = Nothing
Set olMail = Nothing
Set olNS = Nothing

Close #intFile
End Sub
 
S

Sue Mosher [MVP-Outlook]

No, you'd have to use Redemption. CDO is *always* subject to the security prompts, and Outlook objects don't expose the headers.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
H

Howard Kaikow

No, you'd have to use Redemption. CDO is *always* subject to the security
prompts, and Outlook objects don't expose the headers.

Is CDO subject to the security messages if I use a VB 6 COM add-in?
 
H

Howard Kaikow

Howard Kaikow said:
No, you'd have to use Redemption. CDO is *always* subject to the security
prompts, and Outlook objects don't expose the headers.

Is CDO subject to the security messages if I use a VB 6 COM add-in?

http://support.microsoft.com/default.aspx?scid=kb;en-us;327657&Product=ol2002
states that CDO Is not trusted via COM.

Like why does MessySoft impose such restrictions?
Having access to the full headers facilitates better filtering for SPAM.
 
S

Sue Mosher [MVP-Outlook]

It might very well work. I know there is sample code for using SendKeys. (I'm not sure it still works in the latest versions.)

The best solution, of course, is to avoid having any prompts in the first place.
--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
H

Howard Kaikow

Sue Mosher said:
It might very well work. I know there is sample code for using SendKeys.
(I'm not sure it still works in the latest >versions.)

Where is that sample code?

I've tried using both SendKeys to no avail.
It seems that the security message is modal.

If it is indeed modal, then I cannot get the handle for it's window and
process with SendMessage.
 

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