本文共 749 字,大约阅读时间需要 2 分钟。
import smtplibfrom email.mime.text import MIMEText# 第三方 SMTP 服务mail_host = "smtp.163.com" # SMTP服务器mail_user = "username" # 用户名mail_pass = "passwd" # 密码sender = 'user@163.com' # 发件人邮箱(最好写全, 不然会失败)receivers = ['to_someone@qq.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱content = '过期教程害死人!'title = 'Python SMTP Mail Test' # 邮件主题message = MIMEText(content, 'plain', 'utf-8') # 内容, 格式, 编码message['From'] = "{}".format(sender)message['To'] = ",".join(receivers)message['Subject'] = titletry: smtpObj = smtplib.SMTP_SSL(mail_host, 465) # 启用SSL发信, 端口一般是465 smtpObj.login(mail_user, mail_pass) # 登录验证 smtpObj.sendmail(sender, receivers, message.as_string()) # 发送 print("mail has been send successfully.")except smtplib.SMTPException as e: print(e)
转载地址:http://snhdb.baihongyu.com/