开发笔记

  • 首页
  • 工具箱
三味线的博客
  1. 首页
  2. JS/PY
  3. 正文

Python发送邮件

2020-04-27 444点热度 0人点赞 0条评论

使用Python发送邮件:

#!/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_port = 25                   # 端口
mail_user = "xxxxxxxxx@163.com"  # 用户名
mail_passwd = "xxxxxxxxxxxx"     # 密码
# 发件人
mail_from = "某组织<xxxxxxx@163.com>"
# 收件人
receivers = ['xxxxxx@qq.com']

message = MIMEText('Python 邮件发送测试...', 'plain', 'utf-8')
message['From'] = Header(mail_from, 'utf-8')
message['To'] = ','.join(receivers)
# 标题
subject = 'Python SMTP 邮件测试'
message['Subject'] = Header(subject, 'utf-8')

# 发送附件 test.txt 文件
# attach = MIMEText(open('test.txt', 'rb').read(), 'base64', 'utf-8')
# attach["Content-Type"] = 'application/octet-stream'
# attach["Content-Disposition"] = 'attachment; filename="test.txt"'
# message.attach(attach)

try:
    smtpObj = smtplib.SMTP()
    smtpObj.connect(mail_host, mail_port)
    smtpObj.login(mail_user, mail_passwd)
    smtpObj.sendmail(mail_user, receivers, message.as_string())
    print("Send mail success.")
except smtplib.SMTPException:
    print("Error: Send mail failed.")

本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: Python
最后更新:2020-05-10

Clownce

不吃咸鱼的猫

点赞
< 上一篇
下一篇 >

文章评论

取消回复

Captcha Code

COPYRIGHT © 2020 Clownce. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS

蜀ICP备18010095号