python: e-mail

Python has two modules smtplib and email support for SMTP, email messages is responsible for construction, smtplib responsible for sending e-mail

# -*- 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()
Published 25 original articles · won praise 2 · Views 807

Guess you like

Origin blog.csdn.net/yangjinjingbj/article/details/104066014