注意,在内网的话,以下代码不成功,因为有网络限制。 但是在没有需要设置代理的网络,是可以成功的。
直接上code:
mailer.py
# !/usr/bin/env python3 # coding: utf-8 import smtplib from email.mime.text import MIMEText from email.header import Header sender = 'xxx@126.com' receiver = 'yyy@126.com' subject = 'python email test' smtpserver = 'smtp.126.com' username = 'xxx@126.com' password = 'ppppp' # 发送账户的密码 msg = MIMEText('你好', 'plain', 'utf-8') # 中文需参数‘utf-8’,单字节字符不需要 msg['Subject'] = Header(subject, 'utf-8') smtp = smtplib.SMTP() smtp.connect('smtp.126.com') smtp.login(username, password) smtp.sendmail(sender, receiver, msg.as_string()) smtp.quit()