代码语言:javascript复制
# -*- coding:utf-8 -*-
import win32com.client as win32
import warnings
import sys
reload(sys)
sys.setdefaultencoding('utf8')
warnings.filterwarnings('ignore')
def sendemail(sub,body):
outlook = win32.Dispatch('outlook.application')
receivers = ['xxxx@pingan.com.cn;xxxx@pingan.com.cn;xxxx@pingan.com.cn']
mail = outlook.CreateItem(0)
mail.To = receivers[0]
mail.Subject = sub.decode('utf-8')
mail.Body = body.decode('utf-8')
# 添加附件
# mail.Attachments.Add('D:UsersxxxDesktopemail.log')
mail.Send()
sendemail('xxx,xx','xxx')
如果报错:com_error: (-2147221008, 'xc9xd0xcexb4xb5xf7xd3xc3 CoInitializexa1xa3', None, None)
只需导入pythoncom包,import pythoncom
并在方法内加入pythoncom.CoInitialize()即可。