我正在使用 Java 邮件 1.4.6 来阅读和解析 gmail 收件箱的电子邮件。但问题是当我要解析电子邮件主题时(示例主题字符串是“在 Jenkins 中构建失败:appanalytix » appanalytix #51”)和声明
String subjectStr=(String) mimeMessage.getSubject();
我正进入(状态
subjectStr 为“在 Jenkins 中构建失败:appanalytix » appanal”而不是“在 Jenkins 中构建失败:appanalytix » appanalytix #51”。
我可以知道我哪里出错了吗?是否需要解码主题字符串
代码是:
public boolean parseEmailSubject(String host,String userName,String password,String configurationStringToCheck,String saveDirectory){
....
Session session = Session.getDefaultInstance(properties);
try {
IMAPSSLStore store = (IMAPSSLStore)session.getStore(IMAP);
store.connect( host, userName, password ) ;
Folder folderInbox =store.getFolder(IMAP_FOLDER);
folderInbox.open(Folder.READ_ONLY);
Message[] arrayMessages = folderInbox.getMessages();
for (int i = 0; i < arrayMessages.length; i++) {
MimeMessage message = (MimeMessage) arrayMessages[i];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
message.writeTo(bos);
bos.close();
SharedByteArrayInputStream bis =new SharedByteArrayInputStream(bos.toByteArray());
MimeMessage cmsg = new MimeMessage(session, bis);
bis.close();
String megSubject = cmsg.getSubject();
if(megSubject.contains(configurationStringToCheck)){
String contentType = cmsg.getContentType();
if (contentType.contains(IMAP_CONTENT_TYPE)) {
Multipart multiPart = (Multipart) cmsg.getContent();
int numberOfParts = multiPart.getCount();
for (int partCount = 0; partCount < numberOfParts; partCount++) {
MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
String fileName = part.getFileName();
part.saveFile(saveDirectory + File.separator + fileName);
}
}
}
return true;
}
}
folderInbox.close(false);
store.close();
}catch (Exception e) {
}
}
电子邮件示例是(下面的主题行)在 Jenkins 中构建失败:appanalytix » appanalytix #51
(下面的正文部分)更改:
<===[JENKINS REMOTING CAPACITY]===> 频道已启动 log4j:WARN 找不到记录器 (org.apache.commons.beanutils.converters.BooleanConverter) 的附加程序。log4j:WARN 请正确初始化 log4j 系统。