32 lines
928 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import smtplib
import json
from email.mime.text import MIMEText
from email.header import Header
# 从JSON文件中加载邮件配置
with open('email_config.json', 'r') as config_file:
config = json.load(config_file)
sender = config['sender']
receiver = config['receiver']
subject = config['subject']
body = config['body']
smtp_server = config['smtp_server']
password = config['password']
# 创建MIMEText对象设置邮件内容
message = MIMEText(body, 'plain', 'utf-8')
message['From'] = Header(sender)
message['To'] = Header(receiver)
message['Subject'] = Header(subject, 'utf-8')
try:
# 连接SMTP服务器并发送邮件
server = smtplib.SMTP_SSL(smtp_server, 465) # 使用465端口
server.login(sender, password)
server.sendmail(sender, [receiver], message.as_string())
print("邮件发送成功")
server.quit()
except smtplib.SMTPException as e:
print("邮件发送失败", e)