程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2024-11(2)

python 读取outlook邮箱邮件

发布于2019-08-07 13:29     阅读(560)     评论(0)     点赞(5)     收藏(4)


参考:Python3读取Outlook邮件并写入MySQL - 知乎

查看特定时间,特定发件人,特定邮箱账号的邮件,写入pandas Datafrme 格式

import re
from win32com.client.gencache import EnsureDispatch as Dispatch
import pandas as pd
def read_email(date,my_account,sent_account):    
	# 读取邮箱
	outlook = Dispatch("Outlook.Application")
	mapi = outlook.GetNamespace("MAPI")
	Accounts = mapi.Folders
	# 读取的员工账号
	names =[]
	# 短信内容
	contents=[]
	# 读取邮件存入pandas
	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']
	df = pd.DataFrame(columns=c)    
	for Account_Name in Accounts:
	    # 只查找需要的邮箱账号信息
	    if Account_Name.Name==my_account:        
	#         print(' >> 正在查询的帐户名称:',Account_Name.Name,'\n')
	        Level_1_Names = Account_Name.Folders
	        for Level_1_Name in Level_1_Names:
	#             只需要收件箱的邮件
	            if Level_1_Name.Name=='收件箱':
	    #             print(' - 正在查询一级目录:' , 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)[:-6]  # 接收时间
	                        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)

    
    return df
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92


所属网站分类: 技术文章 > 博客

作者:无敌是多么寂寞

链接:https://www.pythonheidong.com/blog/article/11224/c0a91766c1b8b7344c0a/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

5 0
收藏该文
已收藏

评论内容:(最多支持255个字符)