我正在研究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你好世界
如何获得?我试过在循环中使用它:
System.out.println(StringUtils.newStringUtf8(Base64.decodeBase64(m.getPayload().getParts().get(0).getBody().getData()))); 给了我一个NullPointerException ...
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。