,我要撰写邮件并将其保存到Gmail草稿,无需用户干预,
Preferably using Python or Java, I want to compose an email and save it into gmail drafts without user intervention,
推荐答案下面是一个Python脚本来访问Gmail帐户。首先,你需要生成一个OAuth令牌。下载谷歌的xoauth.py模块并运行它。它会引导您完成的步骤。你会得到一个url获取一个验证code - 粘贴到脚本,它会吐出你的令牌和秘密的:
Here's a Python script to access a Gmail account. First you need to generate an OAuth token. Download Google's xoauth.py module and run it. It will walk you through the steps. You'll get a url to obtain a verification code -- paste this into the script and it will spit out your token and secret:
% python xoauth.py --generate_oauth_token --user=youremail@gmail一旦你得到你的令牌和秘密,将它们复制到下面的Python脚本。它采用 xoauth.py 来验证IMAP客户端,连接到IMAP,构建了一个消息,并将其丢弃到草稿文件夹。
Once you've obtained your token and secret, copy them into the Python script below. It uses xoauth.py to authenticate the IMAP client, connects to IMAP, constructs a message and drops it into the Drafts folder.
import email.message import imaplib import random import time import xoauth MY_EMAIL = 'youremail@gmail' MY_TOKEN = '<token>' MY_SECRET = '<secret>' # construct the oauth access token nonce = str(random.randrange(2**64 - 1)) timestamp = str(int(time.time())) consumer = xoauth.OAuthEntity('anonymous', 'anonymous') access = xoauth.OAuthEntity(MY_TOKEN, MY_SECRET) token = xoauth.GenerateXOauthString( consumer, access, MY_EMAIL, 'imap', MY_EMAIL, nonce, timestamp) # connect to gmail's imap service. imap = imaplib.IMAP4_SSL('imap.googlemail') imap.debug = 4 imap.authenticate('XOAUTH', lambda x: token) # create the message msg = email.message.Message() msg['Subject'] = 'subject of the message' msg['From'] = MY_EMAIL msg['To'] = MY_EMAIL msg.set_payload('Body of the message') # append the message to the drafts folder now = imaplib.Time2Internaldate(time.time()) imap.append('[Gmail]/Drafts', '', now, str(msg)) imap.logout()更多推荐
编程方式保存草稿在Gmail草稿文件夹中
发布评论