亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

python 發(fā)送郵件實例

文件形式的郵件

#!/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' 
 
#構造附件 
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) 
#構造附件 
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()  

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

相關文章

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

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

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

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

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

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

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

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

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

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

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

    Python2.x中文亂碼問題解決方法

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

    PyCharm導入numpy庫的幾種方式

    今天給大家?guī)淼氖顷P于Python的相關知識,文章圍繞著PyCharm導入numpy庫的幾種方式展開,文中有非常詳細的解釋及代碼示例,需要的朋友可以參考下
    2021-06-06
  • 淺談Django QuerySet對象(模型.objects)的常用方法

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

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

    Python列表切片常用操作實例解析

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

    python合并文本文件示例

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

最新評論