OutlookEX UDF - Mail Item: Difference between revisions

From AutoIt Wiki
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
 
(11 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:OutlookEX_UDF]]
[[Category:UDF]]
[[Category:Outlook]]
Here you find detailed information about how to work with Mail items.  
Here you find detailed information about how to work with Mail items.  
== Wrapper function ==
== Wrapper function ==
Line 12: Line 13:
#include <OutlookEx.au3>
#include <OutlookEx.au3>
$oOL = _OL_Open()
$oOL = _OL_Open()
_OL_Wrapper_SendMail($oOL, "John Doe", "", "", "Testmail", "&lt;b>Test&lt;/b>", "", $olFormatHTML, $olImportanceHigh)
_OL_Wrapper_SendMail($oOL, "John Doe", "", "", "Testmail", "<b>Test</b>", "", $olFormatHTML, $olImportanceHigh)
_OL_Close($oOL)
</syntaxhighlight>
</syntaxhighlight>


Line 29: Line 31:
; Create a mail item and set some properties
; Create a mail item and set some properties
Global $oItem = _OL_ItemCreate($oOL, $olMailItem, "*\Outlook-UDF-Test\TargetFolder\Mail", "", _
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")
   "Subject=TestMail", "BodyFormat=" & $olFormatHTML, "HTMLBody=Bodytext in <b>bold</b>.", "SentOnBehalfOfName=Doe Jane")
; Add a recipient and resolve it
; Add a recipient and resolve it
_OL_ItemRecipientAdd($oOL, $oItem, Default, $olTo, "Doe John")
_OL_ItemRecipientAdd($oOL, $oItem, Default, $olTo, "Doe John")
Line 36: Line 38:
If @extended = -2147352567 Then _
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!")
   MsgBox(16, "OutlookEX UDF - Example Script", "You don't have permission to send the mail on behalf of the specified person!")
_OL_Close($oOL)
</syntaxhighlight>
</syntaxhighlight>


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'').
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.
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.
=== Send a mail from another account ===
If there are multiple accounts defined in your Outlook profile you can set property "SendUsingAccount" to send a mail using this selected account.<br />
Requires Outlook 2010 or later.
Example:
<syntaxhighlight lang="autoit">
#include <OutlookEX.au3>
Global $oOutlook = _OL_Open()
; Create the mail item
Global $oItem = _OL_ItemCreate($oOutlook, $olMailItem, "", "", "Subject=TestMail", "Body=TestBody")
; Add Jon Doe as recipient
$oItem = _OL_ItemRecipientAdd($oOutlook, $oItem, Default, $olTo, "John.Doe@gmx.com")
; Search for the account with SMTP mail address "mail.address@company.com"
For $oAccount In $oOutlook.Session.Accounts
    If $oAccount.SMTPAddress = "mail.address@company.com" Then
        $oItem.SendUsingAccount = $oAccount
        ExitLoop
    EndIf
Next
; Send the mail from this account
_OL_ItemSend($oOutlook, $oItem)
_OL_Close($oOutlook)
</syntaxhighlight>
=== Send a mail unencrypted or/and unsigned ===
If sending mails signed or/and encrypted is on by default and you need to change this settings for a single mail, use the following example code:
Example:
<syntaxhighlight lang="autoit">
#include <OutlookEX.au3>
Global $oItem, $oPropertyAccessor, $sMapi
Global $oOutlook = _OL_Open()
If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF", "Error creating a connection to Outlook. @error = " & @error & ", @extended = " & @extended)
; Create the item
$oItem = _OL_ItemCreate($oOutlook, $olMailItem, "*", "", "Subject=TestMail", "BodyFormat=" & $olFormatHTML, "HTMLBody=Bodytext in <b>bold</b>.")
If @error <> 0 Then Exit MsgBox(16, "Example Script", "Error creating a mail. @error = " & @error & ", @extended = " & @extended)
; Set recipient
_OL_ItemRecipientAdd($oOutlook, $oItem, Default, $olTo, "Jon.Doe@gmx.com") ; <== MODIFY RECIPIENT !!
; Send mail unencrypted and unsigned
$sProperty = "http://schemas.microsoft.com/mapi/proptag/0x6E010003" ; PR_SECURITY_FLAGS: 0 = no security, 1 = encrypted, 2 = signed, 3 = signed and encrypted
$oPropertyAccessor = $oItem.PropertyAccessor
If @error <> 0 Then Exit MsgBox(16, "Example Script", "Error accessing PropertyAccessor. @error = " & @error & ", @extended = " & @extended)
$oPropertyAccessor.SetProperty($sProperty, 0) ; PR_SECURITY_FLAGS: 0 = no security, 1 = encrypted, 2 = signed, 3 = signed and encrypted
; Send item and close Outlook
_OL_ItemSend($oOutlook, $oItem)
If @error <> 0 Then Exit MsgBox(16, "Example Script", "Error sending mail'. @error = " & @error & ", @extended = " & @extended)
_OL_Close($oOutlook)
</syntaxhighlight>


