码迷,mamicode.com
首页 > 编程语言 > 详细

python发送邮件

时间:2019-03-07 14:09:53      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:utf-8   orm   ica   att   tip   mes   file   mat   ipa   

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header

mail_host = "smtp.163.com"  # 设置服务器
mail_user = "17731456887@163.com"  # 用户名
mail_pass = "python123"  # 口令

sender = 17731456887@163.com
receivers = [1758152342@qq.com,17731456887@163.com]

# 创建一个带附件的实例
message = MIMEMultipart()
message[From] = 17731456887@163.com
message[To] = 1758152342@qq.com
subject = Python SMTP 邮件
message[Subject] = Header(subject, utf-8)

# 邮件正文内容
message.attach(MIMEText(这是菜鸟教程Python 邮件发送……, plain, utf-8))

# 构造附件1,传送当前目录下的 test.txt 文件
att1 = MIMEText(open(rsafe.doc, rb).read(), base64, utf-8)
att1["Content-Type"] = application/octet-stream
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
att1["Content-Disposition"] = attachment; filename="{0}".format(safe.doc)
message.attach(att1)

# # 构造附件2,传送当前目录下的 runoob.txt 文件
# att2 = MIMEText(open(‘runoob.txt‘, ‘rb‘).read(), ‘base64‘, ‘utf-8‘)
# att2["Content-Type"] = ‘application/octet-stream‘
# att2["Content-Disposition"] = ‘attachment; filename="runoob.txt"‘
# message.attach(att2)

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: 无法发送邮件"

 

python发送邮件

标签:utf-8   orm   ica   att   tip   mes   file   mat   ipa   

原文地址:https://www.cnblogs.com/aaronthon/p/10440016.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!