Python 3.4 实现邮件发送功能

Alice
Alice
发布时间:2026-10-12 | 4人浏览过
原创

本文介绍了使用 Python 3.4 实现邮件发送功能的具体代码。通过 smtplib 和 email 模块,示例展示了如何配置发件人、收件人,设置邮件主题与正文,并添加附件。代码包含登录 SMTP 服务器、构建 MIMEMultipart 对象及发送完整邮件的完整流程,供开发者参考。

本文分享了一个使用 Python 实现邮件发送功能的具体代码示例,供读者参考。

import smtplib 
import os 
from email.mime.text import MIMEText 
from email.mime.multipart import MIMEMultipart 
from email import encoders 
user = '*******@qq.com' 
pwd = '*******' 
to = ['******@139.com', '******@qq.com'] 
msg = MIMEMultipart() 
msg['Subject'] = '这里是主题...' 
content1 = MIMEText('这里是正文!', 'plain', 'utf-8') 
msg.attach(content1) 
attfile = 'C:\\Users\\hengli\\Pictures\\CameraMan\\哈哈.doc' 
basename = os.path.basename(attfile) 
fp = open(attfile, 'rb') 
att = MIMEText(fp.read(), 'base64', 'utf-8') 
att["Content-Type"] = 'application/octet-stream' 
att.add_header('Content-Disposition', 'attachment',filename=('gbk', '', basename)) 
encoders.encode_base64(att) 
msg.attach(att) 
#----------------------------------------------------------- 
s = smtplib.SMTP('smtp.qq.com') 
s.login(user, pwd) 
s.sendmail(user, to, msg.as_string()) 
print('发送成功') 
s.close() 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。

相关标签:

本站声明

本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。

Python 相关产品和应用

全部下载

专题列表