使用Python通過QQ郵箱發(fā)送電子郵件的示例代碼
準備工作
安裝必要的 Python 庫:
pip install secure-smtplib
獲取 QQ 郵箱的 SMTP 授權(quán)碼。你需要登錄到 QQ 郵箱,然后進入設置,找到 SMTP 服務設置,并生成授權(quán)碼。
示例代碼
下面是一個簡單的 Python 示例代碼:
import smtplib from email.mime.text import MIMEText from email.header import Header # QQ 郵箱 SMTP 服務器地址 smtp_server = 'smtp.qq.com' smtp_port = 465 # SSL 端口號 # 發(fā)件人和收件人郵箱 sender = 'your_email@qq.com' receiver = 'receiver_email@example.com' # QQ 郵箱 SMTP 授權(quán)碼 password = 'your_authorization_code' # 郵件內(nèi)容 subject = 'Hello, this is a test email' content = 'This is a test email sent from Python.' # 創(chuàng)建 MIMEText 對象 msg = MIMEText(content, 'plain', 'utf-8') msg['From'] = Header(sender) msg['To'] = Header(receiver) msg['Subject'] = Header(subject) # 發(fā)送郵件 try: server = smtplib.SMTP_SSL(smtp_server, smtp_port) server.login(sender, password) server.sendmail(sender, [receiver], msg.as_string()) print('Email sent successfully.') except Exception as e: print(f'Failed to send email: {e}') finally: server.quit()
運行代碼
保存上面的代碼到一個 Python 文件,然后運行它。如果一切設置正確,你應該能成功發(fā)送電子郵件。
這樣,你就可以使用 Python 通過 QQ 郵箱發(fā)送電子郵件了。希望這篇文章能幫助你!
到此這篇關(guān)于使用Python通過QQ郵箱發(fā)送電子郵件的示例代碼的文章就介紹到這了,更多相關(guān)Python發(fā)送電子郵件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中map,reduce,filter和sorted函數(shù)的使用方法
這篇文章主要介紹了Python中map,reduce,filter和sorted函數(shù)的使用方法,是Python入門學習中的基礎知識,需要的朋友可以參考下2015-08-08用Python創(chuàng)建簡易網(wǎng)站圖文教程
今天給大家?guī)淼氖顷P(guān)于Python的相關(guān)知識,文章圍繞著用Python創(chuàng)建簡易網(wǎng)站展開,文中有非常詳細的介紹及圖文示例,需要的朋友可以參考下2021-06-06django下創(chuàng)建多個app并設置urls方法
在本篇文章里小編給大家分享的是一篇關(guān)于django下創(chuàng)建多個app并設置urls方法,需要的朋友們可以參考學習下。2020-08-08Tensorflow 2.4 搭建單層和多層 Bi-LSTM 模型
這篇文章主要為大家介紹了Tensorflow 2.4 搭建單層 Bi-LSTM 模型和多層 Bi-LSTM 模型的實現(xiàn)過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01Python中使用pypdf2合并、分割、加密pdf文件的代碼詳解
這篇文章主要介紹了Python中使用pypdf2合并、分割、加密pdf文件的代碼,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05