最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python郵件發(fā)送smtplib使用詳解

 更新時間:2020年06月16日 14:59:40   作者:johnny  
這篇文章主要為大家詳細介紹了python郵件發(fā)送smtplib的使用方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了python郵件發(fā)送smtplib使用具體代碼,供大家參考,具體內(nèi)容如下

文件形式的郵件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.text import MIMEText 
from email.header import Header 
 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
username = '***' 
password = '***' 
 
msg = MIMEText('你好','text','utf-8')#中文需參數(shù)‘utf-8',單字節(jié)字符不需要 
msg['Subject'] = Header(subject, 'utf-8') 
 
smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msg.as_string()) 
smtp.quit() 

HTML形式的郵件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.text import MIMEText 
 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
username = '***' 
password = '***' 
 
msg = MIMEText('<html><h1>你好</h1></html>','html','utf-8') 
 
msg['Subject'] = subject 
 
smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msg.as_string()) 
smtp.quit() 

帶圖片的HTML郵件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 
from email.mime.image import MIMEImage 
 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
username = '***' 
password = '***' 
 
msgRoot = MIMEMultipart('related') 
msgRoot['Subject'] = 'test message' 
 
msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>good!','html','utf-8') 
msgRoot.attach(msgText) 
 
fp = open('h:\\python\\1.jpg', 'rb') 
msgImage = MIMEImage(fp.read()) 
fp.close() 
 
msgImage.add_header('Content-ID', '<image1>') 
msgRoot.attach(msgImage) 
 
smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msgRoot.as_string()) 
smtp.quit() 

帶附件的郵件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 
from email.mime.image import MIMEImage 
 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
username = '***' 
password = '***' 
 
msgRoot = MIMEMultipart('related') 
msgRoot['Subject'] = 'test message' 
 
#構(gòu)造附件 
att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8') 
att["Content-Type"] = 'application/octet-stream' 
att["Content-Disposition"] = 'attachment; filename="1.jpg"' 
msgRoot.attach(att) 
  
smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msgRoot.as_string()) 
smtp.quit() 

群發(fā)郵件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.text import MIMEText 
 
sender = '***' 
receiver = ['***','****',……] 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
username = '***' 
password = '***' 
 
msg = MIMEText('你好','text','utf-8') 
 
msg['Subject'] = subject 
 
smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msg.as_string()) 
smtp.quit() 

各種元素都包含的郵件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 
from email.mime.image import MIMEImage 
 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
username = '***' 
password = '***' 
 
# Create message container - the correct MIME type is multipart/alternative. 
msg = MIMEMultipart('alternative') 
msg['Subject'] = "Link" 
 
# Create the body of the message (a plain-text and an HTML version). 
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org" 
html = """\ 
<html> 
 <head></head> 
 <body> 
 <p>Hi!<br> 
 How are you?<br> 
 Here is the <a  rel="external nofollow" >link</a> you wanted. 
 </p> 
 </body> 
</html> 
""" 
 
# Record the MIME types of both parts - text/plain and text/html. 
part1 = MIMEText(text, 'plain') 
part2 = MIMEText(html, 'html') 
 
# Attach parts into message container. 
# According to RFC 2046, the last part of a multipart message, in this case 
# the HTML message, is best and preferred. 
msg.attach(part1) 
msg.attach(part2) 
#構(gòu)造附件 
att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8') 
att["Content-Type"] = 'application/octet-stream' 
att["Content-Disposition"] = 'attachment; filename="1.jpg"' 
msg.attach(att) 
 
smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msg.as_string()) 
smtp.quit() 

基于SSL的郵件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.text import MIMEText 
from email.header import Header 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
username = '***' 
password = '***' 
 
msg = MIMEText('你好','text','utf-8')#中文需參數(shù)‘utf-8',單字節(jié)字符不需要 
msg['Subject'] = Header(subject, 'utf-8') 
 
smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.ehlo() 
smtp.starttls() 
smtp.ehlo() 
smtp.set_debuglevel(1) 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msg.as_string()) 
smtp.quit() 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python 怎樣將dataframe中的字符串日期轉(zhuǎn)化為日期的方法

    python 怎樣將dataframe中的字符串日期轉(zhuǎn)化為日期的方法

    這篇文章主要介紹了python 怎樣將dataframe中的字符串日期轉(zhuǎn)化為日期的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Python字符串的拆分與連接詳解

    Python字符串的拆分與連接詳解

    由于字符串?dāng)?shù)據(jù)幾乎無處不在,因此掌握有關(guān)字符串的交易工具非常重要。幸運的是,Python 使字符串操作變得非常簡單,尤其是與其他語言甚至舊版本的 Python 相比時。本文將為大家詳細介紹Python中字符串的拆分與連接,需要的可以參考一下
    2021-12-12
  • Python3實現(xiàn)的畫圖及加載圖片動畫效果示例

    Python3實現(xiàn)的畫圖及加載圖片動畫效果示例

    這篇文章主要介紹了Python3實現(xiàn)的畫圖及加載圖片動畫效果,結(jié)合實例形式分析了Python3基于tkinter庫進行圖片加載動畫效果的相關(guān)實現(xiàn)與使用技巧,需要的朋友可以參考下
    2018-01-01
  • flask框架實現(xiàn)修改密碼和免密登錄功能

    flask框架實現(xiàn)修改密碼和免密登錄功能

    flask是python web開發(fā)的常用框架之一。本文將講述flask如何實現(xiàn)修改密碼和免密登錄功能
    2021-05-05
  • Django發(fā)送郵件功能實例詳解

    Django發(fā)送郵件功能實例詳解

    在本篇文章里小編給大家整理了關(guān)于Django發(fā)送郵件功能的詳細內(nèi)容,有需要的朋友們可以參考學(xué)習(xí)下。
    2019-09-09
  • python 創(chuàng)建一維的0向量實例

    python 創(chuàng)建一維的0向量實例

    今天小編就為大家分享一篇python 創(chuàng)建一維的0向量實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • Python中關(guān)于matplotlib圖片的灰度處理方式

    Python中關(guān)于matplotlib圖片的灰度處理方式

    這篇文章主要介紹了Python中關(guān)于matplotlib圖片的灰度處理方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • python os模塊在系統(tǒng)管理中的應(yīng)用

    python os模塊在系統(tǒng)管理中的應(yīng)用

    這篇文章主要介紹了python os模塊在系統(tǒng)管理中的應(yīng)用,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • 詳解Python下載圖片并保存本地的兩種方式

    詳解Python下載圖片并保存本地的兩種方式

    這篇文章主要介紹了Python下載圖片并保存本地的兩種方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • python非對稱加密算法RSA實現(xiàn)原理與應(yīng)用詳解

    python非對稱加密算法RSA實現(xiàn)原理與應(yīng)用詳解

    RSA加密算法是一種非對稱加密算法,RSA算法的安全性基于大數(shù)分解的困難性,即已知兩個大素數(shù)p和q的乘積n,求解p和q非常困難,RSA算法廣泛應(yīng)用于數(shù)據(jù)加密和數(shù)字簽名等領(lǐng)域,本文將詳細介紹如何在Python中使用RSA算法進行加密和解密,需要的朋友可以參考下
    2024-09-09

最新評論

嘉定区| 临夏县| 秀山| 乡宁县| 兴文县| 敦煌市| 鄢陵县| 赫章县| 吕梁市| 东丰县| 吉安县| 嘉兴市| 四平市| 拜城县| 乌海市| 定边县| 乌兰县| 昭平县| 安陆市| 株洲市| 玉山县| 广昌县| 土默特左旗| 孟连| 琼中| 巧家县| 类乌齐县| 宁陵县| 东阿县| 石景山区| 鸡泽县| 潼关县| 黄石市| 锡林郭勒盟| 津南区| 长葛市| 墨玉县| 双牌县| 阿瓦提县| 濮阳县| 泾源县|