Home  >  Article  >  Backend Development  >  python uses two ways to send emails, smtp and outlook examples

python uses two ways to send emails, smtp and outlook examples

黄舟
黄舟Original
2017-06-04 10:09:452697browse

本篇文章主要介绍了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 = ['Yutao.A.Wang@alcatel-sbell.com.cn'] 
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 = '18298268658@163.com' 
receivers = ['619538553@qq.com'] 
 
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)

The above is the detailed content of python uses two ways to send emails, smtp and outlook examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn