Я изучаю Gmail API. Допустим, мне интересно узнать, в каком письме содержится строка "foobar2000".
На данный момент это мой код:
Main.java
package manhattan.email.bot;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.services.gmail.model.Message;
import com.google.api.services.gmail.model.MessagePart;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.util.List;
import java.util.Properties;
import manhattan.email.bot.google.GmailCredentials;
import manhattan.email.bot.google.GmailService;
import manhattan.email.bot.google.GmailServiceImpl;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.StringUtils;
public class Main {
static final String MY_EMAIL = "xxxxx@gmail.com";
static final String MY_CLIENT_ID = "xxxxxapps.googleusercontent.com";
static final String MY_CLIENT_SECRET = "xxxxx";
static final String MY_ACCESS_TOKEN = "xxxxx";
static final String MY_REFRESH_TOKEN = "xxxxx";
public static void main(String[] args) {
try {
GmailService gmailService = new GmailServiceImpl(GoogleNetHttpTransport.newTrustedTransport());
gmailService.setGmailCredentials(GmailCredentials.builder()
.userEmail(MY_EMAIL)
.clientId(MY_CLIENT_ID)
.clientSecret(MY_CLIENT_SECRET)
.accessToken(MY_ACCESS_TOKEN)
.refreshToken(MY_REFRESH_TOKEN)
.build());
List<com.google.api.services.gmail.model.Message> messageList = gmailService.listMessagesMatchingQuery(MY_EMAIL,"foobar2000");
System.out.println("Search result: ");
for (Message m:messageList){
System.out.println(m.toPrettyString());
}
}
catch (GeneralSecurityException gse){
System.err.println(">>> GSE: "+gse.getMessage());
}
catch (IOException ioe){
System.err.println(">>> IOE: "+ioe.getMessage());
}
}
}
GmailCredentials. java
package manhattan.email.bot.google;
import lombok.Builder;
import lombok.Data;
@Builder
@Data
public class GmailCredentials {
private final String userEmail;
private final String clientId;
private final String clientSecret;
private final String accessToken;
private final String refreshToken;
}
GmailService.java
package manhattan.email.bot.google;
import com.google.api.services.gmail.model.Message;
import java.io.IOException;
import java.util.List;
import javax.mail.MessagingException;
public interface GmailService {
void setGmailCredentials(GmailCredentials credentials);
boolean sendMessage(String recipientAddress, String subject, String body) throws MessagingException, IOException;
List<Message> listMessagesMatchingQuery(String userId, String query) throws IOException;
}
GmailServiceImpl.java
package manhattan.email.bot.google;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.Base64;
import com.google.api.services.gmail.Gmail;
import com.google.api.services.gmail.model.ListMessagesResponse;
import com.google.api.services.gmail.model.Message;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
*
* @author OCIN
*/
public class GmailServiceImpl implements GmailService {
private static final String APPLICATION_NAME = "mail-bot";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private final HttpTransport httpTransport;
private GmailCredentials gmailCredentials;
public GmailServiceImpl(HttpTransport transport){
this.httpTransport = transport;
}
private Gmail createGmail() {
Credential credential = authorize();
return new Gmail.Builder(httpTransport, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
}
private Credential authorize() {
return new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setClientSecrets(gmailCredentials.getClientId(), gmailCredentials.getClientSecret())
.build()
.setAccessToken(gmailCredentials.getAccessToken())
.setRefreshToken(gmailCredentials.getRefreshToken());
}
@Override
public void setGmailCredentials(GmailCredentials credentials) {
this.gmailCredentials = credentials;
}
private MimeMessage createEmail(String to, String from, String subject, String bodyText) throws MessagingException {
MimeMessage email = new MimeMessage(Session.getDefaultInstance(new Properties(), null));
email.setFrom(new InternetAddress(from));
email.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to));
email.setSubject(subject);
email.setText(bodyText);
return email;
}
private Message createMessageWithEmail(MimeMessage emailContent) throws MessagingException, IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
emailContent.writeTo(buffer);
return new Message().setRaw(Base64.encodeBase64URLSafeString(buffer.toByteArray()));
}
@Override
public boolean sendMessage(String recipientAddress, String subject, String body) throws MessagingException, IOException {
Message message = createMessageWithEmail(
createEmail(recipientAddress, gmailCredentials.getUserEmail(), subject, body));
return createGmail().users()
.messages()
.send(gmailCredentials.getUserEmail(), message)
.execute()
.getLabelIds().contains("SENT");
}
public List<Message> listMessagesMatchingQuery(String userId, String query) throws IOException {
ListMessagesResponse response = createGmail().users().messages().list(userId).setQ(query).execute();
List<Message> messages = new ArrayList<Message>();
while (response.getMessages() != null) {
messages.addAll(response.getMessages());
if (response.getNextPageToken() != null) {
String pageToken = response.getNextPageToken();
response = createGmail().users().messages().list(userId).setQ(query).setPageToken(pageToken).execute();
}
else {
break;
}
}
// for (Message message : messages) {
// System.out.println(message.toPrettyString());
// }
return messages;
}
}
Вывод моего кода:
Результат поиска: {"id": "16d929a8524a6bbd", "threadId": "16d929a8524a6bbd"}
Это верно, потому что в теле есть только 1 сообщение с "foobar2000". Содержание всего тела:
Bla bla bla foobar2000 Hello world
Как это получить? Я попытался использовать это внутри цикла:
System.out.println(StringUtils.newStringUtf8(Base64.decodeBase64(m.getPayload().getParts().get(0).getBody().getData())));
Дало мне исключение NullPointerException ...