`
sillycat
  • 浏览: 2551835 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Mail Server Solution(5)gmail API and Download Attachment

 
阅读更多
Mail Server Solution(5)gmail API and Download Attachment

1. List all the Messages based on Query
// List all the messages related to keywords
ListMessagesResponse listMessageResponse = service.users().messages()
.list(user).setQ("job.craigslist.org").execute();
List<Message> messages = new ArrayList<Message>();
while (listMessageResponse.getMessages() != null) {
messages.addAll(listMessageResponse.getMessages());
if (listMessageResponse.getNextPageToken() != null) {
String pageToken = listMessageResponse.getNextPageToken();
listMessageResponse = service.users().messages().list(user).setQ("job.craigslist.org")
.setPageToken(pageToken).execute();
} else {
break;
}
}

for (Message message : messages) {
System.out.println(message.toPrettyString());
}

2. Get the Message Attach Info
//Get the Message Attach Info
Message message = service.users().messages().get(user, "14eda36adbc0789e").execute();
System.out.println("Message snippet: " + message.getSnippet());
List<MessagePart> parts = null;
if(message.getPayload() != null){
parts = message.getPayload().getParts();
if(parts != null && parts.size() > 0){
for(int i = 0; i< parts.size();i++){
MessagePart part = parts.get(i);
System.out.println("====== part " + i + "==========");
System.out.println("filename: " + part.getFilename());
System.out.println("mimetype: " + part.getMimeType());
if(part.getMimeType().equalsIgnoreCase("application/pdf")){
System.out.println("attachmentId : " + part.getBody().getAttachmentId());
}
}
}
}

3. Get the Attachment File
//Get the attachment file
String attachmentId = "ANGjdJ-T4bVPdCT3gQSW-iuq8Txjofxjo3eNjk5qFawdotTon6Rkv2mjH-8EUehiH71OZJsN_mTFA5cHj05JGvuN3_Ev05_26bxUXvO4NaYxw-aExinNWZbUdpgSYqHq5NmS6QVnOKBbsrwdTau1kGzB5JC1qYNuXMKVRuPvrL49RcYEfBR8iIzHDvskqY8bVcyUHPTSCu0mycCf32-eogP7muMu7TO2MjF1HIYv50kCGG2edaSKrBD_miQU6aTqQJVhDJ4BVP7LiEB2bwkZssRuF8esb1Obu7N702e6Gw";
MessagePartBody attachPart = service.users().messages().attachments().get(user, "14eda36adbc0789e", attachmentId).execute();
byte[] fileByteArray = Base64.decodeBase64(attachPart.getData());
        FileOutputStream fileOutFile =
            new FileOutputStream("/Users/carl/data/" + "attach1.pdf");
        fileOutFile.write(fileByteArray);
        fileOutFile.close();

It will query my own email and find the attachment files and save to /Users/carl/data/ Directory.

The output on the console will be:
- SPAM
{
  "id" : "14eda36adbc0789e",
  "threadId" : "14eda360b8f81792"
}
{
  "id" : "14eda365e1ab3916",
  "threadId" : "14eda365e1ab3916"
}
{
  "id" : "14eda363378de6ba",
  "threadId" : "14eda363378de6ba"
}
{
  "id" : "14eda360b8f81792",
  "threadId" : "14eda360b8f81792"
}
Message snippet: From: Amber Link [mailto:amber@jobs2careers.com] Sent: Wednesday, July 29, 2015 9:27 AM To: Suong Luu
====== part 0==========
filename:
mimetype: multipart/alternative
====== part 1==========
filename: JMRES12.ADB.aptx.cc.aus.pdf
mimetype: application/pdf
attachmentId : ANGjdJ-qQ-YVPp8li1c9UG1_dDZcLVIGi44z2wWsz8wOl9Ic3Y9z5yBrlGw-xXW1QIoCmQNHlltmj-AvwlrqvwR9IofMDCJJbQS43VZSRg71raq3vHpPBu_5gFRZnQMd_SSHsWs_duBtrHFXfpPjc_wu-aoxEqLl-HcaCFn3cRMwj2wa1MrpoDKpyzXlGQiKb4Y8afvbHTPa63wVYgNtnBlVlQVl3z1JDADTYazKS8_E0D0Kajktdnc5ezq0E8wMAS6OOBM-fOYm09_NGoEzKCb9zD3q93CnkhvCh49A2g

BUILD SUCCESSFUL

About the Oauth authentication Part
private static final List<String> SCOPES = Arrays.asList(
GmailScopes.GMAIL_READONLY);

References:
https://developers.google.com/gmail/api/v1/reference/users/messages/list




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics