如何使用imaplib创建并发送邮件到特定邮箱
我正在尝试使用Python的imaplib库来创建一封邮件,并把它发送到一个特定名称的邮箱,比如说INBOX。有没有人有什么好的建议呢?
4 个回答
0
因为我还不能对用户3556956的评论进行回复,所以我在这里给出python3的答案:
connection.append('INBOX', '', imaplib.Time2Internaldate(time.time()), str(new_message).encode('utf-8'))
简单来说,你需要把消息作为字节传递,而不是作为Python字符串。
10
你可以使用Python自带的imaplib
模块和append()
命令,把邮件消息添加到IMAP文件夹里:
import imaplib
from email.message import Message
from time import time
connection = imaplib.IMAP4_SSL(HOSTNAME)
connection.login(USERNAME, PASSWORD)
new_message = Message()
new_message["From"] = "hello@itsme.com"
new_message["Subject"] = "My new mail."
new_message.set_payload("This is my message.")
connection.append('INBOX', '', imaplib.Time2Internaldate(time()), str(new_message).encode('utf-8'))