Python使用SMTP发送邮件(163,yeah等网易邮箱已测试可以)

#! /usr/bin/env python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
mailto_list=['xxxxxx@163.com'] #收件人(列表)
mail_host="smtp.126.com" #使用的邮箱的smtp服务器地址,这里是163的smtp地址
mail_user="xxxxxx@126.com" #用户名
mail_pass="xxxxxx" #密码
mail_postfix="126.com" #邮箱的后缀,网易就是163.com
def send_mail(to_list,sub,content):
me="hello"+"<"+mail_user+"@"+mail_postfix+">"
msg = MIMEText(content,_subtype='plain')
msg['Subject'] = sub
msg['From'] = me
msg['To'] = ";".join(to_list) #将收件人列表以‘;’分隔
try:
server = smtplib.SMTP()
server.connect(mail_host) #连接服务器
server.login(mail_user,mail_pass) #登录操作
server.sendmail(me, to_list, msg.as_string())
server.close()
return True
except Exception as e:
print(str(e))
return False
for i in range(1): #发送1封,上面的列表是几个人,这个就填几
if send_mail(mailto_list,"电话","电话是XXX"): #邮件主题和邮件内容
#这是最好写点中文,如果随便写,可能会被网易当做垃圾邮件退信
print ("done!")
else:
print ("failed!")


注:
如果出现“b'User has no permission'”错误,是因为发送邮件的邮箱未开启客户端授权码。参考网址:http://blog.csdn.net/hughnes/article/details/52070878

你可能感兴趣的:(Python使用SMTP发送邮件(163,yeah等网易邮箱已测试可以))