【问题标题】:Change sender account using exchangelib使用 exchangelib 更改发件人帐户
【发布时间】:2020-05-20 18:24:09
【问题描述】:

我在 Outlook 'user1@example.com' 和 'user2@example.com' 上有两个帐户。我在 user1 草稿文件夹中有许多草稿,并希望在发送之前将每封电子邮件更新到 user2 地址,以便 user2 是电子邮件的发件人并出现在邮件项目的发件人字段中。

使用 exchangelib 我设法将“发件人”和“帐户”地址从 user1 更改为 user2(甚至 print(item.sender, item.account) 以验证更改),但更新并未反映 Outlook 字段中的电子邮件完成后草稿文件夹。

import getpass
from exchangelib import Configuration
from exchangelib import Credentials, Account
from exchangelib import FileAttachment, HTMLBody
from exchangelib.properties import DistinguishedFolderId


def authenticate():
    """
    Authenticate into mail.example.com
    """
    email = "user1@example.com"
    passwd = getpass.getpass(prompt="Enter your password: ")
    user_credentials = Credentials(email, passwd)
    config = Configuration(server="mail.example.com",
                           credentials=user_credentials)
    account = Account(primary_smtp_address=email, config=config,
                           credentials=user_credentials, autodiscover=False)
    return account

def main():
    """
     Change sender account to user2@example.com
    """
    user_account = authenticate()
    drafts = DistinguishedFolderId('drafts')
    for item in user_account.drafts.all().order_by('subject'):
        item.sender = 'user2@example.com'
        item.account = 'user2@example.com'
        user_account.drafts.save(update_fields=['sender', 'account'])
        exit("Done")

if __name__ == "__main__":
    main()

【问题讨论】:

    标签: python python-3.x outlook exchangelib


    【解决方案1】:

    您需要在项目而不是文件夹上调用.save()Folder.save() 用于更改文件夹本身的属性,例如文件夹名称。

    按照其他答案中的建议打印该项目只会告诉您该项目的本地副本已更改,而不是服务器上的实际项目。您需要致电 item.refresh() 以查看实际更新的内容(尽管在您致电 item.save() 时应该始终匹配)。

    最后,item.account 是对Account 对象的引用。不要改变那个。包含发件人信息的两个字段是item.senderitem.author,但item.sender是服务器自动设置的,不能更改。 item.author 可以更改,但只能在邮件仍为草稿时更改。这里是 exchangelib 中 Message-specific 字段定义的链接:https://github.com/ecederstrand/exchangelib/blob/3158c076a1e30a18e0b68e99a54fb14b3a6f7cd4/exchangelib/items/message.py#L18

    这是一个例子:

        for item in user_account.drafts.all().order_by('subject'):
            item.author = 'user2@example.com'
            item.save()
            item.refresh()  # This gets a fresh copy of the item on the server
            print(item)  # Now you see whatever the server has
    

    【讨论】:

    • 这个半作品,仍在测试中。它会更新邮件,不会反映在 Outlook 中,但是当我实际发送邮件时,它似乎有更新的地址。
    • Outlook 有时在获取对服务器上项目的更改时非常缓慢。可能该项目实际上已更改,但 Outlook 只是没有注意到。
    【解决方案2】:

    不是真正的解决方案,但您可以寻找:

    你可以这样做:

    for item in user_account.drafts.all().order_by('subject'):
        print(item) #Copy Text into Notepad++ and search for user1/ user1@example.com
        item.sender = 'user2@example.com'
        item.account = 'user2@example.com'
        user_account.drafts.save(update_fields=['sender', 'account'])
        print(item) #Copy Text in another Notepad++ tab to see if every user1 entry has been replaced
        exit("Done")
    

    您应该能够比较 .txt 文件并找到丢失的项目(如果有的话) 警告:根据邮件的数量,会有一个巨大的文字墙

    【讨论】:

    • 确认发件人和帐户已更新,但我的问题是它不会更改 Outlook 上的 from 字段。我确实注意到authormime_content 中仍然有user1。我使用item.authoritem.mime_content 对其进行了更新,但仍然有同样的问题...进一步研究...谢谢。
    猜你喜欢
    • 2010-09-17
    • 2017-10-04
    • 1970-01-01
    • 2023-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-30
    相关资源
    最近更新 更多