- 准备
pycharm中先把自身所需要的库更新完全
pip install pipreqs
pipreqs . --encoding=utf-8
pip install -r requirements.txt
- 读邮件
SMTP_SERVER = 'coremail.xxxx.com.cn' # 更改为你的SMTP服务器
def read_mail(folder = 'inbox', USERNAME = USERNAME, PASSWORD = PASSWORD):
"""
这个函数用于读取指定收件箱的最近一封邮件
"""
mail = imaplib.IMAP4_SSL(IMAP_SERVER)
mail.login(USERNAME, PASSWORD)
if folder != 'inbox':
folder_utf7 = imapclient.imap_utf7.encode('量化部')
folder = f"INBOX/{folder_utf7.decode('utf-8')}"
mail.select(folder)
result, data = mail.uid('search', None, "ALL")
latest_email_uid = data[0].split()[-1]
result, email_data = mail.uid('fetch', latest_email_uid, '(BODY.PEEK[HEADER.FIELDS (SUBJECT FROM)])')
raw_email = email_data[0][1].decode("utf-8")
email_message = email.message_from_string(raw_email)
subject = decode_header(email_message['Subject'])[0][0]
if isinstance(subject, bytes):
# 如果是bytes类型,需要进行解码
subject = subject.decode()
from_ = decode_header(email_message['From'])[0][0]
if isinstance(from_, bytes):
from_ = from_.decode()
print('From :', from_)
print('Subject :', subject)
3.写邮件
网友评论