=== Delay the sending of a mail ===
=== Delay the sending of a mail ===
Line 52: Line 106:
; Create a mail item and set some properties
; Create a mail item and set some properties
Global $oItem = _OL_ItemCreate($oOL, $olMailItem, "*\Outlook-UDF-Test\TargetFolder\Mail", "", _
Global $oItem = _OL_ItemCreate($oOL, $olMailItem, "*\Outlook-UDF-Test\TargetFolder\Mail", "", _
   "Subject=TestMail", "BodyFormat=" & $olFormatHTML, "HTMLBody=Bodytext in &lt;b>Test&lt;/b>.", _
   "Subject=TestMail", "BodyFormat=" & $olFormatHTML, "HTMLBody=Bodytext in <b>bold</b>.", _
   "DeferredDeliveryTime=2017/03/18 18:01:00")
   "DeferredDeliveryTime=2017/03/18 18:01:00")
; Add a recipient and resolve it
; Add a recipient and resolve it
Line 58: Line 112:
; Send the mail
; Send the mail
_OL_ItemSend($oOL, $oItem)
_OL_ItemSend($oOL, $oItem)
_OL_Close($oOL)
</syntaxhighlight>
</syntaxhighlight>


Line 70: Line 125:


'''Problem:'''<br />
'''Problem:'''<br />
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.
You can't use function _OL_ItemCreate or _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:'''<br />
'''Solution:'''<br />
Line 111: Line 166:
$oItem.HTMLBody = "RTF: <b>Mail</b> Text" & $sBody
$oItem.HTMLBody = "RTF: <b>Mail</b> Text" & $sBody
$oItem.Display
$oItem.Display
</syntaxhighlight>
=== 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.
<syntaxhighlight lang="autoit">
#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 <b>bold</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)
</syntaxhighlight>
=== Access full content of the Body and HTMLBody property ===
The Outlook methods used by _OL_ItemFind only return 255 characters of the Body and HTMLBody property. To get the full body you need to directly access the object's property:
<syntaxhighlight lang="autoit">
; Find all items in the inbox
Global $aItems = _OL_ItemFind($oOutlook, "*\Outlook-UDF-Test", $olMail, "", "", "", "EntryID,Subject", "", 1)
$oItem = $oOL.Session.GetItemFromID($aItems[1][0], Default) ; retrieve the items object
$sBody = $oItem.Body
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 09:27, 27 July 2023

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", "<b>Test</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 <b>bold</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.

Send a mail from another account

If there are multiple accounts defined in your Outlook profile you can set property "SendUsingAccount" to send a mail using this selected account.
Requires Outlook 2010 or later.

Example:

#include <OutlookEX.au3>
Global $oOutlook = _OL_Open()
; Create the mail item
Global $oItem = _OL_ItemCreate($oOutlook, $olMailItem, "", "", "Subject=TestMail", "Body=TestBody")
; Add Jon Doe as recipient
$oItem = _OL_ItemRecipientAdd($oOutlook, $oItem, Default, $olTo, "John.Doe@gmx.com")
; Search for the account with SMTP mail address "mail.address@company.com"
For $oAccount In $oOutlook.Session.Accounts
    If $oAccount.SMTPAddress = "mail.address@company.com" Then
        $oItem.SendUsingAccount = $oAccount
        ExitLoop
    EndIf
Next
; Send the mail from this account
_OL_ItemSend($oOutlook, $oItem)
_OL_Close($oOutlook)

Send a mail unencrypted or/and unsigned

If sending mails signed or/and encrypted is on by default and you need to change this settings for a single mail, use the following example code:

Example:

#include <OutlookEX.au3>
Global $oItem, $oPropertyAccessor, $sMapi
Global $oOutlook = _OL_Open()
If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF", "Error creating a connection to Outlook. @error = " & @error & ", @extended = " & @extended)
; Create the item
$oItem = _OL_ItemCreate($oOutlook, $olMailItem, "*", "", "Subject=TestMail", "BodyFormat=" & $olFormatHTML, "HTMLBody=Bodytext in <b>bold</b>.")
If @error <> 0 Then Exit MsgBox(16, "Example Script", "Error creating a mail. @error = " & @error & ", @extended = " & @extended)
; Set recipient
_OL_ItemRecipientAdd($oOutlook, $oItem, Default, $olTo, "Jon.Doe@gmx.com") ; <== MODIFY RECIPIENT !!
; Send mail unencrypted and unsigned
$sProperty = "http://schemas.microsoft.com/mapi/proptag/0x6E010003" ; PR_SECURITY_FLAGS: 0 = no security, 1 = encrypted, 2 = signed, 3 = signed and encrypted
$oPropertyAccessor = $oItem.PropertyAccessor
If @error <> 0 Then Exit MsgBox(16, "Example Script", "Error accessing PropertyAccessor. @error = " & @error & ", @extended = " & @extended)
$oPropertyAccessor.SetProperty($sProperty, 0) ; PR_SECURITY_FLAGS: 0 = no security, 1 = encrypted, 2 = signed, 3 = signed and encrypted
; Send item and close Outlook
_OL_ItemSend($oOutlook, $oItem)
If @error <> 0 Then Exit MsgBox(16, "Example Script", "Error sending mail'. @error = " & @error & ", @extended = " & @extended)
_OL_Close($oOutlook)

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 <b>bold</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 function _OL_ItemCreate or _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 <b>bold</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)

Access full content of the Body and HTMLBody property

The Outlook methods used by _OL_ItemFind only return 255 characters of the Body and HTMLBody property. To get the full body you need to directly access the object's property:

; Find all items in the inbox
Global $aItems = _OL_ItemFind($oOutlook, "*\Outlook-UDF-Test", $olMail, "", "", "", "EntryID,Subject", "", 1)
$oItem = $oOL.Session.GetItemFromID($aItems[1][0], Default) ; retrieve the items object
$sBody = $oItem.Body