以编程方式将草稿保存在Gmail草稿文件夹中

编程入门 行业动态 更新时间:2024-10-22 09:35:06
本文介绍了以编程方式将草稿保存在Gmail草稿文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

最好使用Python或Java,我希望撰写一封电子邮件并将其保存到gmail草稿中,而无需用户干预,

Preferably using Python or Java, I want to compose an email and save it into gmail drafts without user intervention,

推荐答案

以下是用于访问Gmail帐户的Python脚本.首先,您需要生成一个OAuth令牌.下载 Google的 xoauth.py模块并运行它.它将引导您完成所有步骤.您将获得一个网址以获取验证码-将其粘贴到脚本中,它将吐出您的令牌和密码:

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草稿文件夹中

本文发布于:2023-11-12 12:00:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581446.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:草稿   方式   文件   夹中   Gmail

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!