python學習之三 郵件傳送

2021-08-08 02:37:26 字數 2697 閱讀 7122

python學習之三 郵件傳送

此**經過驗證可用

單個郵件傳送demo
# -*- coding:utf-8 -*-

import smtplib

from email.mime.text import mimetext

from email.header import header

# 郵箱方面需要設定 pop3/smtp/imap 許可權

#傳送郵箱伺服器

smtpserver = 'smtp.163.com'

#傳送郵箱使用者/密碼

user = '******[email protected]'

password = '******'

#傳送人郵箱

sender = '***[email protected]'

#收件人郵箱

receiver = '******@qq.com'

#郵件主題

subject ='python_test_email'

defsendemail

():try:

# 編寫html型別的郵件正文

msg = mimetext('你好!

', 'html', 'utf-8')

# 用於郵箱顯示標題、發件人、收件人

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

msg['from'] = sender

msg['to'] = receiver

#連線並傳送郵件

smtp = smtplib.smtp()

smtp.connect(smtpserver)

smtp.login(user, password)

smtp.sendmail(sender, receiver, msg.as_string())

smtp.quit()

except exception as msg:

print msg

sendemail()

多個郵件傳送
# -*- coding:utf-8 -*-

import smtplib

from email.mime.text import mimetext

from email.header import header

class

send_email

():def

__init__

(self, user, password, sender, s,subject, emailtext, emailtextformat):

self.user = user

self.password = password

self.sender = sender

self.receivers = receivers

self.subject = subject

self.emailtext = emailtext

self.emailtextformat = emailtextformat

defsend

(self):

try:

# 編寫html型別的郵件正文

msg = mimetext(self.emailtext, emailtextformat, 'utf-8')

# 用於郵箱顯示標題、發件人、收件人

msg['subject'] = header(self.subject, 'utf-8')

msg['from'] = self.sender

receivers = ','.join(self.receivers)

msg['to'] = receivers

# 連線並傳送郵件

smtp = smtplib.smtp()

smtp.connect(smtpserver)

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

smtp.sendmail(self.sender, self.receivers, msg.as_string())

smtp.quit()

print

"傳送成功!"

except exception as msgerror:

print msgerror

#傳送郵箱伺服器

smtpserver = 'smtp.126.com'

#傳送郵箱使用者/密碼

user = '******@126.com'

password = '*******'

#傳送人郵箱

sender = 'yj***[email protected]'

#收件人郵箱

receivers = ['568****[email protected]', '*****@163.com']

#郵件主題

subject = 'python_test_email7'

#郵件正文

emailtext = '

'#郵件正文格式

emailtextformat='html'

send_email(user, password, sender, receivers, subject, emailtext, emailtextformat).send()

python 傳送郵件學習

import smtplib from email.mime.text import mimetext 傳送文字檔案 from email.utils import formataddr 郵箱位址和口令 sender addr 14 xx qq.com 發件人郵箱 password x 這裡的密碼是...

python 郵件傳送學習

author ouyang test time 2021 1 20 15 01 import smtplib from email.header import header from email.mime.text import mimetext def send mail sender,passw...

郵件學習 python傳送郵件分析

首先呼叫python傳送乙個郵件 usr bin python3 import smtplib from email.mime.text import mimetext from email.utils import formataddr my sender 111111111 qq.com 發件人...