此文章我用的python版本是:python3.x;
脚本代码:
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
send_nickname = '依然范儿特西' # 发件人昵称
send_accout='3151775165@qq.com' # 发件人邮箱账号
send_pass = '******' # 发件人邮箱密码
receive_user ='3151775165@qq.com' # 收件人邮箱账号,
title = "代码改变世界" #邮件的主题或标题
def mail():
ret=True
try:
msg=MIMEText('此处填写邮件内容','plain','utf-8')
msg['From']=formataddr([send_nickname,send_accout]) # 括号里的对应发件人邮箱昵称、发件人邮箱账号
msg['To']=formataddr(["FK",receive_user]) # 括号里的对应收件人邮箱昵称、收件人邮箱账号
msg['Subject']=title # 邮件的主题,也可以说是标题
server=smtplib.SMTP_SSL("smtp.qq.com", 465) # 发件人邮箱中的SMTP服务器,qq端口是465
server.login(send_accout, send_pass) # 括号中对应的是发件人邮箱账号、邮箱密码
server.sendmail(send_accout,[receive_user,],msg.as_string()) # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
server.quit() # 关闭连接
except Exception: # 如果 try 中的语句没有执行,则会执行下面的 ret=False
ret=False
return ret
ret=mail()
if ret:
print("邮件发送成功")
else:
print("邮件发送失败")
邮件发送测试:
python test.py
查看邮件结果: