|
Examples
NOTE: it is assumed that the
Application variable in the samples below is
initialized to point to an instance of the Outlook's Application object. If you
write a COM add-in, you can grab Application from the OnConnection() callback.
If you are creating a standalone app, one of the ways to initialize Outlook is
as follows:
set Application =
CreateObject("Outlook.Application")
set Namespace =
Application.GetNamespace("MAPI")
Namespace.Logon
Send email bypassing the
security prompt. Note that (see FAQ) if you are using
this code with Outlook 2002, it will work flawlessly only if you run it under an
Exchange Server, otherwise the message will stay in the Outbox until you click
"Send/Receive" in Outlook.
dim
SafeItem, oItem
set SafeItem = CreateObject("Redemption.SafeMailItem")
'Create an instance of Redemption.SafeMailItem
set oItem = Application.CreateItem(0)
'Create a new message
SafeItem.Item = oItem
'set Item property
SafeItem.Recipients.Add "somebody@somewhere.com"
SafeItem.Recipients.ResolveAll
SafeItem.Subject = "Testing Redemption"
SafeItem.Send
|
Access blocked property
(Email1Address in this example) that if used directly in Outlook with the
Security patch applied would cause a warning to be displayed. Accessing this
property using Outlook Redemption bypasses the Security Patch.
dim
SafeContact, oContact
set SafeContact = CreateObject("Redemption.SafeContactItem")
'Create an instance of Redemption.SafeContactItem
set oContact =
Application.Session.GetDefaultFolder(10).Items(1)
'Get a contact item from Outlook, can be any other
contact
SafeContact.Item = oContact
'set Item property of a SafeContact to an Outlook
contact item
MsgBox SafeContact.Email1Address
'access Email1Address property from SafeContact, no
warnings are displayed
|
Import function. Import an MSG
file. The example below creates a new mail item in the Drafts folder and imports
an existing MSG file into it:
dim
sItem, oItem
set sItem = CreateObject("Redemption.SafeMailItem")
set oItem = Application.Session.GetDefaultFolder(16).Items.Add(6)
sItem.Item = oItem
sItem.Import "c:\temp\test.msg", 3 'olMSG,
olRFC822 and olTNEF formats are supported
sItem.Save
|
Note the same can be achieved using the
RDO family of objects, which does not rely on
Outlook at all.
set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = Application.Session.MAPIOBJECT
set
Msg = Session.GetDefaultFolder(16).Items.Add(6)
Msg.Import "c:\temp\test.msg", 3 'olMSG,
olRFC822 and olTNEF formats are supported
Msg.Save
|
Fields array. Display sender
e-mail address (PR_SENDER_EMAIL_ADDRESS Extended MAPI property). Note that
Outlook does not expose this property through its object model. There are dozens
more properties that might be of interest to you: PR_TRANSPORT_MESSAGE_HEADERS
on e-mail items, PR_SEND_RICH_INFO on Recipient objects, etc. Use MdbView or
OutlookSpy to see
which properties are available.
dim
sItem, oItem
set sItem = CreateObject("Redemption.SafeMailItem")
set oItem = Application.Session.GetDefaultFolder(6).Items(1) 'get
first e-mail from the Inbox, can be any other item
sItem.Item = oItem
PrSenderEmail = &H0C1F001E
MsgBox sItem.Fields(PrSenderEmail)
|
Another example on how to use the
Fields array. The example shows
how to add an extra RFC822 header to a message to be sent. It is assumed that
MailItem variable points to an existing Outlook MailItem object. When adding
your own headers, the name (second argument to getIDsfromNames) can be
pretty-much anything you want, but make sure that the GUID is exactly the same
as given below - Outlook only adds named properties with that GUID to the
outgoing message headers.
set
sItem = CreateObject("Redemption.SafeMailItem")
sItem.Item = MailItem
tag = sItem.GetIDsFromNames("{00020386-0000-0000-C000-000000000046}",
"x-test-header")
tag = tag or &H1E 'the type is PT_STRING8
sItem.Fields(Tag) = "test value"
sItem.Subject = sItem.Subject 'to trick
Outlook into thinking that something has changed
sItem.Save
|
SafeCurrentUser object. Use
this object to find out the name or e-mail address of the current Outlook user
dim
CU
set CU = CreateObject("Redemption.SafeCurrentUser")
MsgBox CU.Address
CU.Cleanup 'do call cleanup, otherwise
Outlook might have trouble properly closing down
set CU = Nothing
|
CopyTo method. Note that the
code below saves the new item, gets its EntryID, sets all the references to it
(including the Redemption object) to Nothing, then reopens it. This is necessary
since Outlook remembers the old values of some properties, such as whether the
message was sent.
set
oItem = Application.Session.GetDefaultFolder(6).Items(16)
set sItem = CreateObject("Redemption.SafeMailItem")
sItem.Item = oItem
set NewItem = Application.CreateItem(0)
sItem.CopyTo(NewItem)
'must save to have EntryID
NewItem.Save
EID = NewItem.EntryID
'de-reference the old item, then open it
again
'otherwise Outlook might still have tidbits
'left from the original message
set NewItem = Nothing
set sItem = Nothing
set NewItem = Application.Session.GetItemFromID(EID)
NewItem.Display
|
Forcing Outlook to send a message in
plain text format. The same
technique can be used to send in Rich Text Format.
Without going into the details of the one-off recipient entry id format (which
is documented on MSDN), one-off recipient entry ids must have byte #22 (21 zero
based) set to 1. The code first checks if the entry id is indeed one-off, then
sets byte #22 to 1 for all recipients in a message (MailItem is assumed to be
set to an Outlook Object Model message).
Function IsOneOffEntryID(EntryID)
'check the first 20 bytes
OOEID =
Array(0,0,0,0,&H81,&H2B,&H1F,&HA4,&HBE,&HA3,&H10,&H19,&H9D,&H6E,0,&HDD,&H01,&H0F,&H54,&H02)
IsOneOffEntryID = true
for i = 0 to 19
if EntryID(i) <> OOEID(i) Then
IsOneOffEntryID = false
Exit Function
End If
Next
End Function
PR_ENTRYID = &HFFF0102
PR_SEND_RICH_INFO = &H3A40000B
set sItem = CreateObject("Redemption.SafeMailItem")
sItem.Item = MailItem
For i = 1 to sItem.Recipients.Count
entryBytes = sItem.Recipients.Item(i).Fields(PR_ENTRYID)
If IsOneOffEntryID(entryBytes) Then
entryBytes(22) = 1
sItem.Recipients.Item(i).Fields(PR_ENTRYID) = entryBytes
sItem.Recipients.Item(i).Fields(PR_SEND_RICH_INFO) = false
MailItem.Subject = MailItem.Subject ' just
to make Outlook think something has changed
MailItem.Save
End If
Next
|
Create an HTML message
with an embedded image.
The code is adopted from the code posted on
Slipstick.Com
set
DraftsFolder = Application.Session.GetDefaultFolder(16)
set MailItem = DraftsFolder.Items.Add
set sItem = CreateObject("Redemption.SafeMailItem")
sItem.Item = MailItem
'tell Outlook to hide the paperclip icon
'this is a named prop, so we must call GetIDsFromNames() first!
PT_BOOLEAN = 11
PR_HIDE_ATTACH = sItem.GetIDsFromNames("{00062008-0000-0000-C000-000000000046}",
&H8514) or PT_BOOLEAN
sItem.Fields(PR_HIDE_ATTACH) = true
'add attachment
set Attach = sItem.Attachments.Add("c:\temp\test.jpg")
'content type
Attach.Fields(&H370E001E) = "image/jpeg"
'Attachment cid
Attach.Fields(&H3712001E) = "myident"
sItem.HTMLBody = "<b>test image:</b><IMG align=baseline border=0 hspace=0
src=cid:myident>"
sItem.Save
'IMPORTANT - dereference everything
set Attach = Nothing
set sItem = Nothing
set MailItem = Nothing
set DraftsFolder = Nothing
|
Use MAPIFolder
object to access the hidden items collection to store custom configuration
options
set MAPIFolder =
Application.Session.GetDefaultFolder(6)
set sFolder = CreateObject("Redemption.MAPIFolder")
sFolder.Item = MAPIFolder
set Items = sFolder.HiddenItems
'search by subject
set HiddenMsg = Items.Item("My
config message")
If (HiddenMsg is Nothing) Then
set HiddenMsg = Items.Add("IPM.Note.MyProduct.Config")
'set the subject so we can find
this message next time
HiddenMsg.Subject = "My config
message"
End If
'just for the fun of it, store our
custom data
'in a named prop. We can just as
well use any fixed tag prop
' (e.g. &H6900001E) since we are
the only ones using this message
'and there shouldn't be any
conflicts
PT_STRING8 = &H001E
PR_MYSTRING_PROP =
HiddenMsg.GetIDsFromNames("{FFF40745-D92F-4C11-9E14-92701F001EB3}", "TestProp")
or PT_STRING8
PropValue = HiddenMsg.Fields(PR_MYSTRING_PROP)
MsgBox "Old Value: " & PropValue
HiddenMsg.Fields(PR_MYSTRING_PROP)
= "New Value"
HiddenMsg.Save
|
Using GetIDsFromNames
method to access the Email1Address property of a Contact. You can of course
access this property using SafeContactItem.Email1Address, but there some named
properties which are not exposed through the Outlook Object Model. To find out
what the GUIDs and IDs are, use
OutlookSpy (click IMessage button). For the discussion of the named
properties, see the Extended MAPI properties,
especially the section on named properties.
'The code assumes that
OutlookContact points to an initialized ContactItem object retrieved
from Outlook Object Model
sContact = CreateObject("Redemption.SafeContactItem")
sContact.Item = OutlookContact
PT_STRING8 = &H001E
PR_EMAIL1_ADDRESS =
sContact.GetIDsFromNames("{00062004-0000-0000-C000-000000000046}",
&H8083)
PR_EMAIL1_ADDRESS =
PR_EMAIL1_ADDRESS or PT_STRING8 'type must
be set!!!
MsgBox sContact.Fields(PR_EMAIL1_ADDRESS)
|
|