欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

本文實例為大家分享了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開發(fā)SQLite3數(shù)據(jù)庫相關(guān)操作詳解【連接,查詢,插入,更新,刪除,關(guān)閉等】

    Python開發(fā)SQLite3數(shù)據(jù)庫相關(guān)操作詳解【連接,查詢,插入,更新,刪除,關(guān)閉等】

    這篇文章主要介紹了Python開發(fā)SQLite3數(shù)據(jù)庫相關(guān)操作,結(jié)合實例形式較為詳細(xì)的分析了Python操作SQLite3數(shù)據(jù)庫的連接,查詢,插入,更新,刪除,關(guān)閉等相關(guān)操作技巧,需要的朋友可以參考下
    2017-07-07
  • 利用python實時刷新基金估值效果(摸魚小工具)

    利用python實時刷新基金估值效果(摸魚小工具)

    這篇文章主要介紹了利用python實時刷新基金估值(摸魚小工具),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • PYTHON EVAL的用法及注意事項解析

    PYTHON EVAL的用法及注意事項解析

    這篇文章主要介紹了PYTHON EVAL的用法及注意事項解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-09-09
  • Python下簡易的單例模式詳解

    Python下簡易的單例模式詳解

    這篇文章主要介紹了Python下簡易的單例模式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Python自定義scrapy中間模塊避免重復(fù)采集的方法

    Python自定義scrapy中間模塊避免重復(fù)采集的方法

    這篇文章主要介紹了Python自定義scrapy中間模塊避免重復(fù)采集的方法,實例分析了Python實現(xiàn)采集的技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • Python實現(xiàn)修改文件內(nèi)容的方法分析

    Python實現(xiàn)修改文件內(nèi)容的方法分析

    這篇文章主要介紹了Python實現(xiàn)修改文件內(nèi)容的方法,結(jié)合實例形式分析了Python文件讀寫、字符串替換及shell方法調(diào)用等相關(guān)操作技巧,需要的朋友可以參考下
    2018-03-03
  • Django 多表關(guān)聯(lián) 存儲 使用方法詳解 ManyToManyField save

    Django 多表關(guān)聯(lián) 存儲 使用方法詳解 ManyToManyField save

    今天小編就為大家分享一篇Django 多表關(guān)聯(lián) 存儲 使用方法詳解 ManyToManyField save,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • 分享一個python的aes加密代碼

    分享一個python的aes加密代碼

    這篇文章主要介紹了分享一個python的aes加密代碼,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-12-12
  • Scrapy框架爬取Boss直聘網(wǎng)Python職位信息的源碼

    Scrapy框架爬取Boss直聘網(wǎng)Python職位信息的源碼

    今天小編就為大家分享一篇關(guān)于Scrapy框架爬取Boss直聘網(wǎng)Python職位信息的源碼,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • python文件處理詳解

    python文件處理詳解

    這篇文章主要介紹了Python 處理文件的幾種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-10-10

最新評論