- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是第一次使用 gmail api。我正在尝试使用 gmail api 保存草稿消息。源码类似如下,
String urlLink = "https://www.googleapis.com/upload/gmail/v1/users/" + emailSetting.getEmailId() + "/drafts?uploadType=media";
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(urlLink);
httpPost.setHeader("Content-Type", "message/rfc822");
httpPost.setHeader("Authorization", "Bearer " + access_token);
StringEntity params = new StringEntity(message.toString());
httpPost.setEntity(params);
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
这里的消息是 JSONObject,我用它来准备消息 json,如下所示,
JSONObject jsonObject = new JSONObject();
JSONObject message =new JSONObject();
jsonObject.put("threadId", 001);
jsonObject.put("snippet", msg.getSubject());
// Prepare Header start
JSONArray jsonArray = new JSONArray();
JSONObject jsonObj = new JSONObject();
jsonObj.put("name", "Delivered-To");
jsonObj.put("value", msg.getTo());
jsonArray.put(jsonObj);
jsonObj = new JSONObject();
jsonObj.put("name", "To");
//jsonObj.put("value", "<"+msg.getTo()+">");
jsonObj.put("value", msg.getTo());
jsonArray.put(jsonObj);
jsonObj = new JSONObject();
jsonObj.put("name", "From");
jsonObj.put("value", emailSetting.getEmailId());
jsonArray.put(jsonObj);
jsonObj = new JSONObject();
jsonObj.put("name", "Subject");
jsonObj.put("value", msg.getSubject());
jsonArray.put(jsonObj);
jsonObj = new JSONObject();
jsonObj.put("name", "Date");
jsonObj.put("value", new java.util.Date());
jsonArray.put(jsonObj);
JSONObject headerJSONObject = new JSONObject();
headerJSONObject.put("headers", jsonArray);
jsonObject.put("payload", headerJSONObject);
message.put("message", jsonObject);
message.put("id", msg.getMessageId());
上面的代码以状态 200 执行。但是草稿消息是空的,即没有主题,没有“收件人”,也没有正文。如果有人对此源代码有任何建议,请分享,谢谢。
最佳答案
您可以使用这个Users.drafts: create from the documentation 。如果您还没有尝试过。
Creates a new draft with the
DRAFT
label. Try it now or see an example.This method supports an /upload URI and accepts uploaded media with the following characteristics:
- Maximum file size: 35MB
- Accepted Media MIME types:
message/rfc822
这是一个示例 java code provided in the documentation .
import com.google.api.client.repackaged.org.apache.commons.codec.binary.Base64;
import com.google.api.services.gmail.Gmail;
import com.google.api.services.gmail.model.Draft;
import com.google.api.services.gmail.model.Message;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.util.Enumeration;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
// ...
public class MyClass {
// ...
/**
* Create draft email.
*
* @param service Authorized Gmail API instance.
* @param userId User's email address. The special value "me"
* can be used to indicate the authenticated user.
* @param email MimeMessage used as email within Draft.
* @return Created Draft.
* @throws MessagingException
* @throws IOException
*/
public static Draft createDraft(Gmail service, String userId, MimeMessage email)
throws MessagingException, IOException {
Message message = createMessageWithEmail(email);
Draft draft = new Draft();
draft.setMessage(message);
draft = service.users().drafts().create(userId, draft).execute();
System.out.println("draft id: " + draft.getId());
System.out.println(draft.toPrettyString());
return draft;
}
/**
* Create a Message from an email
*
* @param email Email to be set to raw of message
* @return Message containing base64url encoded email.
* @throws IOException
* @throws MessagingException
*/
public static Message createMessageWithEmail(MimeMessage email)
throws MessagingException, IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
email.writeTo(baos);
String encodedEmail = Base64.encodeBase64URLSafeString(baos.toByteArray());
Message message = new Message();
message.setRaw(encodedEmail);
return message;
}
// ...
}
关于java - 无法使用 gmail api 创建草稿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47156474/
我一直在从事一个需要内容的草稿/实时版本的项目,并想到了如下设计: Article ID Creator CreationDate DraftContent(fk to
这是我的第一个问题,如果我做错了什么,请告诉我... 我目前正在用 Java 制作一个草稿游戏。事实上,除了 AI 之外,一切正常。AI 目前是单线程的,使用 minimax 和 alpha-beta
我一直在 Delphi 下使用 Indy 通过 gmail 帐户发送消息,使用 TIdSMTP 和 TIdMessage 组件。这绝对没问题。 但是,我的客户请求将消息保存到 DRAFTS 文件夹,以
我有一个简单的 IBackgroundTask执行查询然后根据特定项目是否存在执行插入或一个或多个更新的实现。但是,更新没有持久化,我不明白为什么。新项目按预期创建。 我正在更新的内容项有 Commo
我的部分应用程序功能是自动填写 Gmail 邮件主题、文本、收件人并附上照片。但是当我退出 gmail Intent 而不发送邮件时,我不想将此邮件保存为草稿。 如何禁止在从我的应用程序开始意向之前保
我正在尝试使用 facebook 的 Draft-JS 框架实现一个编辑器,该框架将被限制为 50 个字符的行。 我不希望使用 css 换行,我希望它们在达到 50 个字符的限制时真正分成单独的 bl
我正在编写一个应该同时支持 draft 17 的小型 WebSocket 服务器应用程序和较旧的变体,例如 draft 00 .我对最新的草稿没有任何问题,但我无法让草稿 00 的客户满意。 出于测试
我正在使用 Facebook 的 Draft.js 库创建富文本框,我需要将文本框的插入符号设为红色。可能吗? 我已阅读有关 -webkit-text-fill-color 属性的内容,但它使键入的文
我正在使用 Gmail API 在 Python 中自动创建 Gmail 草稿。我需要创建 HTML 格式的电子邮件,但我个人也需要创建纯文本回退,因为这是正确的做法。 我以为我已经完成了上述所有工作
从官方文档我知道了 2 种方法:通过键获取实体和获取最后创建的实体。就我而言,我还需要一种方法来访问当前 ContentState 中的所有实体。有什么方法可以执行此操作吗?如果没有,是否有一个可以提
我有一个难题要解决。 Here is my sample project 基本上我想创建一个可编辑的类似书本的容器,并且由于每个书页都是另一个容器,所以我不知道如何在页面充满文本后让编辑器从一个页面过
按此 SO thread我应该能够使用格式如下的超链接直接转到使用 Gmail REST API 创建的 Gmail 草稿邮件: https://mail.google.com/mail/#draft
我一直在尝试弄清楚如何自动将收件人添加到通过他们的 Ruby 库使用 Gmail API 创建的草稿电子邮件。我可以毫无问题地创建草稿,但设置收件人给我带来了麻烦,而且我一直无法找到任何好的示例来展示
是否有文档解释如何在将内容粘贴到 Draft.js 中时保留段落分隔符?其他格式看起来很合理,但粘贴时所有段落 block 都会折叠成单个段落 block 。 最佳答案 您可以使用编辑器的 Prop
我正在构建一个基于draft-js的扩展编辑器来添加脚注功能。在主编辑器中编写文本时,用户可以选择单击“添加脚注”,这会在当前选择处添加脚注标记(带有注释编号的徽章),并打开辅助编辑器,在其中编写文本
我总是使用 Gmail 来保存网页剪辑或笔记。我只需创建一封新邮件,对其进行编辑并另存为草稿。两年多来,我已将 1000 多封邮件转储到 Gmail 草稿文件夹中。我想以编程方式将它们全部发送给自己。
N4791 - 2018-12-07(这与 [basic.start.static]/2 的 current draft 中的措辞一致) Constant initialization is perf
我正在尝试使用 C# 在 winforms 应用程序中生成 Gmail 草稿邮件。消息草稿需要采用 HTML 格式并能够包含附件。 我能够使用 AE.Net.Mail 生成带有附件的草稿,但草稿邮件是
我正在使用来自 their website 的 paypal 创建发票样本 json 我得到了一个足够好的 token ,我将其与帖子一起提交,因为错误不是 401 Unauthorized 而是 4
我是一名优秀的程序员,十分优秀!