outlook发送是Python直接调用win32方式,调用程序outlook直接发送邮件。
示例功能说明:
1.从远程服务器下载Excel文件作为邮件附件。
2.将文件发送老板。
import win32com.client as win32 import time import os import codecs, sys # 获取今天(现在时间) today = time.strftime('%Y-%m-%d',time.localtime(time.time())) year_month = time.strftime('%Y-%m',time.localtime(time.time())) # 文件目录 share_file_host='\\10.4.50.16\fab2文件库' share_file_path='\\10.4.50.16\fab2文件库\Department\daily_report\{}\{}.xlsx'.format(year_month,today) target_file_path='e:\' xlfile= 'e:\'+'{}.xlsx'.format(today) #cope 文件 def copy_share_file(): copy_cmd='xcopy /y {} {}'.format(share_file_path,target_file_path) print(copy_cmd) copy_result=os.popen(copy_cmd) print(copy_result.read()) #发送邮件 def send_mail(): #调用Outlook application
outlook = win32.Dispatch('Outlook.Application') send_account = 'xxx@csmc.crmicro.com' address_1 = 'aaa@csmc.crmicro.com' address_2 = 'bbb@csmc.crmicro.com'+';'+'ccc@csmc.crmicro.com' mail_item = outlook.CreateItem(0) # 0: olMailItem mail_item.To = address_1#收件人 mail_item.CC = address_2#抄送人 mail_item.Subject = today+' CIM小组工作总结' #主题 mail_item.BodyFormat = 2 # 2: Html format #邮件body mail_item.HTMLBody = 'Dear Sir,<br>'+mail_item.Subject+',敬请查阅.<br>xxx<br>tel:5513' #添加附件 mail_item.Attachments.Add(xlfile) mail_item.Send() if __name__ == '__main__': print(today) #拷贝附件到本地 copy_share_file() #发送邮件 send_mail()