python使用两种发邮件的方式smtp和outlook示例
smtp是直接调用163邮箱的smtp服务器,需要在163邮箱中设置一下。outlook发送就是Python直接调用win32方式。调用程序outlook直接发送邮件。
import win32com.client as win32 import xlrd outlook = win32.Dispatch('outlook.application') mail = outlook.CreateItem(0) receivers = ['[email protected]'] mail.To = receivers[0] mail.Subject ='test1' workbook = xlrd.open_workbook('E:\\kpi excel\\00_summary.xls') mySheet = workbook.sheet_by_index(0) nrows = mySheet.nrows content = [] for i in range(nrows): ss = mySheet.row_values(i) content.append(ss) print(content) Truecontent =str(content) mail.Body = Truecontent mail.Attachments.Add('E:\\kpi excel\\00_summary.xls') mail.Send()
smtp发送邮件
import smtplib from email.mime.text import MIMEText mail_host = 'smtp.163.com' mail_user = '18298268658' mail_pass = 'cat123' sender = '[email protected]' receivers = ['[email protected]'] message = MIMEText('content','plain','utf-8') message['Subject'] = 'title' message['From'] = sender message['To'] = receivers[0] try: smtpObj = smtplib.SMTP() smtpObj.connect(mail_host,25) smtpObj.login(mail_user,mail_pass) smtpObj.sendmail( sender,receivers,message.as_string()) smtpObj.quit() print('success') except smtplib.SMTPException as e: print('error',e)
相关推荐
zhangwentaohh 2020-09-16
文山羊 2020-08-16
行吟阁 2020-07-04
xiaodaiwang 2020-06-12
JasonYeung 2020-06-07
oXiaoChong 2020-06-01
sunnyhappy0 2020-05-19
airfling 2020-05-19
singer 2020-05-06
winmeanyoung 2020-04-11
wenjs00 2020-03-06
vs00ASPNET 2020-02-19
zhangpan 2020-01-10
ITxiaobaibai 2020-01-03
huakai 2019-12-29
cbao 2019-12-24
idning 2019-12-21