加入所有现有课件
This commit is contained in:
9
Labs/Lab2/email_config.json
Normal file
9
Labs/Lab2/email_config.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"sender": "jingfan.ke@qq.com",
|
||||
"receiver": "jingfan.ke@qq.com",
|
||||
"subject": "SMTP 邮件测试",
|
||||
"body": "这是一封测试邮件,发送自Python程序。",
|
||||
"smtp_server": "smtp.qq.com",
|
||||
"imap_url": "imap.qq.com",
|
||||
"password": "dbeucjitqdczebad"
|
||||
}
|
||||
207
Labs/Lab2/emails.json
Normal file
207
Labs/Lab2/emails.json
Normal file
File diff suppressed because one or more lines are too long
62
Labs/Lab2/receiver.py
Normal file
62
Labs/Lab2/receiver.py
Normal file
@@ -0,0 +1,62 @@
|
||||
import imaplib
|
||||
import email
|
||||
import json
|
||||
from email.header import decode_header
|
||||
|
||||
# 从JSON文件中加载配置信息
|
||||
with open('email_config.json', 'r') as config_file:
|
||||
config = json.load(config_file)
|
||||
|
||||
user = config['sender']
|
||||
password = config['password']
|
||||
imap_url = config['imap_url']
|
||||
|
||||
# 连接到IMAP服务器
|
||||
mail = imaplib.IMAP4_SSL(imap_url)
|
||||
mail.login(user, password)
|
||||
mail.select('inbox')
|
||||
|
||||
# 搜索所有邮件
|
||||
result, data = mail.search(None, 'ALL')
|
||||
mail_ids = data[0]
|
||||
|
||||
id_list = mail_ids.split()
|
||||
id_list.reverse()
|
||||
emails = []
|
||||
|
||||
# 遍历邮件ID
|
||||
for i in id_list:
|
||||
result, data = mail.fetch(i, '(RFC822)')
|
||||
raw_email = data[0][1]
|
||||
raw_email_string = raw_email.decode('utf-8',errors="ignore")
|
||||
email_message = email.message_from_string(raw_email_string)
|
||||
|
||||
# 解析邮件内容
|
||||
mail_from = email_message['From']
|
||||
mail_subject = decode_header(email_message['Subject'])[0][0]
|
||||
if isinstance(mail_subject, bytes):
|
||||
mail_subject = mail_subject.decode('utf-8')
|
||||
mail_body = ''
|
||||
if email_message.is_multipart():
|
||||
for part in email_message.walk():
|
||||
ctype = part.get_content_type()
|
||||
cdispo = str(part.get('Content-Disposition'))
|
||||
if ctype == 'text/plain' and 'attachment' not in cdispo:
|
||||
mail_body = part.get_payload(decode=True).decode('utf-8')
|
||||
break
|
||||
else:
|
||||
mail_body = email_message.get_payload(decode=True).decode('utf-8')
|
||||
|
||||
emails.append({'From': mail_from, 'Subject': mail_subject, 'Body': mail_body})
|
||||
|
||||
# 保存邮件列表到JSON文件
|
||||
with open('emails.json', 'w') as outfile:
|
||||
json.dump(emails, outfile, indent=4, ensure_ascii=False)
|
||||
|
||||
# 打印最后一封邮件的信息
|
||||
if emails:
|
||||
print("From: ", emails[0]['From'])
|
||||
print("Subject: ", emails[0]['Subject'])
|
||||
print("Body: ", emails[0]['Body'])
|
||||
|
||||
mail.logout()
|
||||
31
Labs/Lab2/sender.py
Normal file
31
Labs/Lab2/sender.py
Normal file
@@ -0,0 +1,31 @@
|
||||
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)
|
||||
Reference in New Issue
Block a user