共計 864 個字符,預計需要花費 3 分鐘才能閱讀完成。
使用 Python 發送郵件,可以使用 smtplib 庫。下面是一個示例代碼:
python
import smtplib
from email.mime.text import MIMEText
# 設置郵件內容
msg = MIMEText(' 這是一封測試郵件 ', 'plain', 'utf-8')
msg['From'] = 'sender@example.com' # 發件人郵箱
msg['To'] = 'recipient@example.com' # 收件人郵箱
msg['Subject'] = ' 測試郵件 ' # 郵件主題
# 發送郵件
smtp_server = 'smtp.example.com' # SMTP 服務器 地址
smtp_port = 25 # SMTP 服務器端口
smtp_username = 'sender@example.com' # SMTP 服務器用戶名
smtp_password = 'password' # SMTP 服務器密碼
try:
smtp_obj = smtplib.SMTP(smtp_server, smtp_port)
smtp_obj.login(smtp_username, smtp_password)
smtp_obj.sendmail(msg['From'], msg['To'], msg.as_string())
print(' 郵件發送成功 ')
except smtplib.SMTPException as e:
print(' 郵件發送失敗:', e)
請注意,需要將示例代碼中的 郵件服務器 地址、端口、發件人郵箱、收件人郵箱、用戶名和密碼替換為實際的信息。
丸趣 TV 網 – 提供最優質的資源集合!