【问题标题】:Replying To email via Outlook in Python在 Python 中通过 Outlook 回复电子邮件
【发布时间】:2016-07-20 00:49:49
【问题描述】:

我正在尝试使用主题中的关键字回复电子邮件,但我需要通过 Outlook 完成所有操作。我当前的代码运行良好,但如果它可以直接回复而不是创建新消息会更好。

希望这是问这个问题的正确地方:)

    outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
    o = win32com.client.Dispatch("Outlook.Application")

    inbox = outlook.GetDefaultFolder(6)    
    def check_mail():
        global message
        messages = inbox.Items
        message = messages.GetLast()
        if (message.subject.find('@Bot') != -1 and message.unread and whtlist.find(message.SenderName)!= -1 ):
            return 1
        else:
            return 0

    def Read_mail():
        global message
        global ACTIVE
        body_content = message.body
        print(bcolors.WARNING+'\n______________________________________________________________________\n'+bcolors.OKGREEN)
        print (body_content)
        print(bcolors.WARNING+'\n______________________________________________________________________\n'+bcolors.OKGREEN)
        for att in message.Attachments:
            break
        try:
            att.SaveAsFile(os.getcwd() + '\\new.xlsx')
            print(os.getcwd())
        except :
            print(bcolors.WARNING+'No Attachment Found'+bcolors.OKGREEN)
        message.unread=False
        Msg = o.CreateItem(0)
        Msg.To = message.SenderEmailAddress
        print(bcolors.FAIL+'Reply sent to: {}'.format(message.SenderEmailAddress+bcolors.OKGREEN))
        Msg.Subject = 'Autoreply'
        Msg.Body = 'I see you {}.\n\nTesting\n-Bot'.format(message.SenderName)
        Msg.Send()

【问题讨论】:

标签: python email outlook


【解决方案1】:

不要使用Items.GetLast - 不能保证返回最后收到的电子邮件。除非您明确调用 Items.Sort,否则 Items 集合不会以任何特定方式排序。在您的特定情况下,使用Items.Restrict 或Items.Find/FindNext 根据您的条件(未读、主题包含特定单词等)查找邮件。

要获得回复,请调用 MailItem.Reply 而不是 Application.CreateItem - 它将返回新的 MailItem 对象。

【讨论】:

  • 谢谢,我确定我已经尝试过“message.Reply()”,但现在可以完美运行了 :)
猜你喜欢
  • 2011-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-19
  • 1970-01-01
  • 2015-10-27
  • 2012-03-03
  • 1970-01-01
相关资源
最近更新 更多