Python:如何通过 IMAP 将带有 BCC 收件人的草稿电子邮件存储到 Exchange Server?

发布于 2024-07-16 23:07:43 字数 1996 浏览 8 评论 0 原文

我尝试通过 IMAP 将草稿电子邮件存储到在 MS Exchange 上运行的文件夹中。 一切正常,除了密件抄送收件人不会显示在服务器上存储的草稿消息中。 如果我使用 MS Outlook 发送电子邮件,密件抄送收件人也不会收到该电子邮件。 如果我将消息存储在服务器上后用 Python 读回,我可以在草稿中看到密件抄送。

以下 Python 代码重现了此行为:

import imaplib 
import time 
from email.MIMEMultipart import MIMEMultipart 
from email.MIMEText import MIMEText 

message = MIMEMultipart() 
message['Subject'] = 'Test Draft' 
message['From'] = '[email protected]' 
message['to'] = '[email protected]' 
message['cc'] = '[email protected]' 
message['bcc'] = '[email protected]' 
message.attach(MIMEText('This is a test.\n')) 

server= imaplib.IMAP4('the.ser.ver.ip') 
server.login('test', 'test') 
server.append("Drafts" 
              ,'\Draft' 
              ,imaplib.Time2Internaldate(time.time()) 
              ,str(message)) 
server.logout() 

如果我运行此代码,草稿将存储到 Exchange Server 上的 Draft 文件夹中。 但是,如果我使用 MS Outlook 查看草稿,它不包括 bcc 收件人 (message['bcc'] = '[电子邮件受保护]')。 Messagetofromcc 好的,没有错误。

如果我从 Exchange 文件夹下载已包含密件抄送的草稿,我也可以看到密件抄送。 仅上传对我来说不起作用。

非常感谢任何帮助。 谢谢。 顺便说一句,MAPI 不是一个选项。

更新X-Receiver 不适合我。 至于在 Outlook 中使用 IMAP 文件夹,我得到了一个有趣的结果。 如果我通过 Outlook 中的 IMAP 文件夹访问草稿,我会看到密件抄送。 但如果我通过 MAPI 文件夹访问它,我就看不到它。 将对此进行一些尝试。

结论:实际上,代码运行得很好。 请参阅下面我找到的答案。

I try to store a draft e-mail via IMAP to a folder running on MS Exchange. Everything ok, except that Bcc recipients don't get shown in the draft message stored on the server. Bcc recipients also don't receive the email if I send it with MS Outlook. If I read the message back with Python after I have stored it on the server, I can see the Bcc in the draft.

The following Python code reproduces this behavior:

import imaplib 
import time 
from email.MIMEMultipart import MIMEMultipart 
from email.MIMEText import MIMEText 

message = MIMEMultipart() 
message['Subject'] = 'Test Draft' 
message['From'] = '[email protected]' 
message['to'] = '[email protected]' 
message['cc'] = '[email protected]' 
message['bcc'] = '[email protected]' 
message.attach(MIMEText('This is a test.\n')) 

server= imaplib.IMAP4('the.ser.ver.ip') 
server.login('test', 'test') 
server.append("Drafts" 
              ,'\Draft' 
              ,imaplib.Time2Internaldate(time.time()) 
              ,str(message)) 
server.logout() 

If I run this code, a draft gets stored into the Draft folder on the Exchange Server. But if I look at the draft with MS Outlook, it does not include the bcc recipient (message['bcc'] = '[email protected]'). Message, to, from, cc ok, no error.

If I download drafts that already include a bcc from an Exchange folder, I can also see the bcc. Only uploading doesn't work for me.

Any help very much appreciated. Thanks. BTW, MAPI is not an option.

Update: X-Receiver didn't work for me. As for playing around with an IMAP-Folder in Outlook, I got an interesting result. If I access the draft via the IMAP-Folder in Outlook, I see the bcc. But if I access it via the MAPI-Folder, I don't see it. Will play a little bit around with that.

Conclusion: Actually, the code works just fine. See below for the answer that I found.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白色秋天 2024-07-23 23:07:43

事实上,代码运行得很好。 它会创建包含所有正确标头(包括密件抄送)的正确邮件。

邮件客户端如何显示密件抄送?

邮件客户端 (例如在我的例子中通过 IMAP 或 MAPI 的 Python 或 MS Outlook)决定是否以及如何显示密件抄送标头。 例如,Outlook 不显示 IMAP 文件夹中的密件抄送标头。 这是一项隐藏密件抄送收件人的功能,其中密件抄送收件人之前未从邮件中删除(标准中不清楚是否允许一个密件抄送收件人查看所有其他密件抄送收件人,请参阅 维基百科)。

发送电子邮件时谁处理密件抄送?

假设现在我们已经在邮件客户端中起草了一条消息并将其存储在 IMAP 或 MAPI 文件夹中。 提供 IMAP/MAPI 文件夹的服务器保持草稿消息不变。 发送邮件时密件抄送标头发生的情况取决于实现,并且可能取决于邮件客户端和 邮件传输代理(例如我的例子中的MS Exchange Server)。 简而言之,人们不同意邮件客户端或邮件传输代理是否负责删除密件抄送标头。 然而,似乎大多数开发人员都认为邮件传输代理不接触邮件是邮件客户端的事(例如MS Exchange、MS SMTP、Exim、OpenWave)。 在这种情况下,邮件传输代理会将电子邮件发送至 RCPT TO: 中定义的收件人。 ="nofollow noreferrer">SMTP 通信,否则电子邮件保持不变。 然而,其他一些邮件传输代理会从电子邮件中删除密件抄送标头(例如 sendmail、Lotus Notes)。 可以在 Exim 邮件列表中找到非常全面的讨论,起始 这里

