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

python 七種郵件內(nèi)容發(fā)送方法實例

 更新時間:2014年04月22日 10:00:06   作者:  
這篇文章主要介紹了python 七種郵件內(nèi)容發(fā)送方法實例,需要的朋友可以參考下

一、文件形式的郵件

復制代碼 代碼如下:
#!/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('</pre>
<h1>你好</h1>
<pre>','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.
<img alt="" src="cid:image1" />
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', '')
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'

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

五、群郵件

復制代碼 代碼如下:
#!/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 = """\

 
Hi!

       How are you?

       Here is the <a >link</a> you wanted.

 

"""

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

相關文章

  • Pyecharts可視化圖片渲染的方法詳解

    Pyecharts可視化圖片渲染的方法詳解

    使用 pyecharts 渲染成圖片一直是開發(fā)者比較關心的功能,pyecharts提供了selenium、phantomjs和pyppeteer 三種方式。本文將具體介紹一下這三種方式的使用,需要的可以參考一下
    2022-02-02
  • Sklearn多種算法實現(xiàn)人臉補全的項目實踐

    Sklearn多種算法實現(xiàn)人臉補全的項目實踐

    本文主要介紹了Sklearn多種算法實現(xiàn)人臉補全的項目實踐,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-03-03
  • python 獲取字符串MD5值方法

    python 獲取字符串MD5值方法

    今天小編就為大家分享一篇python 獲取字符串MD5值方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • Python+numpy實現(xiàn)一個蜘蛛紙牌游戲

    Python+numpy實現(xiàn)一個蜘蛛紙牌游戲

    蜘蛛紙牌大家玩過沒有?之前的電腦上自帶的游戲,用他來摸魚過的舉個手。但是現(xiàn)在的電腦上已經(jīng)沒有蜘蛛紙牌了。所以本文就來用Python做一個吧,需要的可以參考一下
    2022-12-12
  • python利用文件時間批量重命名照片和視頻

    python利用文件時間批量重命名照片和視頻

    這篇文章主要為大家詳細介紹了python利用文件時間批量重命名照片和視頻,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-02-02
  • Python?3行代碼提取音樂高潮部分

    Python?3行代碼提取音樂高潮部分

    這篇文章主要介紹了利用Python代碼提取音樂高潮部分,文章圍毆繞Python代碼的相關詳情展開提取音樂的內(nèi)容,需要的小伙伴可以參考一下
    2022-01-01
  • Python異步與定時任務提高程序并發(fā)性和定時執(zhí)行效率

    Python異步與定時任務提高程序并發(fā)性和定時執(zhí)行效率

    Python異步與定時任務是Python編程中常用的兩種技術,異步任務可用于高效處理I/O密集型任務,提高程序并發(fā)性;定時任務可用于定時執(zhí)行計劃任務,提高程序的執(zhí)行效率。這兩種技術的應用有助于提升Python程序的性能和效率
    2023-05-05
  • 淺談python之自動化運維(Paramiko)

    淺談python之自動化運維(Paramiko)

    這篇文章主要介紹了淺談python之自動化運維(Paramiko),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-01-01
  • Python內(nèi)置函數(shù) next的具體使用方法

    Python內(nèi)置函數(shù) next的具體使用方法

    這篇文章主要介紹了Python內(nèi)置函數(shù) next的具體使用方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • python socket 超時設置 errno 10054

    python socket 超時設置 errno 10054

    這篇文章主要介紹了python 遠程主機強迫關閉了一個現(xiàn)有的連接 socket 超時設置 errno 10054 ,需要的朋友可以參考下
    2014-07-07

最新評論