關於python3 傳送郵件

2022-09-06 03:12:13 字數 2585 閱讀 6408

from email.mime.text import

mimetext

from email.header import

header

from smtplib import

smtp_ssl

#qq伺服器

host_server = '

smtp.qq.com'#

sender_qq為發件人的qq號碼

sender_qq = '**

263357**

'#qpass

為qq郵箱的授權碼

qpass = '

iajfnsdabvjxh***'#

發件人的郵箱

sender_qq_mail = '**

2633573**@qq.com'#

收件人郵箱

receiver = '***

5228***@qq.com

'#傳送

郵件的正文內容

mail_content = '

郵箱測試

'#傳送

郵件標題

mail_title = '

python的郵件'#

ssl登入qq郵箱

smtp =smtp_ssl(host_server)

smtp.set_debuglevel(1)

smtp.ehlo(host_server)

smtp.login(sender_qq,qpass)

#傳送郵件

msg = mimetext(mail_content, "

plain

", '

utf-8')

msg[

"subject

"] = header(mail_title, '

utf-8')

msg[

"from

"] =sender_qq_mail

msg["to

"] =receiver

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

smtp.quit()

from email.mime.text import

mimetext

from email.mime.multipart import

mimemultipart

from email.header import

header

from smtplib import

smtp_ssl

#qq郵箱smtp伺服器

host_server = '

smtp.qq.com'#

sender_qq為發件人的qq號碼

sender_qq = '**

263357**

'#qpass

為qq郵箱的授權碼

qpass = '

iajfnsdabvjxh***'#

發件人的郵箱

sender_qq_mail = '**

263357**@qq.com'#

收件人郵箱

receiver = '***

6643***@qq.com'#

郵件的正文內容

mail_content = '

郵箱測試'#

郵件標題

mail_title = '

python的郵件'#

ssl登入

smtp =smtp_ssl(host_server)

smtp.set_debuglevel(1)

smtp.ehlo(host_server)

smtp.login(sender_qq,qpass)

msg=mimemultipart()

msg[

"subject

"] = header(mail_title, '

utf-8')

msg[

"from

"] =sender_qq_mail

msg["to

"] =receiver

msg.attach(mimetext(mail_content,

"plain

", '

utf-8'))

#附件,當前目錄下的 test.txt 檔案

att1 = mimetext(open('

test.txt

', '

rb').read(), '

base64

', '

utf-8')

att1[

"content-type

"] = ''#

這裡的filename可以任意寫

att1["

content-disposition

"] = '

attachment; filename="test.txt"

'msg.attach(att1)

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

smtp.quit()

Python3 傳送郵件踩坑記錄

新手學習python3.5,根據教程試一試使用python3.5中的smtplib模組傳送電子郵件。出現多個問題 1 error 無法傳送郵件.case 550,b user has no permission 和 error 無法傳送郵件.case 535,b error authenticati...

利用Python3傳送郵件 親測可行

1 修改pip源為國內源頭mkdir pip vim pip pip.conf 寫入下面的內容 global index url install trusted host mirrors.aliyun.com2 安裝發郵件的包pip3 install pyemail3 郵箱配置開啟smtp 進入郵箱...

python3 傳送任意檔案郵件的例項

例項如下所示 usr bin python coding utf 8 import smtplib import email.mime.multipart import email.mime.text from email.mime.text import mimetext from email.m...