Python API

Exchange

class RPA.Email.Exchange.Exchange

Bases: object

Exchange is a library for sending, reading, and deleting emails. Exchange is interfacing with Exchange Web Services (EWS).

For more information about server settings, see this Microsoft support article.

Examples

Robot Framework

*** Settings ***
Library     RPA.Email.Exchange
Task Setup  Authorize  username=${ACCOUNT}  password=${PASSWORD}

*** Variables ***
${ACCOUNT}              ACCOUNT_NAME
${PASSWORD}             ACCOUNT_PASSWORD
${RECIPIENT_ADDRESS}    RECIPIENT
${IMAGES}               myimage.png
${ATTACHMENTS}          C:${/}files${/}mydocument.pdf

*** Tasks ***
Sending email
    Send Message  recipients=${RECIPIENT_ADDRESS}
    ...           subject=Exchange Message from RPA Robot
    ...           body=<p>Exchange RPA Robot message body<br><img src='myimage.png'/></p>
    ...           save=${TRUE}
    ...           html=${TRUE}
    ...           images=${IMAGES}
    ...           cc=EMAIL_ADDRESS
    ...           bcc=EMAIL_ADDRESS
    ...           attachments=${ATTACHMENTS}

Python

from RPA.Email.Exchange import Exchange

ex_account = "ACCOUNT_NAME"
ex_password = "ACCOUNT_PASSWORD"

mail = Exchange()
mail.authorize(username=ex_account, password=ex_password)
mail.send_message(
    recipients="RECIPIENT",
    subject="Message from RPA Python",
    body="RPA Python message body",
)
ROBOT_LIBRARY_DOC_FORMAT = 'REST'
ROBOT_LIBRARY_SCOPE = 'GLOBAL'
authorize(username: str, password: str, autodiscover: bool = True, access_type: str = 'DELEGATE', server: str = None, primary_smtp_address: str = None) → None

Connect to Exchange account

Parameters
  • username – account username

  • password – account password

  • autodiscover – use autodiscover or set it off

  • accesstype – default “DELEGATE”, other option “IMPERSONATION”

  • server – required for configuration options

  • primary_smtp_address – by default set to username, but can be set to be different than username

create_folder(folder_name: str = None, parent_folder: str = None) → bool

Create email folder

Parameters
  • folder_name – name for the new folder

  • parent_folder – name for the parent folder, by default INBOX

Returns

True if operation was successful, False if not

delete_folder(folder_name: str = None, parent_folder: str = None) → bool

Delete email folder

Parameters
  • folder_name – current folder name

  • parent_folder – name for the parent folder, by default INBOX

Returns

True if operation was successful, False if not

empty_folder(folder_name: str = None, parent_folder: str = None, delete_sub_folders: bool = False) → bool

Empty email folder of all items

Parameters
  • folder_name – current folder name

  • parent_folder – name for the parent folder, by default INBOX

  • delete_sub_folders – delete sub folders or not, by default False

Returns

True if operation was successful, False if not

list_messages(folder_name: str = None, count: int = 100) → list

List messages in the account inbox. Order by descending received time.

Parameters

count – number of messages to list

move_messages(criterion: str = '', source: str = None, target: str = None, contains: bool = False) → bool

Move message(s) from source folder to target folder

Parameters
  • criterion – move messages matching this criterion

  • source – source folder

  • target – target folder

  • contains – if matching should be done using contains matching and not equals matching, default False is means equals matching

Returns

boolean result of operation, True if 1+ items were moved else False

Criterion examples:

  • subject:my message subject

  • body:something in body

  • sender:sender@domain.com

rename_folder(oldname: str = None, newname: str = None, parent_folder: str = None) → bool

Rename email folder

Parameters
  • oldname – current folder name

  • newname – new name for the folder

  • parent_folder – name for the parent folder, by default INBOX

Returns

True if operation was successful, False if not

send_message(recipients: str, subject: str = '', body: str = '', attachments: str = None, html: bool = False, images: str = None, cc: str = None, bcc: str = None, save: bool = False)

Keyword for sending message through connected Exchange account.

Parameters
  • recipients – list of email addresses, defaults to []

  • subject – message subject, defaults to “”

  • body – message body, defaults to “”

  • attachments – list of filepaths to attach, defaults to []

  • html – if message content is in HTML, default False

  • images – list of filepaths for inline use, defaults to []

  • cc – list of email addresses, defaults to []

  • bcc – list of email addresses, defaults to []

  • save – is sent message saved to Sent messages folder or not, defaults to False

Email addresses can be prefixed with ex: to indicate an Exchange account address.

Recipients is a required parameter.

wait_for_message(criterion: str = '', timeout: float = 5.0, interval: float = 1.0, contains: bool = False) → Any

Wait for email matching criterion to arrive into INBOX.

Parameters
  • criterion – wait for message matching criterion

  • timeout – total time in seconds to wait for email, defaults to 5.0

  • interval – time in seconds for new check, defaults to 1.0

  • contains – if matching should be done using contains matching and not equals matching, default False is means equals matching

Returns

list of messages