How can you send mail using IMAP?

2019-01-18 18:21发布

I'm developing a lightweight Gmail client for mobile phones. It access Gmail by IMAP. Then I want to send a draft from the Drafts folder, but it has some attachments and I can't download all of them to send it by SMTP.

Moving/copying it to "Sent Mail" doesn't send it, just moves it to that folder.

How can I send a Draft directly without fetch all the content and attachments from the client? Is there any IMAP command to do it?

标签: smtp gmail imap
5条回答
祖国的老花朵
2楼-- · 2019-01-18 18:47

I sent an email to my own email address using IMAP using Python 3 to a gmail account. What is does is append a message to a mailbox. You need to utilize a handful of Python's native libraries. Also study this documentation for imaplib, this code is featured in the section Uploading Messages: To add a new message to a mailbox, construct a Message instance and pass it to the append() method, along with the timestamp for the message.

Then check your gmail inbox and you'll see the new message.

import imaplib
import time
import email.message
import imaplib_connect

new_message = email.message.Message()
new_message.set_unixfrom('name')
new_message['Subject'] = 'Test'
new_message['From'] = 'name@gmail.com'
new_message['To'] = 'name@gmail.com'
new_message.set_payload('This is an example message body.\n')

print(new_message)

with imaplib_connect.open_connection() as c:
    c.append('INBOX', '',
             imaplib.Time2Internaldate(time.time()),
             str(new_message).encode('utf-8'))

# Show the headers for all messages in the mailbox
c.select('INBOX')
typ, [msg_ids] = c.search(None, 'ALL')
for num in msg_ids.split():
    typ, msg_data = c.fetch(num, '(BODY.PEEK[HEADER])')
    for response_part in msg_data:
        if isinstance(response_part, tuple):
            print('\n{}:'.format(num))
            print(response_part[1])
查看更多
老娘就宠你
3楼-- · 2019-01-18 18:52

IMAP is a mailbox protocol. It does not (natively) support sending mail, only accessing it. In order to send mail you must use SMTP. Its possible that there is an IMAP extension for sending mail, and its possible that Google Mail supports that extension, but I doubt it. Hence, if you want to send an email with attachments, you must actually have the full content of the message available to you to send.

查看更多
够拽才男人
4楼-- · 2019-01-18 18:56

IMAP was designed to receive email messages, not to send it. There is no IMAP command for sending email AFAIK. There is, however, at least one IMAP server which supports a special 'Outbox' folder. When you place the message into this folder it will be sent automatically.

Check Courier-IMAP documentation on Sending mail via an IMAP connection. Note, that this is a non standard method and I'm not aware of any other server which supports this.

There RFC 4468 which extends SMTP so it can fetch the mail content from the IMAP server, but I don't know about any working and widely used implementation.

Talking about gmail: sticking with SMTP is probably the safest way to go.

查看更多
我命由我不由天
5楼-- · 2019-01-18 18:59

Sending email is a special feature of some imap servers. Its nothing in the imap protocol. You just copy your email into a special imap directory on the server and it sends them. I doubt that gmail supports this.

查看更多
Evening l夕情丶
6楼-- · 2019-01-18 19:08

By the way, now that any modern mail client (including the webbased ones) supports a Sent folder, you typicaly have to use both SMTP and IMAP to send a single mail. And there's a race condition between sending the e-mail over SMTP and successfully saving the e-mail to the IMAP Sent folder. Using IMAP for sending e-mail is a way to avoid this race condition.

查看更多
登录 后发表回答