https://www.runoob.com/python/python-email.html
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 第三方 SMTP 服务
mail_host="smtp.XXX.com" #设置服务器
mail_user="XXXX" #用户名
mail_pass="XXXXXX" #口令
sender = '[email protected]'
receivers = ['[email protected]'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
message = MIMEText('Python 邮件发送测试...', 'plain', 'utf-8')
message['From'] = Header("菜鸟教程", 'utf-8')
message['To'] = Header("测试", 'utf-8')
subject = 'Python SMTP 邮件测试'
message['Subject'] = Header(subject, 'utf-8')
try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host, 25) # 25 为 SMTP 端口号
smtpObj.login(mail_user,mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
print "邮件发送成功"
except smtplib.SMTPException:
print "Error: 无法发送邮件"
实践版本
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 第三方 SMTP 服务
mail_host = "smtp.163.com" # 设置服务器
mail_user = "****@163.com" # 用户名
mail_pass = "******" # 口令
sender = '*****@163.com'
receivers = ['*****[email protected]'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host, 25) # 25 为 SMTP 端口号 未加密
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receivers, '123455667')
print("邮件发送成功")
except smtplib.SMTPException:
print("Error: 无法发送邮件")
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import ssl
# 第三方 SMTP 服务
mail_host = "smtp.163.com" # 设置服务器
mail_user = "******@163.com" # 用户名
mail_pass = "******" # 口令
sender = mail_user
receivers = ['******@qq.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
try:
context = ssl.create_default_context() #使用ssl模块的context加载系统允许的证书,在登录时进行验证
smtpObj = smtplib.SMTP_SSL(mail_host, 465, context=context) # 完成加密通讯
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receivers, '123455667')
print("邮件发送成功")
smtpObj.quit()
except smtplib.SMTPException:
print("Error: 无法发送邮件")
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import ssl
# 第三方 SMTP 服务
mail_host = "smtp.163.com" # 设置服务器
mail_user = "*****@163.com" # 用户名
mail_pass = "****" # 口令
sender = mail_user
receivers = ['****@qq.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
message = MIMEText('mainSpider Report123123132', 'plain', 'utf-8')
message['From'] = sender
subject = 'mainSpider Report'
message['Subject'] = Header(subject, 'utf-8')
try:
context = ssl.create_default_context() #使用ssl模块的context加载系统允许的证书,在登录时进行验证
smtpObj = smtplib.SMTP_SSL(mail_host, 465, context=context) # 完成加密通讯
smtpObj.login(mail_user, mail_pass)
for r in receivers:
message['To'] = r
smtpObj.send_message(message)
# smtpObj.sendmail(message.as_string())
print("邮件发送成功")
smtpObj.quit()
except smtplib.SMTPException:
print("Error: 无法发送邮件")
Python操作邮箱发送邮件(网易邮箱)
PYTHON学习笔记(31)——关于smtplib库MIMEText和MIMEMultipart类的应用之自动发送邮件(html格式,有正文,有链接,有附件)