py發送email

import smtplib
from email.mime.text import MIMEText

# SMTP服務器設置
smtp_server = 'smtp.qq.com'
smtp_port = 587
secure_connection = 'STARTTLS'

# 發件人和收件人信息
sender_email = '[email protected]'
receiver_email = '[email protected]'

# 郵件內容
message = MIMEText('This is the body of the email.')
message['Subject'] = 'Subject of the email 2'
message['From'] = sender_email
message['To'] = receiver_email

# SMTP賬戶信息
username = '[email protected]'
password = '密碼'

# 連接到SMTP服務器併發送郵件
try:
    with smtplib.SMTP(smtp_server, smtp_port) as server:
        #if secure_connection == 'STARTTLS':
        #    server.starttls()
        server.login(username, password)
        server.sendmail(sender_email, receiver_email, message.as_string())
        print('Email sent successfully!')
except smtplib.SMTPException as e:
    print('Error sending email:', str(e))

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章