OutlookEX UDF - Mail Item

From AutoIt Wiki
Revision as of 11:41, 3 April 2015 by Rt01 (talk | contribs)
Jump to navigation Jump to search

Here you find detailed information about how to work with Mail items.

Wrapper function

The following function is a "wrapper" that combines multiple function calls to mimic functions available in the "original" Outlook UDF.
This should make your scripts a bit shorter and live a bit easier.

SendMail

The wrapper function _OL_Wrapper_SendMail allows to send a mail to multiple recipients (To, Cc and BCc), add attachments, set some properties and send the mail in one go. The body can either be normal text or html. This wrapper function mimics the function of _OutlookSendMail of the original UDF.

Example:

#include <OutlookEx.au3>
$oOL = _OL_Open()
_OL_Wrapper_SendMail($oOL, "John Doe", "", "", "Testmail", "&lt;b>Test&lt;/b>", "", $olFormatHTML, $olImportanceHigh)
_OL_Close($oOL)

This sends a mail to 'John Doe' with Subject 'Testmail', body as HTML and high importance.

Tips & Tricks

Send a mail on behalf of another user

Set property SentOnBehalfOfName to the displayname of the intended sender.

Example:

#include <OutlookEx.au3>
; Open the connection to Outlook
Global $oOL = _OL_Open()
; Create a mail item and set some properties
Global $oItem = _OL_ItemCreate($oOL, $olMailItem, "*\Outlook-UDF-Test\TargetFolder\Mail", "", _
  "Subject=TestMail", "BodyFormat=" & $olFormatHTML, "HTMLBody=Bodytext in &lt;b>Test&lt;/b>.", "SentOnBehalfOfName=Doe Jane")
; Add a recipient and resolve it
_OL_ItemRecipientAdd($oOL, $oItem, Default, $olTo, "Doe John")
; Send the mail
_OL_ItemSend($oOL, $oItem)
If @extended = -2147352567 Then _
  MsgBox(16, "OutlookEX UDF - Example Script", "You don't have permission to send the mail on behalf of the specified person!")
_OL_Close($oOL)

This creates a mail in your folder Outlook-UDF-Test\TargetFolder\Mail, recipient is John Doe, subject is Testmail, body as HTML. SentOnBehalfOfName is set to the displayname of the sender (Jane Doe). The mail is sent and the extended error code is checked. An error message is being displayed if you do not have the required permissions to send a mail on behalf of another user.

Delay the sending of a mail

Set property DeferredDeliveryTime to date and time when the mail is to be delivered.

Example:

#include <OutlookEx.au3>
; Open the connection to Outlook
Global $oOL = _OL_Open()
; Create a mail item and set some properties
Global $oItem = _OL_ItemCreate($oOL, $olMailItem, "*\Outlook-UDF-Test\TargetFolder\Mail", "", _
  "Subject=TestMail", "BodyFormat=" & $olFormatHTML, "HTMLBody=Bodytext in &lt;b>Test&lt;/b>.", _
  "DeferredDeliveryTime=2017/03/18 18:01:00")
; Add a recipient and resolve it
_OL_ItemRecipientAdd($oOL, $oItem, Default, $olTo, "Doe John")
; Send the mail
_OL_ItemSend($oOL, $oItem)
_OL_Close($oOL)

This creates a mail in your folder Outlook-UDF-Test\TargetFolder\Mail, recipient is John Doe, subject is Testmail, body as HTML. DeferredDeliveryTime is set to 2017/03/18 18:01:00. The mail is sent by Outlook but transmission is deferred by the Exchange server until the specified date/time has been reached.

Send a mail or reply to a mail with a signature

Outlook allows to add a signature when a new mail or a reply to a mail is created. The following steps are needed:

  • Create a signature to be added to new or reply to mails. This can be done manually by using Outlook or by calling function _OL_MailSignatureCreate
  • Set a signature as the one to be used when a new mail or a reply to a mail is created. This can be done manually by using Outlook or by calling function _OL_MailSignatureSet
  • Create the new or reply to mail. The signature is created immediately by Outlook before any other text is added to the body

Problem:
You can't use functions _OL_ItemCreate and _OL_Wrapper_SendMail to set the body of the mail. Because the functions just set the body property and hence the signature will be overwritten.

Solution:
Don't use _OL_Wrapper_SendMail. There is no solution for this function.
Create the mail, retrieve the body (which now only consists of the signature), add the mail text and set the body property again.

Example:

#include <OutlookEX.au3>
Global $oOutlook = _OL_Open()
;===========
; Plain Text
;===========
$oItem = _OL_ItemCreate($oOutlook, $olMailItem, "", "", "Subject=Subject for BodyFormat=Plain Text")
$oItem.BodyFormat = $olFormatPlain
$oItem.GetInspector
$sBody = $oItem.Body
$oItem.Body = "Text: Mail Text" & $sBody
$oItem.Display

;=====
; HTML
;=====
$oItem = _OL_ItemCreate($oOutlook, $olMailItem, "", "", "Subject=Subject for BodyFormat=HTML")
$oItem.BodyFormat = $olFormatHTML
$oItem.GetInspector
$sBody = $oItem.HTMLBody
$oItem.HTMLBody = "HTML: <b>Mail</b> Text" & $sBody
$oItem.Display

;==========
; Rich Text
;==========
$oItem = _OL_ItemCreate($oOutlook, $olMailItem, "", "", "Subject=Subject for BodyFormat=RTF")
$oItem.BodyFormat = $olFormatRichText
$oItem.GetInspector
$sBody = $oItem.HTMLBody
$oItem.HTMLBody = "RTF: <b>Mail</b> Text" & $sBody
$oItem.Display

Send a mail to an SMTP address

If you have problems sending a mail to an SMTP address, means _OL_ItemRecipientAdd returns an error telling you that a recipient could not be resolved, then you should try to directly set the "To" property.

#include <OutlookEx.au3>
; Open the connection to Outlook
Global $oOL = _OL_Open()
; Create a mail item and set some properties
Global $oItem = _OL_ItemCreate($oOL, $olMailItem, "*\Outlook-UDF-Test\TargetFolder\Mail", "", _
  "Subject=TestMail", "BodyFormat=" & $olFormatHTML, "HTMLBody=Bodytext in &lt;b>Test&lt;/b>.", "To=Jane.Doe@company.com")
; Send the mail
_OL_ItemSend($oOL, $oItem)
If @extended = -2147352567 Then _
  MsgBox(16, "OutlookEX UDF - Example Script", "You don't have permission to send the mail on behalf of the specified person!")
_OL_Close($oOL)