网页给qq邮箱发送

您所在的位置:网站首页 qq邮箱怎么给别人发文件 网页给qq邮箱发送

网页给qq邮箱发送

#网页给qq邮箱发送| 来源: 网络整理| 查看: 265

最近用到Python自动发送邮件,主要就是三步,登录邮件、写邮件内容、发送,用到的库是 smtplib 和 email,直接使用pip安装即可

我使用的是QQ邮箱,首先需要设置QQ邮箱POP3/SMTP服务

7b9910cfdf481ee162cf91117dfa7ef7.png

9f5b3a23601380b2439fd34e8f7319d7.png

357d5ebdf5c54ee43aae6ee945f0c1c9.png

记住这个授权码,这个授权码就是Python脚本中登录邮箱时的密码,而不是你平时登录邮箱时的那个密码

一.发送普通文本邮件

#发送多种类型的邮件

from email.mime.multipart import MIMEMultipart

msg_from = '[email protected]' # 发送方邮箱

passwd = 'xxx'  #就是上面的授权码

to= ['[email protected]'] #接受方邮箱

#设置邮件内容

#MIMEMultipart类可以放任何内容

msg = MIMEMultipart()

conntent="这个是字符串"

#把内容加进去

msg.attach(MIMEText(conntent,'plain','utf-8'))

#设置邮件主题

msg['Subject']="这个是邮件主题"

#发送方信息

msg['From']=msg_from

#开始发送

#通过SSL方式发送,服务器地址和端口

s = smtplib.SMTP_SSL("smtp.qq.com", 465)

# 登录邮箱

s.login(msg_from, passwd)

#开始发送

s.sendmail(msg_from,to,msg.as_string())

print("邮件发送成功")

6c807789425b9f78a9e2588b2c28d9ce.png

二.发送携带附件的邮件

import smtplib

from email.mime.text import MIMEText

#发送多种类型的邮件

from email.mime.multipart import MIMEMultipart

msg_from = '[email protected]' # 发送方邮箱

passwd = 'xxxxx'

to= ['[email protected]'] #接受方邮箱

#设置邮件内容

#MIMEMultipart类可以放任何内容

msg = MIMEMultipart()

conntent="这个是字符串"

#把内容加进去

msg.attach(MIMEText(conntent,'plain','utf-8'))

#添加附件

att1=MIMEText(open('result.xlsx','rb').read(),'base64','utf-8') #打开附件

att1['Content-Type']='application/octet-stream'  #设置类型是流媒体格式

att1['Content-Disposition']='attachment;filename=result.xlsx' #设置描述信息

msg.attach(att1)  #加入到邮件中

#设置邮件主题

msg['Subject']="这个是邮件主题"

#发送方信息

msg['From']=msg_from

#开始发送

#通过SSL方式发送,服务器地址和端口

s = smtplib.SMTP_SSL("smtp.qq.com", 465)

# 登录邮箱

s.login(msg_from, passwd)

#开始发送

s.sendmail(msg_from,to,msg.as_string())

print("邮件发送成功")

36d1db9524dce3c4008e6002458de2a0.png

三.发送携带图片的附件

同理,可以使用上面的方法也可以发送图片附件

import smtplib

from email.mime.text import MIMEText

#发送多种类型的邮件

from email.mime.multipart import MIMEMultipart

msg_from = '[email protected]' # 发送方邮箱

passwd = 'xxxxx'

to= ['[email protected]'] #接受方邮箱

#设置邮件内容

#MIMEMultipart类可以放任何内容

msg = MIMEMultipart()

conntent="这个是字符串"

#把内容加进去

msg.attach(MIMEText(conntent,'plain','utf-8'))

#添加附件

att1=MIMEText(open('result.xlsx','rb').read(),'base64','utf-8') #打开附件

att1['Content-Type']='application/octet-stream'  #设置类型是流媒体格式

att1['Content-Disposition']='attachment;filename=result.xlsx' #设置描述信息

att2=MIMEText(open('1.jpg','rb').read(),'base64','utf-8')

att2['Content-Type']='application/octet-stream'  #设置类型是流媒体格式

att2['Content-Disposition']='attachment;filename=1.jpg' #设置描述信息

msg.attach(att1)  #加入到邮件中

msg.attach(att2)

#设置邮件主题

msg['Subject']="这个是邮件主题"

#发送方信息

msg['From']=msg_from

#开始发送

#通过SSL方式发送,服务器地址和端口

s = smtplib.SMTP_SSL("smtp.qq.com", 465)

# 登录邮箱

s.login(msg_from, passwd)

#开始发送

s.sendmail(msg_from,to,msg.as_string())

print("邮件发送成功")

9eec12fc9ead88e6a2280be99ade44f8.png

四.发送 html 格式的邮件

import smtplib

from email.mime.text import MIMEText

#发送多种类型的邮件

from email.mime.multipart import MIMEMultipart

import datetime

msg_from = '[email protected]' # 发送方邮箱

passwd = 'xxxxxx'

to= ['[email protected]'] #接受方邮箱

#设置邮件内容

#MIMEMultipart类可以放任何内容

msg = MIMEMultipart()

# conntent="这个是字符串"

# #把内容加进去

# msg.attach(MIMEText(conntent,'plain','utf-8'))

#添加附件

att1=MIMEText(open('result.xlsx','rb').read(),'base64','utf-8') #打开附件

att1['Content-Type']='application/octet-stream'  #设置类型是流媒体格式

att1['Content-Disposition']='attachment;filename=result.xlsx' #设置描述信息

att2=MIMEText(open('1.jpg','rb').read(),'base64','utf-8')

att2['Content-Type']='application/octet-stream'  #设置类型是流媒体格式

att2['Content-Disposition']='attachment;filename=1.jpg' #设置描述信息

msg.attach(att1)  #加入到邮件中

msg.attach(att2)

now_time = datetime.datetime.now()

year = now_time.year

month = now_time.month

day = now_time.day

mytime = str(year) + " 年 " + str(month) + " 月 " + str(day) + " 日 "

fayanren="爱因斯坦"

zhuchiren="牛顿"

#构造HTML

content = '''

这个是标题,xxxx通知

您好:

以下内容是本次会议的纪要,请查收!

发言人:{fayanren}

主持人:{zhuchiren}

{mytime}

'''.format(fayanren=fayanren, zhuchiren=zhuchiren, mytime=mytime)

msg.attach(MIMEText(content,'html','utf-8'))

#设置邮件主题

msg['Subject']="这个是邮件主题"

#发送方信息

msg['From']=msg_from

#开始发送

#通过SSL方式发送,服务器地址和端口

s = smtplib.SMTP_SSL("smtp.qq.com", 465)

# 登录邮箱

s.login(msg_from, passwd)

#开始发送

s.sendmail(msg_from,to,msg.as_string())

print("邮件发送成功")

5a2e0ae25cf87769e6971b597678d036.png

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



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3