傳送混合郵件

2021-10-25 03:27:03 字數 1703 閱讀 1601

我們平時需要使用 python 傳送各類郵件,這個需求怎麼來實現?答案其實很簡單,smtplib 和 email庫可以幫忙實現這個需求。smtplib 和 email 的組合可以用來傳送各類郵件:普通文字,html 形式,帶附件,**郵件,帶的郵件等等。我們這裡將會分幾節把傳送郵件功能解釋完成。

smtplib 是 python 用來傳送郵件的模組,email 是用來處理郵件訊息。

傳送郵件系列最後一篇將會介紹傳送混合郵件:裡面包含附件,html 形式,不同文字:

import smtplib

from email.mime.multipart import mimemultipart

from email.mime.text import mimetext

sender = '***'

receiver = '***'

subject = 'python email test'

smtpserver = 'smtp.163.com'

username = '***'

password = '***'

# create message container - the correct mime type is multipart/alternative.

msg = mimemultipart('mixed')

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:\n"

html = """\

hi! how are you?

here is the link 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)

# 構造附件

smtp.quit()

注意:這裡的**並沒有把異常處理加入,需要讀者自己處理異常。

html傳送郵件 Python傳送郵件(三十)

簡單郵件傳輸協議 smtp 是一種協議,用於在郵件伺服器之間傳送電子郵件和路由電子郵件。python提供smtplib模組,該模組定義了乙個smtp客戶端會話物件,可用於使用smtp或esmtp偵聽器守護程式向任何網際網路機器傳送郵件。這是乙個簡單的語法,用來建立乙個smtp物件,稍後將演示如何用它...

C 傳送郵件

今天俺學習c 傳送郵件的方法 在命名空間system.web.mail 傳送電子郵件主要用到了二個物件 乙個是mailmessage物件,此物件主要是封裝電子郵件的各個屬性,即所謂的發信人,收信人,信件的主題,信件的內容和信件的附件等。另外乙個是 tpmail物件,這個物件的最大作用是把已經定義好各...

C 傳送郵件

今天作乙個小專案,其中有個提醒功能,需要簡訊和郵件。因此查詢了一下c 傳送郵件,原來在學習計算機網路時了解到,其實就是實現 tp協議和pop3協議,但是自己有沒有伺服器,於是利用126的郵箱製作。如下 mailmessage message new mailmessage message.from ...