Gmail API:解码邮件正文 (Java/Android)
Gmail APIs: Decoding the body of the message (Java/Android)
我需要以用户邮箱中的格式获取检索到的邮件,即:HTML。
我在解码检索到的消息的正文时遇到问题。
请在 Java 中提出完成此操作的方法。
我目前正在这样做以获取消息:
public class MyClass {
public static Message getMessage(Gmail service, String userId, String messageId)
throws IOException {
Message message = service.users().messages().get(userId, messageId).execute();
System.out.println("Message snippet: " + message.getSnippet());
return message;
}
public static MimeMessage getMimeMessage(Gmail service, String userId, String messageId)
throws IOException, MessagingException {
Message message = service.users().messages().get(userId, messageId).setFormat("raw").execute();
byte[] emailBytes = Base64.decodeBase64(message.getRaw());
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
MimeMessage email = new MimeMessage(session, new ByteArrayInputStream(emailBytes));
return email;
}
}
String mimeType = message.getPayload().getMimeType();
List<MessagePart> parts = message.getPayload().getParts();
if (mimeType.contains("alternative")) {
log.info("entering alternative loop");
for (MessagePart part : parts) {
mailBody = new String(Base64.decodeBase64(part.getBody()
.getData().getBytes()));
}
log.info(mailBody);
}
您可以使用以下方法:
private String getContent(Message message) {
StringBuilder stringBuilder = new StringBuilder();
try {
getPlainTextFromMessageParts(message.getPayload().getParts(), stringBuilder);
byte[] bodyBytes = Base64.decodeBase64(stringBuilder.toString());
String text = new String(bodyBytes, "UTF-8");
return text;
} catch (UnsupportedEncodingException e) {
logger.error("UnsupportedEncoding: " + e.toString());
return message.getSnippet();
}
}
private void getPlainTextFromMessageParts(List<MessagePart> messageParts, StringBuilder stringBuilder) {
for (MessagePart messagePart : messageParts) {
if (messagePart.getMimeType().equals("text/plain")) {
stringBuilder.append(messagePart.getBody().getData());
}
if (messagePart.getParts() != null) {
getPlainTextFromMessageParts(messagePart.getParts(), stringBuilder);
}
}
}
它将所有消息部分与 mimeType "text/plain" 和 returns 组合为一个字符串。
新字符串(messageResult.getPayload().getParts().get(0).getBody().decodeData())
我需要以用户邮箱中的格式获取检索到的邮件,即:HTML。
我在解码检索到的消息的正文时遇到问题。
请在 Java 中提出完成此操作的方法。
我目前正在这样做以获取消息:
public class MyClass {
public static Message getMessage(Gmail service, String userId, String messageId)
throws IOException {
Message message = service.users().messages().get(userId, messageId).execute();
System.out.println("Message snippet: " + message.getSnippet());
return message;
}
public static MimeMessage getMimeMessage(Gmail service, String userId, String messageId)
throws IOException, MessagingException {
Message message = service.users().messages().get(userId, messageId).setFormat("raw").execute();
byte[] emailBytes = Base64.decodeBase64(message.getRaw());
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
MimeMessage email = new MimeMessage(session, new ByteArrayInputStream(emailBytes));
return email;
}
}
String mimeType = message.getPayload().getMimeType();
List<MessagePart> parts = message.getPayload().getParts();
if (mimeType.contains("alternative")) {
log.info("entering alternative loop");
for (MessagePart part : parts) {
mailBody = new String(Base64.decodeBase64(part.getBody()
.getData().getBytes()));
}
log.info(mailBody);
}
您可以使用以下方法:
private String getContent(Message message) {
StringBuilder stringBuilder = new StringBuilder();
try {
getPlainTextFromMessageParts(message.getPayload().getParts(), stringBuilder);
byte[] bodyBytes = Base64.decodeBase64(stringBuilder.toString());
String text = new String(bodyBytes, "UTF-8");
return text;
} catch (UnsupportedEncodingException e) {
logger.error("UnsupportedEncoding: " + e.toString());
return message.getSnippet();
}
}
private void getPlainTextFromMessageParts(List<MessagePart> messageParts, StringBuilder stringBuilder) {
for (MessagePart messagePart : messageParts) {
if (messagePart.getMimeType().equals("text/plain")) {
stringBuilder.append(messagePart.getBody().getData());
}
if (messagePart.getParts() != null) {
getPlainTextFromMessageParts(messagePart.getParts(), stringBuilder);
}
}
}
它将所有消息部分与 mimeType "text/plain" 和 returns 组合为一个字符串。
新字符串(messageResult.getPayload().getParts().get(0).getBody().decodeData())