如果是本地客户端邮件,安装win32com可实现调用
网页版使用smtplib、email调用
本文收发使用的是办公常会用到outlook,通过win32com调用
import win32com.client as win32
#调用outlook.application,启动进程
outlook = win32com.client.Dispatch('outlook.application')
#创建一个item,即邮件
mail = outlook.CreateItem()
mail.to = '收件人'
mail.cc = '抄送人'
mail.Subject = '这是一个邮件的主题'
mail.Body = '这是一个邮件的内容'
mail.Attachment.Add("这是附件的路径")
mail.send
import re
from win32com.client.gencache import EnsureDispatch as Dispatch
import pandas as pd
def read_email(date,my_account):
#读取邮件
outlook = Dispatch("outlook.Application")
mapi = outlook.GetNamespace("MAPI")
Accounts = mapi.Folders
#读取账号
names = []
#读取内容
contents = []
c=['Root_Directory_Name_1', 'Level_1_FolderName_1', 'Level_2_FolderName_1', 'ReceivedTime_1', 'SenderName_1', 'to_to_1', 'cc_cc_1', 'Subject_1', 'MessageID_1', 'ConversationTopic_1', 'ConversationID_1', 'ConversationIndex_1', 'EmailBody_1']
c_names=['账户名称','文件一级目录','文件二级目录','接收日期','发件人','收件人','抄送人','主题','邮件MessageID','会话主题','会话ID','会话记录相对位置','邮件内容']
df = pd.DataFrame(columns = c)
for Account_Name in Accounts:
# 只查找需要的邮箱账号信息
if Account_Name.Name==my_account:
Level_1_Names = Account_Name.Folders
for Level_1_Name in Level_1_Names:
# 只需要收件箱的邮件
if Level_1_Name.Name=='收件箱':
Mail_1_Messages = Level_1_Name.Items
# 将邮件按日期排序,可减少遍历内容
Mail_1_Messages.Sort("[ReceivedTime]", True)
for xx in Mail_1_Messages: # xx = 'mail' # 开始查看单个邮件的信息
Root_Directory_Name_1 = Account_Name.Name
Level_1_FolderName_1 = Level_1_Name.Name
Level_2_FolderName_1 = ''
if (hasattr(xx, 'ReceivedTime')):
ReceivedTime_1 = str(xx.ReceivedTime)[:10] # 接收日期
else:
ReceivedTime_1 = ''
if ReceivedTime_1 < date: #只要特定日期的邮件
break
if (hasattr(xx, 'SenderName')): # 发件人
SenderName_1 = xx.SenderName
else:
SenderName_1 = ''
# if SenderName_1 !=sent_account: #只要特定发件人的邮件
# continue
if (hasattr(xx, 'Subject')): # 主题
Subject_1 = xx.Subject
else:
Subject_1 = ''
if (hasattr(xx, 'Body')): # 邮件内容
EmailBody_1 = xx.Body
else:
EmailBody_1 = ''
if (hasattr(xx, 'To')): # 收件人
to_to_1 = xx.To
else:
to_to_1 = ''
if (hasattr(xx, 'CC')): # 抄送人
cc_cc_1 = xx.CC
else:
cc_cc_1 = ''
if (hasattr(xx, 'EntryID')): # 邮件MessageID
MessageID_1 = xx.EntryID
else:
MessageID_1 = ''
if (hasattr(xx, 'ConversationTopic')): # 会话主题
ConversationTopic_1 = xx.ConversationTopic
else:
ConversationTopic_1 = ''
if (hasattr(xx, 'ConversationID')): # 会话ID
ConversationID_1 = xx.ConversationID
else:
ConversationID_1 = ''
if (hasattr(xx, 'ConversationIndex')): # 会话记录相对位置
ConversationIndex_1 = xx.ConversationIndex
else:
ConversationIndex_1 = ''
data = [Root_Directory_Name_1, Level_1_FolderName_1, Level_2_FolderName_1, ReceivedTime_1, SenderName_1, to_to_1, cc_cc_1, Subject_1, MessageID_1, ConversationTopic_1, ConversationID_1, ConversationIndex_1, EmailBody_1]
dataf=dict(zip(c,data))
df = df.append(dataf,ignore_index=True)
df.columns = c_names
return df