Python calls the Outlook e-mail accounts and more

Microsoft Office COM interface-based programming. Python can easily call each component through pywin32. If you download and install pywin32 have difficulty to pick Sourceforge's mirror network to find the right version.

Single account

import win32com.client as win32

def send_mail():
    outlook = win32.Dispatch('Outlook.Application')

    mail_item = outlook.CreateItem(0) # 0: olMailItem

    mail_item.Recipients.Add('[email protected]')
    mail_item.Subject = 'Mail Test'

    mail_item.BodyFormat = 2          # 2: Html format
    mail_item.HTMLBody  = '''
        <H2>Hello, This is a test mail.</H2>
        Hello Guys. 

    mail_item.Send()

if __name__ == '__main__':
    send_mail()

Multiple accounts to send mail

If Outlook has multiple accounts, you need to select the account to send mail, the account needs to be broken in judgment # code, the code is as follows:

def send_mail():
    outlook_app = win32.Dispatch('Outlook.Application')

    # choose sender account
    send_account = None
    for account in outlook_app.Session.Accounts:
        if account.DisplayName == '[email protected]':
            send_account = account
            break

    mail_item = outlook_app.CreateItem(0)   # 0: olMailItem

    # mail_item.SendUsingAccount = send_account not working
    # the following statement performs the function instead
    mail_item._oleobj_.Invoke(*(64209, 0, 8, 0, send_account))

    mail_item.Recipients.Add('[email protected]')
    mail_item.Subject = 'Test sending using particular account'
    mail_item.BodyFormat = 2   # 2: Html format
    mail_item.HTMLBody = '''
        <H2>Hello, This is a test mail.</H2>
        Hello Guys. 
        '''

    mail_item.Send()


if __name__ == '__main__':
    send_mail()

Here a little black magic, directly mail_item.SendUsingAccount not work, the return value is None, always from the first email account to send a message, I'm using Office 365 version. You need to call OLEObj .Invoke () method. Listed after the reference link.

In essence, this method is to call a COM component, you can query Microsoft's development to help understand the properties and methods related objects, for example, I want to know the details of the Account, it is specifically referenced below this help: https: //docs.microsoft. com / zh-cn / office / vba / api / outlook.account. COM programming language-independent. In addition you can in Outlook ALT + F11, enter the VBE environment, then F2 to enter the object browser interface to view such as the following screen shows the properties and methods of Account:

Here Insert Picture Description
About debugging
python as a dynamic language, access to information through the Debug COM objects is not very easy, for example, the code below:

import win32com.client as win32

def print_outlook_accounts():
    outlook_app = win32.Dispatch('Outlook.Application')

    for account in outlook_app.Session.Accounts:
        print (account.DeliveryStore.DisplayName)

if __name__ == '__main__':
    send_mail()

Set a breakpoint debugging interface:

Here Insert Picture Description

We only know that account is a COM Object, it contains a lot of information is unknown account of. In that case, I generally use C # or write VBA code debugging. If I need to learn more about the properties and methods account in any component of Office, such as Excel, write a code below:

Public Sub Print_Outlook_Accounts()

    ' 工具 -> 引用:添加 Microsoft Outook Object Library 引用
    
    Dim outlookApp As New Outlook.Application
    Dim accounts As Outlook.accounts
    
    Set accounts = outlookApp.Session.accounts
    
    Dim account As Outlook.account
    For Each account In accounts
        Debug.Print account.DisplayName
    Next
End Sub

Shows the monitor window, set breakpoints, information acquired accounts:

Here Insert Picture Description
Add a variable in the watch window accounts:

Here Insert Picture Description
Expand:

Here Insert Picture Description

Item 1 and Item 2 represent two accounts, and now we want to see account information Item 2 and Item 2 will unfold:

Here Insert Picture Description

DeliveryStore property also contains account information, you can expand the view.
Reference
SendUsingAccount Does Not Work in Outlook 2010, Possible bug?
Python win32com Outlook 2013 SendUsingAccount return Exception

Author: Stone0823
link: https: //www.jianshu.com/p/4f0ed762f521
Source: Jane books
are copyrighted by the author. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

Released eight original articles · won praise 3 · Views 5551

Guess you like

Origin blog.csdn.net/cwjcw81/article/details/104074092