COM addin outlook 2003



hello everybody,

Iam using the below code to check whether the subject is empty when sending
mail in VB.NET office outlook add-in.
Private Sub Application_ItemSend(ByVal Item As Object, ByRef Cancel As
Boolean) Handles Application.ItemSend
Dim sendmessage As Integer
If Item.class = Outlook.OlObjectClass.olMail Then
If Item.subject = "" Then
sendmessage = MessageBox.Show("subject missing", "new",
MessageBoxButtons.OKCancel, MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign, False)

'MsgBox("subject text", MsgBoxStyle.DefaultButton1, "subject
If sendmessage = vbNo Then
Cancel = True
End If
End If

End If
End Sub

but even when i send the empty mail without subject then it is not showing
the message box.could anyone help me ?

|Thank you



Ken Slovak - [MVP - Outlook]

So, is that event handler being called? If so have you run in debug mode and
stepped the code to see what's happening?


Respected ken slovak,

Actaully i tested with a message box. it display the message box when the
event is called.Can u help me, there is a another event called
Private Sub Application_NewMail(ByVal Item As Object, ByRef Cancel As
Boolean) Handles Application.Newmail
which fires when a new mail is in this event how can i use the
mailmessage object and read the internet header of that mail and show it
message box.

Thank you for your previous answer.

Ken Slovak - [MVP - Outlook]

Well, on the first problem I'd run in the debugger and step that event
handler and see why you can't get the item subject. It shouldn't be empty
unless there really is no subject.

For NewMail, which is not really the best event to use since it tends to
miss incoming items, Item is the item that came in. Test Item.Class to make
sure it is a mail item, then cast Item to MailItem. From there you can only
get the Internet headers by using something other than the Outlook object
model for Outlook 2003. PR_TRANSPORT_MESSAGE_HEADERS is not exposed to the
Outlook object model at all. In Outlook 2007 you can use
item.PropertyAccessor to get at that property, but not in earlier versions.

So, you must use an alternate API to get the headers. Neither CDO 1.21 or
Extended MAPI are supported for use in managed code and Extended MAPI is C++
or Delphi only anyway. I personally use a COM wrapper around MAPI for things
like that, Redemption ( For Redemption or other
API you would use property tag 0x007D001E to get

As to NewMail, it fires only at intervals and tends to miss things
sometimes. Better are NewMailEx or an ItemAdd event handler for the Items
collection of the Inbox. However, any MAPI event handler will miss the event
if more than 16 items come in at once. NewMailEx is best at handling that
case but even that misses things sometimes.


Respected ken slovak,

ok thank you for your time and reply. i will use the newmailex. since iam
new to addin programming i dont know what is that com wrapper around mapi and
also iam doing this in with visual studio 2005 with VSTO for office
outlook 2003. so could be please help to how to write code in NewMailEx to
display the internet header in messagebox for the newmail received.

It will be great if u suggest me a code example or code to do it.

Thank you for the reply

Ken Slovak - [MVP - Outlook]

As I said, CDO and Extended MAPI aren't supported for use in managed code.
Some people use them but things fail at odd times, usually at a client site
after the code is deployed, and no one can help you. The main problem is the
memory model used by MAPI, which is not compatible with the memory model
used by the COM Interop.

For headers using CDO code you can look at the code sample at The CDO code not
only is not supported but it will fire the Outlook security prompt warning.

For a Redemption code sample of getting the headers take a look at the
sample on the page.

There are lots of other samples of getting the headers, I googled on
"Internet headers code sample" and turned up lots of examples, including
some in managed code. However those mostly use CDO so are unsupported and
fire the security prompts.




Respected Ken slovak,

Thank you so much for your help.anyway i will try this and let you know.


Hello slovak,

iam using your code to read the internet header code in newmailEx.

Private Sub Application_NewMailEx(ByVal EntryIDCollection As String) Handles
'MessageBox.Show("hello", "come on", MessageBoxButtons.OKCancel,
MessageBoxIcon.Information, MessageBoxDefaultButton.Button1,
MessageBoxOptions.RightAlign, False)
Dim objOutlook As Outlook.Application
Dim objItem As Outlook.MailItem
Dim objCDO As MAPI.Session
Dim objMessage As MAPI.Message
Dim objFields As MAPI.Fields
Dim strID As String
On Error Resume Next

' Instantiate an Outlook Application object.
objOutlook = CreateObject("Outlook.Application")

'Find the current email item and get its EntryID
objItem = objOutlook.ActiveInspector.CurrentItem
strID = objItem.EntryID

'Then set up a CDO Session using a piggy-back login
objCDO = CreateObject("MAPI.Session")
objCDO.Logon("", "", False, False)

'Now get the item as a CDO Message
objMessage = objCDO.GetMessage(strID)

'Now get the headers from the message
objFields = objMessage.Fields
Dim InternetHeaders As String
InternetHeaders =
'Now that the headers are captured in a string you can do whatever
you want with them
MessageBox.Show(InternetHeaders, "testing", MessageBoxButtons.OK,
MessageBoxIcon.Information, MessageBoxDefaultButton.Button1,
MessageBoxOptions.RightAlign, False)

objFields = Nothing
objMessage = Nothing
objCDO = Nothing
objItem = Nothing
objOutlook = Nothing

End Sub

when i declare the const CdoPR_TRANSPORT_MESSAGE_HEADERS = &H7D001E it
shows me the warning the const without as, then i changed as the above. Then
when i put a breakpoint in the program and check the value at this point

InternetHeaders = objFields.Item(CdoPR_TRANSPORT_MESSAGE_HEADERS).Value

the value is different and iam getting null value in the InternetHeaders

One more doubt is it possible to display the header when i click the newmail
bcos this one display when i click the send/receive and when a newmail is
arrived. is there any event that i can use to display the header when i click
the email in the inbox.

Thank you so much for your previous help and time.



Ken Slovak - [MVP - Outlook]

I think you took that code sample and didn't really consider what it was
doing. If you look at the line referencing ActiveInspector, that refers to
the currently active open item. That would not be relevant when handling

You aren't using the EntryIDCollection you are passed in NewMailEx() at all,
that is a string value of EntryID's with each id separated from the next one
by a comma. So you have to take that string, split it into the component
EntryID's and use those to retrieve each item that came in using
NameSpace.GetItemFromID() or by using objCDO.GetMessage() with each EntryID
you parsed.

In addition, there are a few things you should know about that CDO 1.21

First, as I mentioned you will get the security prompts when accessing
PR_TRANSPORT_MESSAGE_HEADERS since that can be used to harvest email
addresses and so is restricted.

Second, CDO 1.21 is not supported for use in managed code, as I also
mentioned. If you do have problems all anyone can say is "CDO is not
supported for use in managed code, why did you use it?"

When you click an email in the folder view you are changing
ActiveExplorer.Selection. You can read the Selection collection and iterate
it to see what's selected. The event that fires when the selection is
changed is the Explorer.SelectionChange() event. But that still won't change
the fact that CDO is not supported for use with managed code.

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