javamail 解码 base64 html格式邮件_python使用QQ邮箱实现自动发送邮件
生活随笔
收集整理的這篇文章主要介紹了
javamail 解码 base64 html格式邮件_python使用QQ邮箱实现自动发送邮件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近用到Python自動發送郵件,主要就是三步,登錄郵件、寫郵件內容、發送,用到的庫是 smtplib 和 email,直接使用pip安裝即可
我使用的是QQ郵箱,首先需要設置QQ郵箱POP3/SMTP服務
記住這個授權碼,這個授權碼就是Python腳本中登錄郵箱時的密碼,而不是你平時登錄郵箱時的那個密碼
一.發送普通文本郵件
#發送多種類型的郵件 from email.mime.multipart import MIMEMultipartmsg_from = '1508691067@qq.com' # 發送方郵箱 passwd = 'xxx' #就是上面的授權碼to= ['1508691067@qq.com'] #接受方郵箱#設置郵件內容 #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("郵件發送成功")二.發送攜帶附件的郵件
import smtplib from email.mime.text import MIMEText #發送多種類型的郵件 from email.mime.multipart import MIMEMultipartmsg_from = '1508691067@qq.com' # 發送方郵箱 passwd = 'xxxxx'to= ['1508691067@qq.com'] #接受方郵箱#設置郵件內容 #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("郵件發送成功")三.發送攜帶圖片的附件
同理,可以使用上面的方法也可以發送圖片附件
import smtplib from email.mime.text import MIMEText #發送多種類型的郵件 from email.mime.multipart import MIMEMultipartmsg_from = '1508691067@qq.com' # 發送方郵箱 passwd = 'xxxxx'to= ['1508691067@qq.com'] #接受方郵箱#設置郵件內容 #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("郵件發送成功")四.發送 html 格式的郵件
import smtplib from email.mime.text import MIMEText #發送多種類型的郵件 from email.mime.multipart import MIMEMultipart import datetime msg_from = '1508691067@qq.com' # 發送方郵箱 passwd = 'xxxxxx'to= ['1508691067@qq.com'] #接受方郵箱#設置郵件內容 #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 = '''<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']="這個是郵件主題"#發送方信息 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("郵件發送成功")以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。
總結
以上是生活随笔為你收集整理的javamail 解码 base64 html格式邮件_python使用QQ邮箱实现自动发送邮件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python杨辉三角_yiduobo的每
- 下一篇: 新手一小时就写出人工智能应用 - 看图识