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

詳解python 發(fā)送郵件實(shí)例代碼

 更新時(shí)間:2016年12月22日 11:08:20   作者:lonelycatcher  
本篇文章主要介紹了python 發(fā)送郵件實(shí)例代碼,詳細(xì)的介紹了各種方式發(fā)送郵件,包括文件形式的郵件、HTML形式的郵件、帶圖片的HTML郵件等,有興趣的可以了解一下。

python 發(fā)送郵件實(shí)例

文件形式的郵件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from emailmimetext import MIMEText 
from emailheader import Header 
 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtpcom' 
username = '***' 
password = '***' 
 
msg = MIMEText('你好','text','utf-8')#中文需參數(shù)‘utf-8',單字節(jié)字符不需要 
msg['Subject'] = Header(subject, 'utf-8') 
 
smtp = smtplibSMTP() 
smtpconnect('smtpcom') 
smtplogin(username, password) 
smtpsendmail(sender, receiver, msgas_string()) 
smtpquit() 

HTML形式的郵件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from emailmimetext import MIMEText 
 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtpcom' 
username = '***' 
password = '***' 
 
msg = MIMEText('<html><h1>你好</h1></html>','html','utf-8') 
 
msg['Subject'] = subject 
 
smtp = smtplibSMTP() 
smtpconnect('smtpcom') 
smtplogin(username, password) 
smtpsendmail(sender, receiver, msgas_string()) 
smtpquit() 

帶圖片的HTML郵件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from emailmimemultipart import MIMEMultipart 
from emailmimetext import MIMEText 
from emailmimeimage import MIMEImage 
 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtpcom' 
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') 
msgRootattach(msgText) 
 
fp = open('h:\\python\\jpg', 'rb') 
msgImage = MIMEImage(fpread()) 
fpclose() 
 
msgImageadd_header('Content-ID', '<image1>') 
msgRootattach(msgImage) 
 
smtp = smtplibSMTP() 
smtpconnect('smtpcom') 
smtplogin(username, password) 
smtpsendmail(sender, receiver, msgRootas_string()) 
smtpquit() 

帶附件的郵件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from emailmimemultipart import MIMEMultipart 
from emailmimetext import MIMEText 
from emailmimeimage import MIMEImage 
 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtpcom' 
username = '***' 
password = '***' 
 
msgRoot = MIMEMultipart('related') 
msgRoot['Subject'] = 'test message' 
 
#構(gòu)造附件 
att = MIMEText(open('h:\\python\\jpg', 'rb')read(), 'base64', 'utf-8') 
att["Content-Type"] = 'application/octet-stream' 
att["Content-Disposition"] = 'attachment; filename="jpg"' 
msgRootattach(att) 
     
smtp = smtplibSMTP() 
smtpconnect('smtpcom') 
smtplogin(username, password) 
smtpsendmail(sender, receiver, msgRootas_string()) 
smtpquit() 

群郵件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from emailmimetext import MIMEText 
 
sender = '***' 
receiver = ['***','****',……] 
subject = 'python email test' 
smtpserver = 'smtpcom' 
username = '***' 
password = '***' 
 
msg = MIMEText('你好','text','utf-8') 
 
msg['Subject'] = subject 
 
smtp = smtplibSMTP() 
smtpconnect('smtpcom') 
smtplogin(username, password) 
smtpsendmail(sender, receiver, msgas_string()) 
smtpquit() 

