import smtplib
from email.mime.text import MIMEText # 往邮件中写内容的对象
from email.header import Header # 邮件头中写东西
"""
配置发送邮箱信息
"""
# 发送方邮箱,即谁发送的
msg_from = '[email protected]'
# 生成的授权码
password = '**********'
# 发送给谁,即接收邮件的邮箱地址
msg_to = '[email protected]' # 或者给多人发 ['[email protected]','[email protected]']
# 邮件主标题
subject = "邮件标题"
# 邮件内容
content = '邮件的内容,内容格式为字符串'
# 邮件内容:html 格式
# content = '''
# <h1>HTML格式邮件</h1>
# <p>
# <a href="http://www.baidu.com">打开百度</a>
# </p>
# '''
# 生成一个 MIMEText 对象
msg = MIMEText(content)
# 放入主题邮件
msg['Subject'] = subject
# 也可以这样传
# msg['Subject'] = Header(subject, 'utf-8')
# 放入发件人
msg['From'] = msg_from
# 放入收件人
msg['To'] = msg_to
try:
# 通过 SSL 方式发送,服务地址、端口
s = smtplib.SMTP_SSL('smtp.qq.com', 465)
# 登录到邮箱
s.login(msg_from, password)
# 发送邮件:发送方,收件方,要发送的消息
s.sendmail(msg_from, msg_to, msg.as_string())
print("邮件发送成功!")
except s.SMTPException as e:
print(e)
finally:
s.quit()
import smtplib
from email.mime.text import MIMEText # 往邮件中写内容的对象
from email.header import Header # 邮件头中写东西
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email import encoders
"""
配置发送邮箱信息
"""
# 发送方邮箱,即谁发送的
msg_from = '[email protected]'
# 生成的授权码
password = '**************'
# 发送给谁,即接收邮件的邮箱地址
msg_to = '[email protected]'
# 邮件主标题
subject = "带附件邮件标题"
# 创建一个带附件的实例
msg = MIMEMultipart()
# 放入主题邮件
msg['Subject'] = subject
# 也可以这样传
# msg['Subject'] = Header(subject, 'utf-8')
# 放入发件人
msg['From'] = msg_from
# 放入收件人
msg['To'] = msg_to
# 邮件正文内容
msg.attach(MIMEText('Python 邮件发送带附件测试...', 'plain', 'utf-8'))
# 添加附件
# 附件1为文件
att1 = MIMEText(open(r'D:\Code\PythonProject\Python 入门与进阶\Celery\README.md', 'rb').read(), 'base64', 'utf-8')
att1['Content-Type'] = 'application/octet-stream'
# 这里的filename可以任意写,写什么,邮件中显示什么名字
att1["Content-Disposition"] = 'attachment; filename="README.md"'
msg.attach(att1)
# 附件2为图片
with open(r'D:\Code\PythonProject\Python 入门与进阶\Celery\img-10.png', 'rb') as f:
# 设置附件的MIME和文件名,这里是png类型
mime = MIMEBase('image', 'png', filename='授权码.png')
# 加上必要的头信息
mime.add_header('Content-Disposition', 'attachment', filename="授权码.png")
mime.add_header('Content-ID', '<0>')
mime.add_header('X-Attachment-Id', '0')
# 把附件的内容读进来
mime.set_payload(f.read())
# 用 Base64 编码
encoders.encode_base64(mime)
# 添加到 MIMEMultipart
msg.attach(mime)
try:
# 通过 SSL 方式发送,服务地址、端口
s = smtplib.SMTP_SSL('smtp.qq.com', 465)
# 登录到邮箱
s.login(msg_from, password)
# 发送邮件:发送方,收件方,要发送的消息
s.sendmail(msg_from, msg_to, msg.as_string())
print("邮件发送成功!")
except s.SMTPException as e:
print(e)
finally:
s.quit()
163.com:
接收邮件服务器: http://pop.163.com
接收端口: 110或995(使用ssl时)
接收邮件服务器: http://imap.163.com
接收端口: 143或993(使用ssl时)
发送邮件服务器: http://smtp.163.com
发送端口: 25或465/994(使用ssl时)
126邮箱:
接收邮件服务器: http://pop.126.com
接收端口: 110
发送邮件服务器: http://smtp.126.com
发送端口: 25
139邮箱:
POP3服务器地址:http://POP.139.com(端口:110)
SMTP服务器地址:http://SMTP.139.com(端口:25)
QQ邮箱:
接收邮件服务器: http://pop.qq.com
接收端口: 110或995(使用ssl时)
接收邮件服务器: http://imap.qq.com
接收端口: 143或993(使用ssl时)
发送邮件服务器: http://smtp.qq.com
发送端口: 25或465/587(使用ssl时)
QQ企业邮箱 :
POP3服务器地址:http://pop.exmail.qq.com (SSL启用 端口:995)
SMTP服务器地址:http://smtp.exmail.qq.com (端口:25或465/587(使用ssl时))
gmail邮箱:
POP3服务器地址:http://pop.gmail.com(SSL启用 端口:995)
SMTP服务器地址:http://smtp.gmail.com(SSL启用 端口:587)
Foxmail:
POP3服务器地址:http://POP.foxmail.com(端口:110)
SMTP服务器地址:http://SMTP.foxmail.com(端口:25)
sina.com:
接收邮件服务器: http://pop.sina.com
接收端口: 110
发送邮件服务器: http://smtp.sina.com
发送端口: 25
sinaVIP:
POP3服务器:http://pop3.vip.sina.com (端口:110)
SMTP服务器:http://smtp.vip.sina.com (端口:25)
sohu.com:
POP3服务器地址:http://pop3.sohu.com(端口:110)
SMTP服务器地址:http://smtp.sohu.com(端口:25)
雅虎邮箱:
接收邮件服务器: http://pop.mail.yahoo.cn
接收端口: 110或995(使用ssl时)
接收邮件服务器: http://imap.mail.yahoo.cn
接收端口: 143或993(使用ssl时)
发送邮件服务器: http://smtp.mail.yahoo.cn
发送端口: 25或465(使用ssl时)
HotMail :
接收邮件服务器: http://pop3.live.com
接收端口: 995
发送邮件服务器: http://smtp.live.com
发送端口: 25
263.net:
POP3服务器地址:http://pop3.263.net(端口:110)
SMTP服务器地址:http://smtp.263.net(端口:25)
263.net.cn:
POP3服务器地址:http://pop.263.net.cn(端口:110)
SMTP服务器地址:http://smtp.263.net.cn(端口:25)