python使⽤QQ邮箱实现⾃动发送邮件最近⽤到Python⾃动发送邮件,主要就是三步,登录邮件、写邮件内容、发送,⽤到的库是 smtplib 和 email,直接使⽤pip安装即可
我使⽤的是QQ邮箱,⾸先需要设置QQ邮箱POP3/SMTP服务
记住这个授权码,这个授权码就是Python脚本中登录邮箱时的密码,⽽不是你平时登录邮箱时的那个密码
⼀.发送普通⽂本邮件
#发送多种类型的邮件
from email.mime.multipart import MIMEMultipart
msg_from = '1508691067@qq' # 发送⽅邮箱
passwd = 'xxx' #就是上⾯的授权码
to= ['1508691067@qq'] #接受⽅邮箱
#设置邮件内容
#MIMEMultipart类可以放任何内容
msg = MIMEMultipart()
conntent="这个是字符串"
#把内容加进去
msg.attach(MIMEText(conntent,'plain','utf-8'))
#设置邮件主题
msg['Subject']="这个是邮件主题"
#发送⽅信息
msg['From']=msg_from
#开始发送
#通过SSL⽅式发送,服务器地址和端⼝
s = smtplib.SMTP_SSL("smtp.qq", 465)
# 登录邮箱
s.login(msg_from, passwd)
#开始发送
s.sendmail(msg_from,to,msg.as_string())
print("邮件发送成功")
⼆.发送携带附件的邮件
import smtplib
from import MIMEText
#发送多种类型的邮件
from email.mime.multipart import MIMEMultipart
msg_from = '1508691067@qq' # 发送⽅邮箱
passwd = 'xxxxx'
to= ['1508691067@qq'] #接受⽅邮箱
#设置邮件内容
#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", 465)
# 登录邮箱
s.login(msg_from, passwd)
#开始发送
s.sendmail(msg_from,to,msg.as_string())
print("邮件发送成功")
三.发送携带图⽚的附件
同理,可以使⽤上⾯的⽅法也可以发送图⽚附件
import smtplib
几号出梅from import MIMEText
#发送多种类型的邮件
from email.mime.multipart import MIMEMultipart
msg_from = '1508691067@qq' # 发送⽅邮箱
passwd = 'xxxxx'
to= ['1508691067@qq'] #接受⽅邮箱
#设置邮件内容
#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", 465)
# 登录邮箱
s.login(msg_from, passwd)
#开始发送
s.sendmail(msg_from,to,msg.as_string())
print("邮件发送成功")
四.发送 html 格式的邮件
import smtplib
from import MIMEText
#发送多种类型的邮件
from email.mime.multipart import MIMEMultipart
国标螺丝import datetime
msg_from = '1508691067@qq' # 发送⽅邮箱
passwd = 'xxxxxx'
to= ['1508691067@qq'] #接受⽅邮箱
#设置邮件内容
#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 = w()
year = ar
month = h
day = now_time.day
mytime = str(year) + " 年 " + str(month) + " ⽉ " + str(day) + " ⽇ "
fayanren="爱因斯坦"
zhuchiren="⽜顿"
#构造HTML
content = '''
<html>
<body>
<h1 align="center">这个是标题,xxxx通知</h1>
哪种牌子的消毒柜好<p><strong>您好:</strong></p>
<blockquote><p><strong>以下内容是本次会议的纪要,请查收!</strong></p></blockquote> <blockquote><p><strong>发⾔⼈:{fayanren}</strong></p></blockquote>
<blockquote><p><strong>主持⼈:{zhuchiren}</strong></p></blockquote>
压的多音字组词<p align="right">{mytime}</p>
<body>
<html>
'''.format(fayanren=fayanren, zhuchiren=zhuchiren, mytime=mytime)
msg.attach(MIMEText(content,'html','utf-8'))
#设置邮件主题
msg['Subject']="这个是邮件主题"
#发送⽅信息qq钻解冻
msg['From']=msg_from
#开始发送
#通过SSL⽅式发送,服务器地址和端⼝
s = smtplib.SMTP_SSL("smtp.qq", 465)
# 登录邮箱
s.login(msg_from, passwd)
#开始发送
s.sendmail(msg_from,to,msg.as_string())
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论