python : 发邮件

Python对SMTP支持有smtplib和email两个模块,email负责构造邮件,smtplib负责发送邮件

# -*- coding: UTF-8 -*-
from email.mime.text import MIMEText

# 输入Email地址和授权码:
from_addr = "[email protected]"
password = "xxx"
# 输入收件人地址:
to_addr = "[email protected]"
# 输入SMTP服务器地址:
smtp_server = "smtp.163.com"

msg = MIMEText('hello, send by Python...', 'plain', 'utf-8')
msg['Subject'] = "主题为: 邮件的主题我还没想好呢~~"
msg['From'] = from_addr   # 发送者
msg['To'] = to_addr   # 接收者

import smtplib
server = smtplib.SMTP() # SMTP协议默认端口是25
server.connect(smtp_server, 25)
server.set_debuglevel(1)
server.login(from_addr, password)
server.sendmail(from_addr, to_addr, msg.as_string())
print("邮件发送成功")
server.quit()
发布了25 篇原创文章 · 获赞 2 · 访问量 807

猜你喜欢

转载自blog.csdn.net/yangjinjingbj/article/details/104066014