Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用exchangelib更改发件人帐户_Python_Python 3.x_Outlook_Exchangelib - Fatal编程技术网

Python 使用exchangelib更改发件人帐户

Python 使用exchangelib更改发件人帐户,python,python-3.x,outlook,exchangelib,Python,Python 3.x,Outlook,Exchangelib,我在outlook上有两个帐户user1@example.com“还有”user2@example.com'. 我在user1草稿文件夹中有许多草稿,希望在发送之前将每封电子邮件更新到user2地址,以便user2是电子邮件的发件人,并显示在邮件项目的发件人字段中 使用我成功地将“发件人”和“帐户”地址从user1更改为user2(甚至打印(item.sender,item.account)以验证更改),但更新完成后不会反映在outlook草稿文件夹的“电子邮件发件人”字段上 import ge

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

使用我成功地将“发件人”和“帐户”地址从user1更改为user2(甚至
打印(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()

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

你可以做:

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")
您应该能够比较.txts并找到缺少的项(如果有)
警告:根据邮件数量的不同,将有一堵巨大的文本墙

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

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

最后,
item.account
是对
account
对象的引用。不要改变这一点。包含发件人信息的两个字段是
item.sender
item.author
,但
item.sender
由服务器自动设置,不能更改<代码>项目。作者可以更改,但只能在邮件仍为草稿时更改。以下是exchangelib中消息特定字段定义的链接:

下面是一个例子:

用于用户帐户中的项目。草稿。全部()。订购人('subject'):
item.author=user2@example.com'
item.save()
item.refresh()#这将在服务器上获取项目的新副本
打印(项目)#现在您可以看到服务器上的任何内容

确认发件人和帐户已更新,但我的问题是它没有更改outlook上的“发件人”字段。我确实注意到
作者
mime\u内容
中仍然有user1。我使用
item.author
item.mime\u content
对其进行了更新,但仍然存在相同的问题…进一步研究…谢谢。此半成品仍在测试中。它会更新邮件,不会反映在outlook中,但当我实际发送邮件时,它似乎有更新的地址。outlook有时在接收服务器上项目的更改时非常缓慢。这可能是因为该项目实际上已更改,但Outlook没有注意到。