How can you send mail using IMAP?

后端 未结 5 1888
挽巷
挽巷 2020-12-06 09:03

I am developing a lightweight Gmail client for mobile phones, accessing Gmail by IMAP. I want to send a draft from the Drafts folder, but it has some attachments and I canno

相关标签:
5条回答
  • 2020-12-06 09:36

    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.

    0 讨论(0)
  • 2020-12-06 09:36

    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])
    
    0 讨论(0)
  • 2020-12-06 09:46

    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.

    0 讨论(0)
  • 2020-12-06 09:55

    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.

    0 讨论(0)
  • 2020-12-06 09:58

    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.

    0 讨论(0)
提交回复
热议问题