对于 MS Outlook 和 MS Exchange,MS Outlook 从不发送密件抄送(而是为每个密件抄送收件人发送单独的电子邮件),并且 MS Exchange 不会触及电子邮件标头,而是将完整电子邮件(可能包括密件抄送收件人)发送到定义的收件人在RCPT TO:中。

结论

我不明白密件抄送没有保证的行为,并且通常由客户端处理密件抄送。 我将重写我的 Python 代码以循环密件抄送收件人并为每个密件抄送收件人生成一封电子邮件。

Actually, the code works just fine. It creates the proper mail with all the right headers including bcc.

How does the mail client display bcc?

The mail client (e.g. Python or MS Outlook via IMAP or MAPI in my case) decides whether and how to display bcc-headers. Outlook for example doesn't display bcc headers from an IMAP folder. This is a feature to hide bcc recipients from each other where they have not been stripped away from the mail before (it is not clear from the standard whether one bcc recipient is allowed to see all other bcc recipients or not, see Wikipedia).

Who handles bcc upon sending an email?

Suppose now that we have drafted a message in a mail client and stored it in an IMAP or MAPI folder. The server providing the IMAP / MAPI folders leaves the draft message unchanged. What happens to the bcc-headers upon sending the mail is implementation dependent, and might depend both on the mail client and the mail transfer agent (e.g. MS Exchange Server in my case). In a nutshell, people do not agree whether the mail client or the mail transfer agent is reponsible for removing bcc headers. It seems however to be the case that a majority of developers is of the opinion that it is the mail client's business with the mail transfer agent not touching the mail (e.g. MS Exchange, MS SMTP, Exim, OpenWave). In this case, the mail transfer agent sends the email to the recipient as defined in the RCPT TO: of the SMTP communication, and leaves the email unchanged otherwise. Some other mail transfer agents strip bcc headers from emails however (e.g. sendmail, Lotus Notes). A very thorough discussion can be found on the Exim mailing list starting here.

In the case of MS Outlook and MS Exchange, MS Outlook never sends bcc (but sends individual emails for each bcc recipient) and MS Exchange does not touch the email headers, but sends the full email (possibly including bcc recipients) to the recipients defined in RCPT TO:.

Conclusion

I did not understand that there is no guaranteed behavior for bcc, and that usually the client handles bcc. I will rewrite my Python code to loop over bcc recipients and generate one email for each bcc recipient.

兰花执着 2024-07-23 23:07:43

设计上可能就是这样。 毕竟,密件抄送的全部意义在于收件人彼此之间是隐藏的。

我知道您没有发送电子邮件,只是存储它。 但我的猜测是,当邮件被 IMAP.appended 到文件夹时,Exchange 的内部规则就会启动,导致密件抄送字段被删除。

显然,当使用 Outlook 将邮件保存到文件夹时,密件抄送字段不会被删除。 但我猜想 Outlook 使用某种内部机制(MAPI?)与 Exchange 进行通信。

以上一切都只是猜测。

您可以尝试一些有趣的事情:

  • 在空的 Outlook/MAPI 配置文件中,创建一个 IMAP 帐户。 将其设置为在 Exchange 服务器上存储草稿和已发送邮件。
  • 查看使用 IMAP 的 Outlook 是否可以正确保存草稿的密件抄送。

我使用通过 IMAP 连接到 Exchange 的 Evolution 电子邮件客户端尝试了上述操作。 使用 Outlook(以正常方式连接),然后我查看了草稿和已发送项目。 两个地方都缺少密件抄送字段。

我相信这支持了我的理论。

It could be that way by design. After all, the whole point of bcc is that the recipients are hidden from each other.

I understand that you are not sending the e-mail, just storing it. But my guess is that Exchange's internal rules kick in when the message is IMAP.appended to the folder, causing the bcc field to be stripped away.

Obviously, when messages are saved to a folder using Outlook the bcc field is not stripped away. But I guess outlook communicates with Exchange using some internal mechanizm (MAPI?).

All the above is just guesswork.

Something fun you could try:

  • In an empty Outlook/MAPI profile, create a IMAP account. Set it up to store Drafts and Sent Items on the Exchange server.
  • See if outlook using IMAP can save bcc of Drafts correctly.

I tried the above using the Evolution e-mail client connected to Exchange over IMAP. Using outlook (connected the normal way), I then had a look in Drafts and Sent Items. The bcc field was missing in both places.

I belive this supports my theory.

删除→记忆 2024-07-23 23:07:43

尝试设置此备用版本的 BCC 标头:

X-Receiver: [email protected]

在您发送该标头时,Exchange 会将其视为 BCC。 但我敢打赌,当您通过 IMAP 写入时,它不会删除它。 您可以通过复制这一行来包含多个密件抄送收件人。

显然,这是一次彻底的黑客攻击。

Try setting this alternate version of the BCC header:

X-Receiver: [email protected]

Exchange in particular will treat this like a BCC when you send it. But I bet it will not strip it when you write via IMAP. You can include more than one BCC recipient by duplicating this line.

This is a complete hack, obviously.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文