[英]How to extract the date of the received emails in Outlook using Python scripting?
I am new to Python and I developed a Python script to ping messages in an application called Spark. 我是Python的新手,我开发了Python脚本来在名为Spark的应用程序中ping消息。 So far, the code is fine and I managed to generate the subject of the emails.
到目前为止,代码还不错,我设法生成了电子邮件的主题。 But I also want to add the date when I received the email along with the subject.
但是,我也想添加收到电子邮件的日期以及主题。 Here is the script :-
这是脚本:-
import win32com.client
import requests
import time
import datetime
def postMessageInSpark(mytoken, roomId, text):
header = {'Authorization':mytoken, 'Content-Type':'application/json'}
payload = {'roomId':roomId,'text':text}
result = requests.post(url='https://api.ciscospark.com/v1/messages',headers=header,json=payload)
print(str(result.status_code))
return str(result.status_code)
token ="Bearer "+'OGU3MDcyMmYtZDUzYS00OWU4LTk0ZTItYTJlNTc1OTUzODA5ZmQzYmRkNjMtYzcy'
teamId='Y2lzY29zcGFyazovL3VzL1JPT00vNGZjYWI5NzAtZGNhMS0xMWU2LWE3ODItYTM1OTY2OWNkMzcx'
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
folders = inbox.Folders
for folder in folders:
if (folder.Name == 'TES FOLDER'):
items = folder.Items
for item in items:
subject=item.Subject
message=item.GetLast()
date=message.senton.date()
for idx in reversed(items):
fail_msg=subject+date
postMessageInSpark(token,teamId,fail_msg)
break
I used date=message.senton.date()
but it doesn't return me the date of the emails. 我使用
date=message.senton.date()
但它没有返回电子邮件日期。 Please help me. 请帮我。
Hi I found a solution to my problem:- 嗨,我找到了解决我的问题的方法:
import win32com.client
import requests
import time
import datetime
def postMessageInSpark(mytoken, roomId, text):
header = {'Authorization':mytoken, 'Content-Type':'application/json'}
payload = {'roomId':roomId,'text':text}
result = requests.post(url='https://api.ciscospark.com/v1/messages',headers=header,json=payload)
print(str(result.status_code))
return str(result.status_code)
token ="Bearer "+'OGU3MDcyMmYtZDUzYS00OWU4LTk0ZTItYTJlNTc1OTUzODA5ZmQzYmRkNjMtYzcy'
teamId='Y2lzY29zcGFyazovL3VzL1JPT00vNGZjYWI5NzAtZGNhMS0xMWU2LWE3ODItYTM1OTY2OWNkMzcx'
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
folders = inbox.Folders
for folder in folders:
if (folder.Name == 'TES FOLDER'):
items = folder.Items
for item in reversed(items):
subject=item.Subject
date = item.SentOn.strftime("%d-%m-%y")
for idx in reversed(items):
fail_msg=subject+" at "+date
postMessageInSpark(token,teamId,fail_msg)
idx.Move(inbox.Folders("TEST"))
break
Actually the message=item.GetLast()
and date=message.senton.date()
was only giving me the output of the last email received. 实际上,
message=item.GetLast()
和date=message.senton.date()
仅给了我上一封收到的电子邮件的输出。 But after I used date = item.SentOn.strftime("%d-%m-%y")
it gave me the output I desired. 但是在我使用
date = item.SentOn.strftime("%d-%m-%y")
它给了我所需的输出。
MailItem
object does not have a method called GetLast
. MailItem
对象没有名为GetLast
的方法。 Only Items
object does. 只有
Items
对象可以。 Did you mean to use item.SentOn
property? 您是要使用
item.SentOn
属性吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.