我正在使用 exchangelib 连接以交换和回复电子邮件。但是在发送回复时它不支持附件。
根据 this answer我必须“创建一个普通的邮件项目,它有一个‘Re: some subject’标题,包含附件,并在需要时引用原始邮件。”
但我不确定如何“引用”原始消息
我正在使用以下代码回复:
从路径库导入路径 从 exchangelib 导入消息、帐户、文件附件
account = Account(...)
item = ...
file_to_attach = Path('/file/to/attach.txt')
message = Message(
account=account,
subject="Re: " + item.subject,
body="This is reply by code",
cc_recipients=item.cc_recipients,
to_recipients=[item.sender],
in_reply_to=item.id,
conversation_id=item.conversation_id,
)
with file_to_attach.open('rb') as f:
content = f.read()
message.attach(FileAttachment(name=file_to_attach.name, content=content))
message.send_and_save()
它发送带附件的电子邮件,但不保留原始邮件中的回复文本,看起来是一封新邮件而不是回复。也不会在 gmail 中显示为对话
我可能在这里遗漏了一些小东西。请建议如何解决这个问题
请您参考如下方法:
在花了更多时间寻找解决方案后,我找到了 this在 C# 中回答,使用它我能够实现以下解决方案:
attachment = FileAttachment(name=file_name, content=f.read())
reply = item.create_reply("Re: " + item.subject, "THIS IS REPLY FROM CODE" )
msg = reply.save(account.drafts)
msg.attach(attachment)
msg.send()
希望这可以帮助其他人寻找类似问题的解决方案。
