2017-11-07 1 views
0

erstellen Ich verwende Google Mail APIs zum ersten Mal. Ich versuche, einen Entwurf einer Nachricht mit Gmail API zu speichern. Der Quellcode ist ähnlich wie folgtkann keinen Entwurf mit 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); 

Hierher Nachricht wird die JSONObject, die ich die Meldung json der Vorbereitung bin mit wie folgt

 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()); 

Der obige Code mit dem Status ausgeführt wird 200. Aber der Entwurf der Botschaft ist leer, dh es gibt kein Subjekt, kein "zu" und keinen Körper. Wenn jemand einen Vorschlag zu diesem Quellcode hat, dann bitte teilen, danke.

Antwort

0

Sie können diese Users.drafts: create from the documentation verwenden. Wenn Sie es noch nicht versucht haben.

Erstellt einen neuen Entwurf mit dem Label DRAFT. Try it now oder see an example.

Diese Methode unterstützt eine / URI laden und akzeptiert Medien mit den folgenden Merkmalen hochgeladen:

  • Maximale Dateigröße: 35MB
  • Accepted Medien MIME-Typen:message/rfc822

Hier ist ein Beispiel 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; 
    } 

    // ... 

}