python傳送郵件

2021-09-01 11:47:59 字數 3808 閱讀 3823

例項1:

#!/usr/bin/python

# -*- coding: utf-8 -*-

import smtplib

from email.mime.text import mimetext

from email.header import header

sender = '[email protected]'

receivers = ['[email protected]'] # 接收郵件,可設定為你的qq郵箱或者其他郵箱

# 三個引數:第乙個為文字內容,第二個 plain 設定文字格式,第三個 utf-8 設定編碼

message = mimetext('python 郵件傳送測試...', 'plain', 'utf-8')

message['from'] = header("菜鳥教程", 'utf-8') # 傳送者

message['to'] = header("測試", 'utf-8') # 接收者

subject = 'python smtp 郵件測試'

message['subject'] = header(subject, 'utf-8')

try:

smtpobj = smtplib.smtp('localhost')

smtpobj.sendmail(sender, receivers, message.as_string())

print "郵件傳送成功"

except smtplib.smtpexception:

print "error: 無法傳送郵件"

例項2:

import smtplib

from email.mime.text import mimetext

from email.header import header

# 第三方 smtp 服務

mail_host="smtp.***.com" #設定伺服器

mail_user="***x" #使用者名稱

mail_pass="******" #口令

sender = '[email protected]'

receivers = ['[email protected]'] # 接收郵件,可設定為你的qq郵箱或者其他郵箱

message = mimetext('python 郵件傳送測試...', 'plain', 'utf-8')

message['from'] = header("菜鳥教程", 'utf-8')

message['to'] = header("測試", 'utf-8')

subject = 'python smtp 郵件測試'

message['subject'] = header(subject, 'utf-8')

try:

smtpobj = smtplib.smtp()

smtpobj.connect(mail_host, 25) # 25 為 smtp 埠號

smtpobj.login(mail_user,mail_pass)

smtpobj.sendmail(sender, receivers, message.as_string())

print "郵件傳送成功"

except smtplib.smtpexception:

print "error: 無法傳送郵件"

例項3:傳送html格式郵件

# !/usr/bin/python

# -*- coding: utf-8 -*-

import smtplib

from email.mime.text import mimetext

from email.header import header

sender = '[email protected]'

receivers = ['[email protected]'] # 接收郵件,可設定為你的qq郵箱或者其他郵箱

mail_msg = """

python 郵件傳送測試...

這是乙個鏈結

"""message = mimetext(mail_msg, 'html', 'utf-8')

message['from'] = header("菜鳥教程", 'utf-8')

message['to'] = header("測試", 'utf-8')

subject = 'python smtp 郵件測試'

message['subject'] = header(subject, 'utf-8')

try:

smtpobj = smtplib.smtp('localhost')

smtpobj.sendmail(sender, receivers, message.as_string())

print "郵件傳送成功"

except smtplib.smtpexception:

print "error: 無法傳送郵件"

例項4:傳送帶有附件的郵件

# !/usr/bin/python

# -*- coding: utf-8 -*-

import smtplib

from email.mime.text import mimetext

from email.mime.multipart import mimemultipart

from email.header import header

sender = '[email protected]'

receivers = ['[email protected]'] # 接收郵件,可設定為你的qq郵箱或者其他郵箱

#建立乙個帶附件的例項

message = mimemultipart()

message['from'] = header("菜鳥教程", 'utf-8')

message['to'] = header("測試", 'utf-8')

subject = 'python smtp 郵件測試'

message['subject'] = header(subject, 'utf-8')

#郵件正文內容

message.attach(mimetext('這是菜鳥教程python 郵件傳送測試……', 'plain', 'utf-8'))

# 構造附件1,傳送當前目錄下的 test.txt 檔案

att1 = mimetext(open('test.txt', 'rb').read(), 'base64', 'utf-8')

# 這裡的filename可以任意寫,寫什麼名字,郵件中顯示什麼名字

att1["content-disposition"] = 'attachment; filename="test.txt"' message.attach(att1)

# 構造附件2,傳送當前目錄下的 runoob.txt 檔案

att2 = mimetext(open('runoob.txt', 'rb').read(), 'base64', 'utf-8')

att2["content-disposition"] = 'attachment; filename="runoob.txt"'

message.attach(att2)

try:

smtpobj = smtplib.smtp('localhost')

smtpobj.sendmail(sender, receivers, message.as_string())

print "郵件傳送成功"

except smtplib.smtpexception:

print "error: 無法傳送郵件"

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

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

python 傳送郵件

coding utf 8 import smtplib from email.mime.text import mimetext from email.header import header 檔案形式的郵件 def email file mail host smtp.qq.com 郵箱伺服器 ma...

python 傳送郵件

smtp mail transfer protocol 即簡單郵件傳輸協議,它是一組用於由源位址到目的位址傳送郵件的規則,由它來控制信件的中轉方式。python的smtplib提供了一種很方便的途徑傳送電子郵件。它對smtp協議進行了簡單的封裝。直接貼 coding utf 8 import smt...