使用 Python 中的 imaplib 在 Gmail 中创建草稿邮件

发布于 2024-12-06 04:13:49 字数 1469 浏览 1 评论 0原文

我想编写一个 python 模块,将数据发送到 G 邮件帐户中的草稿消息。大约两周前我写了一个脚本,使用 imaplib 完美运行。我的模块的简化示例如下。 (我已经为任何人创建了一个测试电子邮件地址来测试此脚本。)

import imaplib
import time
conn = imaplib.IMAP4_SSL('imap.gmail.com', port = 993)
conn.login('[email protected]', '123456aaa')
conn.select('[Gmail]/Drafts')
conn.append("[Gmail]/Drafts", '', imaplib.Time2Internaldate(time.time()), "TEST")

它使用了 .append 函数,但今天当我运行该模块时,它会产生以下错误:

Traceback (most recent call last):
  File "C:/Windows/System32/email_append_test.py", line 6, in <module>
    conn.append("[Gmail]/Drafts", '', imaplib.Time2Internaldate(time.time()), "TEST")
  File "C:\Python26\lib\imaplib.py", line 317, in append
    return self._simple_command(name, mailbox, flags, date_time)
  File "C:\Python26\lib\imaplib.py", line 1060, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "C:\Python26\lib\imaplib.py", line 895, in _command_complete
    raise self.error('%s command error: %s %s' % (name, typ, data))
imaplib.error: APPEND command error: BAD ['Invalid Command']

正如我之前所说,此模块之前工作过。它成功创建了正文中包含字符串“Test”的草稿消息。由于此脚本曾经有效,因此它似乎更有可能与 Google 对 G 邮件帐户 IMAP 功能所做的更改有关,但该错误似乎表明 APPEND 命令中存在错误。我已经在两台不同的计算机上测试了 python 脚本,以查看我的库文件是否已损坏,但仍然存在相同的错误。

另外,我正在使用Python 2.6。任何帮助表示赞赏。

I want to write a python module that sends data to a draft message in a G-mail account. I have written a script about two weeks ago that worked perfectly using imaplib. A simplified example of my module is below. (I have created a test email address for anyone to test this script on.)

import imaplib
import time
conn = imaplib.IMAP4_SSL('imap.gmail.com', port = 993)
conn.login('[email protected]', '123456aaa')
conn.select('[Gmail]/Drafts')
conn.append("[Gmail]/Drafts", '', imaplib.Time2Internaldate(time.time()), "TEST")

It utilized the .append function, but today when I run the module and it produces the following error:

Traceback (most recent call last):
  File "C:/Windows/System32/email_append_test.py", line 6, in <module>
    conn.append("[Gmail]/Drafts", '', imaplib.Time2Internaldate(time.time()), "TEST")
  File "C:\Python26\lib\imaplib.py", line 317, in append
    return self._simple_command(name, mailbox, flags, date_time)
  File "C:\Python26\lib\imaplib.py", line 1060, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "C:\Python26\lib\imaplib.py", line 895, in _command_complete
    raise self.error('%s command error: %s %s' % (name, typ, data))
imaplib.error: APPEND command error: BAD ['Invalid Command']

As I said before, this module worked before. It successfully created draft messages with the string "Test" in its body. Since this script used to work, it seems more likely that it has something to do with a change Google made to the G-mail accounts IMAP features, but the error seems to indicate an error in the APPEND command. I have tested the python script on two different computer to see if my library file was corrupt, but the same error remained.

Also, I am using Python 2.6. Any help is appreciated.

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

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

发布评论

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

评论(1

逐鹿 2024-12-13 04:13:49

conn.append 之前添加以下内容:

import email

然后将 conn.append 行更改为:

conn.append("[Gmail]/Drafts",
            '',
            imaplib.Time2Internaldate(time.time()),
            str(email.message_from_string('TEST')))

Before the conn.append, add the following:

import email

Then change the conn.append line to read:

conn.append("[Gmail]/Drafts",
            '',
            imaplib.Time2Internaldate(time.time()),
            str(email.message_from_string('TEST')))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文