2014-12-05 134 views
0

我正在製作一個應用程序,該應用程序應該使用Gmail API接收Gmail用戶的電子郵件。但是,我必須收到每封電子郵件的數據作爲內容,發件人,收到的主題和日期。有沒有人實施過這個?如何從Gmail API中的用戶獲取所有電子郵件的內容?

請按照在谷歌抓住了部分,在那裏我只能得到ID和線程每個電子郵件ID:

public class GmailApiQuickstart { 

// Check https://developers.google.com/gmail/api/auth/scopes for all available scopes 
private static final String SCOPE = "https://www.googleapis.com/auth/gmail.readonly"; 
private static final String APP_NAME = "GmailApiQuickstart"; 
private static final String USER = "me"; 
// Path to the client_secret.json file downloaded from the Developer Console 
private static final String CLIENT_SECRET_PATH = "c:/client_secret.json"; 

private static GoogleClientSecrets clientSecrets; 

public void buscar() throws IOException { 
HttpTransport httpTransport = new NetHttpTransport(); 
JsonFactory jsonFactory = new JacksonFactory(); 

clientSecrets = GoogleClientSecrets.load(jsonFactory, new FileReader(CLIENT_SECRET_PATH)); 

// Allow user to authorize via url. 
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, jsonFactory, clientSecrets, Arrays.asList(SCOPE)) 
.setAccessType("online") 
.setApprovalPrompt("auto").build(); 

String url = flow.newAuthorizationUrl().setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI) 
.build(); 
System.out.println("Please open the following URL in your browser then type" 
       + " the authorization code:\n" + url); 

// Read code entered by user. 
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
String code = br.readLine(); 

// Generate Credential using retrieved code. 
GoogleTokenResponse responseT = flow.newTokenRequest(code) 
.setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI).execute(); 
GoogleCredential credential = new GoogleCredential() 
.setFromTokenResponse(responseT); 

// Create a new authorized Gmail API client 
Gmail service = new Gmail.Builder(httpTransport, jsonFactory, credential) 
.setApplicationName(APP_NAME).build(); 

ListMessagesResponse response = service.users().messages().list(USER).setQ("vagas").execute(); 

List<Message> messages = new ArrayList<Message>(); 
while (response.getMessages() != null) { 
    messages.addAll(response.getMessages()); 
    if (response.getNextPageToken() != null) { 
    String pageToken = response.getNextPageToken(); 
    response = service.users().messages().list(USER).setQ("vagas") 
     .setPageToken(pageToken).execute(); 
    } else { 
    break; 
    } 
} 

for (Message message : messages) { 
    //Essa é a parte onde pega o ID e Thread ID 
    //qual(ais) parâmetro(s) devo adicionar para pegar o que preciso??? 
    System.out.println(message.toPrettyString()); 
} 
} 

} 

感謝所有!

回答

0

給定電子郵件的ID,您可以使用Users.messages資源的get方法獲取內容。它返回JSON響應中的內容,發件人,主題和日期,收件人,發件人字段。

當您獲取消息時,您可以指定負載格式作爲響應。 FULL(默認)格式返回有效負載字段中的整個解析消息。 MINIMAL格式僅返回元數據,如標識符和標籤。 RAW格式在原始屬性中將數據作爲base64url編碼字符串返回。

+0

哪裏可以設置有效載荷格式? – 2016-01-04 17:17:32

相關問題