Outlook
文章目录
一、调用 win32com 操作 outlook 发送邮件
需要为你的电脑安装 outlook
代码如下:
from win32com.client import Dispatch # 导入包
class outlook():
'''重写outlook邮件功能'''
def __init__(self):
self.outlook = Dispatch('outlook.application')
def __str__(self):
pass
def send_email(self, to_receiver_list, cc_receiver_list=None, title=None, text=None, html_text=None,
file_list=None, importance=None, png_list=None):
'''
发送邮件
'''
mail = self.outlook.CreateItem(0)
if isinstance(to_receiver_list, list): # 发送邮箱
mail.To = ';'.join(to_receiver_list)
else:
mail.To = to_receiver_list
mail.Subject = title # 标题
'''正文二选一,如果有图片选择html_text'''
if text != None: # 正文
mail.Body = text
if html_text != None: # 支持html格式正文
mail.HTMLBody = html_text
if cc_receiver_list != None: # 抄送邮箱
if isinstance(cc_receiver_list, list):
mail.Cc = ';'.join(cc_receiver_list)
else:
mail.Cc = cc_receiver_list
if file_list != None: # 邮件附件
if isinstance(file_list, list):
for path in file_list:
mail.Attachments.Add(path)
else:
mail.Attachments.Add(file_list)
if importance != None: # 2 1 0 邮件重要性
mail.Importance = importance
if png_list != None: # html正文添加图片
if isinstance(png_list, list):
i = 1
for path in png_list:
attachment = mail.Attachments.Add(path)
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E",
"image{}".format(i))
i += 1
else:
attachment = mail.Attachments.Add(png_list)
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E",
"image{}".format(1))
mail.Send()
if __name__ == '__main__':
Outlook = outlook()
Outlook.send_email(to_receiver_list=['[email protected]'], title='测试标题', html_text='测试正文', importance = 1)
总结
win32com:https://learn.microsoft.com/zh-cn/windows/win32/LearnWin32/com-coding-practices
标签:__,outlook,python,最全,list,None,receiver,mail From: https://blog.csdn.net/SzyPy/article/details/139530296