Python實現傳送郵件

2021-07-22 12:25:56 字數 1876 閱讀 7831

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

#引入相關的模組

import smtplib

from email.mime.text import mimetext

from email.mime.image import mimeimage

from email.header import header

from email.mime.multipart import mimemultipart

#傳送給以下兩個郵箱,有列表儲存

mailto_list=["******@qq.com","******@qq.com"]

#郵箱伺服器,126郵箱是smtp.126.com

mail_host="smtp.126.com"

#發件人郵箱

mail_user="[email protected]"

#發件人郵箱密碼

mail_pass="123456"

#封裝成乙個函式

defmail_send

(to_list,content):

try:

#伺服器名,smtp預設埠號25

server=smtplib.smtp(mail_host,25)

#登陸server.login(mail_user,mail_pass)

message=mimemultipart()

#正文msg=mimetext(content,"plain","utf-8")

message.attach(msg)

#附件文件

subject="python smtp 郵件測試"

message["subject"]=header(subject,"utf8").encode()

to_addr="******@qq.com"

message["to"]=header(u"******@qq.com")

server.sendmail("[email protected]",to_list,message.as_string())

server.close()

return

true

except exception,e:

print str(e)

return

false

content="hello,my name is jack,this is send by python program!"

if __name__=="__main__":

if mail_send(mailto_list,content):

print

"傳送成功!"

else:

print

"傳送失敗!"

python 實現傳送郵件

可採用email模組傳送電子郵件附件。傳送乙個未知mime型別的檔案附件其基本思路如下 1.構造mimemultipart物件做為根容器 2.構造mimetext物件做為郵件顯示內容並附加到根容器 3.構造mimebase物件做為檔案附件內容並附加到根容器 a.讀入檔案內容並格式化 b.設定附件頭 ...

python實現傳送郵件

有時我們需要程式在執行出現問題時傳送郵件通知我們,在這裡寫了乙個使用qq傳送的python指令碼,也是綜合了網上的資源 然而網上的案例好像都不能用 搞得,當然只要把主機埠啥的改一下就能使用其他郵箱了,如下 usr bin python coding utf 8 import smtplib from...

python傳送郵件實現

send mail python 傳送郵件的模組 usr bin env python coding utf 8 匯入smtplib和mimetext import smtplib from email.mime.text import mimetext 要發給誰 mailto list qq.co...