各種元素都包含的郵件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from emailmimemultipart import MIMEMultipart 
from emailmimetext import MIMEText 
from emailmimeimage import MIMEImage 
 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtpcom' 
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://wwwpythonorg" 
html = """\ 
<html> 
 <head></head> 
 <body> 
  <p>Hi!<br> 
    How are you?<br> 
    Here is the <a href="http://wwwpythonorg">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 
msgattach(part1) 
msgattach(part2) 
#構(gòu)造附件 
att = MIMEText(open('h:\\python\\jpg', 'rb')read(), 'base64', 'utf-8') 
att["Content-Type"] = 'application/octet-stream' 
att["Content-Disposition"] = 'attachment; filename="jpg"' 
msgattach(att) 
   
smtp = smtplibSMTP() 
smtpconnect('smtpcom') 
smtplogin(username, password) 
smtpsendmail(sender, receiver, msgas_string()) 
smtpquit() 

基于SSL的郵件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from emailmimetext import MIMEText 
from emailheader import Header 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtpcom' 
username = '***' 
password = '***' 
 
msg = MIMEText('你好','text','utf-8')#中文需參數(shù)‘utf-8',單字節(jié)字符不需要 
msg['Subject'] = Header(subject, 'utf-8') 
 
smtp = smtplibSMTP() 
smtpconnect('smtpcom') 
smtpehlo() 
smtpstarttls() 
smtpehlo() 
smtpset_debuglevel(1) 
smtplogin(username, password) 
smtpsendmail(sender, receiver, msgas_string()) 
smtpquit()  

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

相關(guān)文章

  • python 根據(jù)時(shí)間來(lái)生成唯一的字符串方法

    python 根據(jù)時(shí)間來(lái)生成唯一的字符串方法

    今天小編就為大家分享一篇python 根據(jù)時(shí)間來(lái)生成唯一的字符串方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • pygame游戲之旅 調(diào)用按鈕實(shí)現(xiàn)游戲開(kāi)始功能

    pygame游戲之旅 調(diào)用按鈕實(shí)現(xiàn)游戲開(kāi)始功能

    這篇文章主要為大家詳細(xì)介紹了pygame游戲之旅的第12篇,教大家調(diào)用按鈕實(shí)現(xiàn)游戲開(kāi)始功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • python字典get()方法用法分析

    python字典get()方法用法分析

    這篇文章主要介紹了python字典get()方法用法,實(shí)例分析了使用get方法獲取字典值的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04
  • Python制作動(dòng)態(tài)字符畫的源碼

    Python制作動(dòng)態(tài)字符畫的源碼

    python字符畫是一個(gè)簡(jiǎn)單有趣的圖畫,它一般由程序制作而成,接下來(lái)通過(guò)本文給大家分享Python制作動(dòng)態(tài)字符畫的源碼,需要的朋友可以參考下
    2021-08-08
  • 一百多行python代碼實(shí)現(xiàn)搶票助手

    一百多行python代碼實(shí)現(xiàn)搶票助手

    一百多行python代碼輕松實(shí)現(xiàn)搶票助手,十一出行不再愁!本文具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-09-09
  • Python2.x中文亂碼問(wèn)題解決方法

    Python2.x中文亂碼問(wèn)題解決方法

    這篇文章主要介紹了Python2.x中文亂碼問(wèn)題解決方法,本文解釋問(wèn)題原因、給出了處理辦法并講解了編碼解碼的一些知識(shí),需要的朋友可以參考下
    2015-06-06
  • PyCharm導(dǎo)入numpy庫(kù)的幾種方式

    PyCharm導(dǎo)入numpy庫(kù)的幾種方式

    今天給大家?guī)?lái)的是關(guān)于Python的相關(guān)知識(shí),文章圍繞著PyCharm導(dǎo)入numpy庫(kù)的幾種方式展開(kāi),文中有非常詳細(xì)的解釋及代碼示例,需要的朋友可以參考下
    2021-06-06
  • 淺談Django QuerySet對(duì)象(模型.objects)的常用方法

    淺談Django QuerySet對(duì)象(模型.objects)的常用方法

    這篇文章主要介紹了淺談Django QuerySet對(duì)象(模型.objects)的常用方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • Python列表切片常用操作實(shí)例解析

    Python列表切片常用操作實(shí)例解析

    這篇文章主要介紹了Python列表切片常用操作實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • python合并文本文件示例

    python合并文本文件示例

    這篇文章主要介紹了python合并文本文件示例,需要的朋友可以參考下
    2014-02-02

最新評(píng)論