python发送邮件 api_python接口自动化(5)发送邮件

发送邮件

import smtplib

from email.mime.text import MIMEText

from email.mime.multipart import MIMEMultipart #如果要添加附件,必须使用MIMEMultipart

from email.header import Header

#smtpserver,port,sender,receivers,sender_password

def sendEmail(smtpserver,port,sender,receivers,sender_password):

message=MIMEMultipart()

message.attach(MIMEText('发送邮件测试','plain','utf-8'))

message['From']=Header("Jimmy",'utf-8')

message['To']=Header('测试','utf-8')

subject='python smtp 邮件测试'

message['Subject']=Header(subject,'utf-8')

#添加附件1:

att1=MIMEText(open('../sources/runoob1.txt','rb').read(),'base64','utf-8')

att1['Content-Type']='application/octet-stream'

att1['Content-Disposition']='attachment; filename=runoob1.txt'

message.attach(att1)

#添加附件2:

att1=MIMEText(open('../sources/runoob2.txt','rb').read(),'base64','utf-8')

att1['Content-Type']='application/octet-stream'

att1['Content-Disposition']='attachment;filename=runoob2.txt'

message.attach(att1)

try:

smtpObj=smtplib.SMTP_SSL(smtpserver,port)

smtpObj.login(sender,sender_password)

smtpObj.sendmail(sender,receivers,message.as_string())

smtpObj.quit()

print("邮件发送成功")

except smtplib.SMTPException:

print("Error:无法发送邮件")

if __name__=='__main__':

sender='974643517@qq.com'

receivers=['974643517@qq.com']

sendEmail("smtp.qq.com",465,sender,receivers,"gofyiuzkjzlzbdic")

运行结果如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值