Python 傳送郵件

2021-10-02 11:21:22 字數 2217 閱讀 6429

import smtplib

from email.mime.multipart import mimemultipart

from email.mime.text import mimetext

import os

class

mailsender

:def

__init__

(self, address, password)

: self.address = address

self.password = password

self.server = address[address.index(

"@")+1

:]# 傳送文字

defsend_text

(self, receiver, subject, text)

: mail = mimetext(text)

mail[

'subject'

]= subject

mail[

'from'

]= self.address

# 此步可新增多個接收人,只需不同接收人逗號隔開

mail[

'to'

]= receiver

smtp = smtplib.smtp(

"smtp."

+ self.server, port=25)

smtp.login(self.address, self.password)

smtp.sendmail(self.address, receiver, mail.as_string())

smtp.quit(

)# 傳送檔案

defsend_file

(self, receiver, subject, path)

: mail = mimemultipart(

) mail[

'subject'

]= subject

mail[

'from'

]= self.address

mail[

'to'

]= receiver

# 郵件正文內容

mail.attach(mimetext(

"檔案"

,'plain'

,'utf-8'))

att = mimetext(

open

(path,

'rb'

).read(),

'base64'

,'utf-8'

) att[

"content-type"]=

# 附件名稱

att[

"content-disposition"]=

"attachment; filename=\""

+ os.path.split(path)[1

]+"\""

att[

"name"

]= os.path.split(path)[1

]# 此步可新增多個附件

mail.attach(att)

smtp = smtplib.smtp(

"smtp."

+ self.server, port=25)

smtp.login(self.address, self.password)

smtp.sendmail(self.address, receiver, mail.as_string())

smtp.quit(

)

from mailsender import mailsender

mail_sender = mailsender(

"[email protected]"

,"*********"

)print

("傳送文字"

)mail_sender.send_text(

"[email protected]"

,"傳送文字"

,"文字"

)print

("傳送檔案"

)mail_sender.send_file(

"[email protected]"

,"傳送檔案"

,"e:\\activation.jar"

)

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...