Python 調(diào)用 Outlook 發(fā)送郵件過(guò)程解析
微軟 Office 提供基于 COM 接口的編程。Python 通過(guò) pywin32 可以方便地調(diào)用各組件。如果下載和安裝 pywin32 有困難,可以到 Sourceforge 的鏡像網(wǎng)摘查找合適的版本。
單一賬號(hào)
import win32com.client as win32
def send_mail():
outlook = win32.Dispatch('Outlook.Application')
mail_item = outlook.CreateItem(0) # 0: olMailItem
mail_item.Recipients.Add('someone@qq.com')
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()
多賬號(hào)發(fā)送郵件
如果 Outlook 有多個(gè)賬號(hào),需要選擇賬號(hào)發(fā)送郵件,需要在代碼中對(duì)賬號(hào)進(jìn)行判斷,代碼如下:
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 == 'sender@hotmail.com':
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('receipient@qq.com')
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()
這里有點(diǎn)黑魔法,直接設(shè)置 mail_item.SendUsingAccount 不會(huì)起作用,返回值是 None, 永遠(yuǎn)從第一個(gè)郵箱賬號(hào)發(fā)送郵件,我使用的是 Office 365 版。需要調(diào)用 _oleobj_.Invoke() 方法。后面列出了參考鏈接。
本質(zhì)上,這種方法是調(diào)用 COM 組件,可以查詢微軟的開(kāi)發(fā)幫助了解相關(guān)對(duì)象的屬性和方法,比如我想知道 Account 的細(xì)節(jié),就特意參考了下面這篇幫助:https://docs.microsoft.com/zh-cn/office/vba/api/outlook.account 。COM 編程與語(yǔ)言無(wú)關(guān)。另外可以在 Outlook 中 ALT + F11,進(jìn)入 VBE 環(huán)境,然后 F2 進(jìn)入對(duì)象瀏覽器界面查看比如下面的界面顯示了 Account 的屬性和方法:

關(guān)于調(diào)試
python 作為動(dòng)態(tài)語(yǔ)言,通過(guò) Debug 獲取 COM 對(duì)象信息并不是很方便,比如下面代碼:
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()
設(shè)置斷點(diǎn)的調(diào)試界面:

我們只知道 account 是一個(gè) COM Object,account 包含的信息很多都是 unknown 的。碰到這種情況,我一般用 C# 或者 VBA 編寫代碼進(jìn)行調(diào)試。如果我需要詳細(xì)了解 account 的屬性和方法,在 Office 的任何組件中,比如 Excel,寫一段下面的代碼:
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
顯示出監(jiān)視窗口,設(shè)置斷點(diǎn),獲取 accounts 信息:

在監(jiān)視窗口添加變量 accounts:

展開(kāi):

Item 1 和 Item 2 表示有兩個(gè)賬號(hào),現(xiàn)在我們想看到 Item 2 的賬號(hào)信息,將 Item 2 展開(kāi):

DeliveryStore 屬性也包含 account 的信息,可以展開(kāi)查看。
參考
SendUsingAccount Does Not Work in Outlook 2010, possible bug?
python win32com outlook 2013 SendUsingAccount return exception
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python 整行讀取文本方法并去掉readlines換行\(zhòng)n操作
這篇文章主要介紹了Python 整行讀取文本方法并去掉readlines換行\(zhòng)n操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
利用Python內(nèi)置庫(kù)實(shí)現(xiàn)創(chuàng)建命令行應(yīng)用程序
Python?有一個(gè)叫做argparse的內(nèi)置庫(kù),可以用它來(lái)創(chuàng)建一個(gè)命令行界面。本文將詳解如何利用argparse實(shí)現(xiàn)創(chuàng)建一個(gè)命令行應(yīng)用程序,需要的可以參考一下2022-06-06
Tensorflow2.4從頭訓(xùn)練Word?Embedding實(shí)現(xiàn)文本分類
這篇文章主要為大家介紹了Tensorflow2.4從頭訓(xùn)練Word?Embedding實(shí)現(xiàn)文本分類,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Windows下python3安裝tkinter的問(wèn)題及解決方法
這篇文章主要介紹了Windows下python3安裝tkinter問(wèn)題及解決方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
一文總結(jié)學(xué)習(xí)Python的14張思維導(dǎo)圖
一文總結(jié)學(xué)習(xí)Python的14張思維導(dǎo)圖,本文涵蓋了Python編程的核心知識(shí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
python爬蟲(chóng)scrapy基本使用超詳細(xì)教程
這篇文章主要介紹了python爬蟲(chóng)scrapy基本使用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
Python?Flask框架實(shí)現(xiàn)Proteus仿真Arduino與網(wǎng)頁(yè)數(shù)據(jù)交互
這篇文章主要介紹了Python?Flask框架實(shí)現(xiàn)Proteus仿真Arduino與網(wǎng)頁(yè)數(shù)據(jù)交互,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-11-11

