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()

结果:

 

转载于:https://www.cnblogs.com/BenLam/p/10757403.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值