Python 发邮件例子
Python 发邮件例子
例子
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2019-04-23 16:12:33
# @Author : BenLam
# @Link : https://www.cnblogs.com/BenLam/
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
#发送邮箱服务器
smtpserver='smtp.mxhichina.com'
#发送邮箱用户/密码
user='tester@qq.com'
password='123456'
#发送人
sender='tester@qq.com'
#收件人
receiver='tester@qq.com'
#主题
subject='test E-mail'
msgRoot=MIMEMultipart('test')
#正文
content='测试邮件地址!'
cont=MIMEText(content,'plain','utf-8')
#附件
file=open('C:\\bug.txt','rb').read()
mark=MIMEText(file,'base64','utf-8')
mark["Content-Type"]='application/octet-stream'
mark["Content-Disposition"]='attachment;filename="bug.txt"'
msgRoot['Subject'] = subject
msgRoot.attach(cont)
msgRoot.attach(mark)
#开始发送邮件
smtp=smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(user, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()
结果:
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步