我正在尝试阅读电子邮件,将电子邮件另存为对象,保存其附件,然后将对象作为JSON发布到API。一切正常,只是主体只输出为com.sun.mail.util.BASE64DecoderStream@1q9q9qj4。我认为代码会处理带附件的邮件正文,但它似乎不起作用。
public class ReadEmails {
private static String saveDirectory = "C://attachments";
public void setSaveDirectory(String dir) {
ReadEmails.saveDirectory = dir;
}
public static void downloadEmailAttachments(String host, String port, String userName, String password) {
Properties properties = new Properties();
properties.put("mail.pop3.host", host);
properties.put("mail.pop3.port", port);
properties.put("mail.pop3.starttls.enable", "true");
properties.put("mail.pop3.ssl.trust", host);
properties.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.pop3.socketFactory.fallback", "true");
properties.setProperty("mail.pop3.socketFactory.port", String.valueOf(port));
Session session = Session.getDefaultInstance(properties);
try {
Store store = session.getStore("pop3");
store.connect(userName, password);
Folder folderInbox = store.getFolder("INBOX");
folderInbox.open(Folder.READ_ONLY);
Message[] arrayMessages = folderInbox.getMessages();
for (int i = 0; i < arrayMessages.length; i++) {
Message message = arrayMessages[i];
Address[] fromAddress = message.getFrom();
String from = fromAddress[0].toString();
String subject = message.getSubject();
String sentDate = message.getSentDate().toString();
String contentType = message.getContentType();
String messageContent = "";
String attachFiles = "";
if (contentType.contains("multipart")) {
// content may contain attachments
Multipart multiPart = (Multipart) message.getContent();
int numberOfParts = multiPart.getCount();
for (int partCount = 0; partCount < numberOfParts; partCount++) {
MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
// this part is attachment
String fileName = part.getFileName();
attachFiles += fileName + ", ";
part.saveFile(saveDirectory + File.separator + fileName);
} else {
// this part may be the message content
messageContent = part.getContent().toString();
}
}
if (attachFiles.length() > 1) {
attachFiles = attachFiles.substring(0, attachFiles.length() - 2);
}
} else if (contentType.contains("text/plain") || contentType.contains("text/html")) {
Object content = message.getContent();
if (content != null) {
messageContent = content.toString();
}
}
NewCase nc = new NewCase();
nc.setTitle(subject);
nc.setDescription(messageContent);
nc.setEmail(from);
sendEmails(nc);
System.out.print("email: "+ nc.getTitle() + nc.getDescription() + nc.getEmail());
}
folderInbox.close(false);
store.close();
} catch (NoSuchProviderException ex) {
System.out.println("No provider for pop3.");
ex.printStackTrace();
System.out.println(ex);;
} catch (MessagingException ex) {
System.out.println("Could not connect to the message store");
ex.printStackTrace();
System.out.println(ex);;
} catch (IOException ex) {
ex.printStackTrace();
System.out.println(ex);;
}
}
static void sendEmails(NewCase nc) {
final String url = "http://localhost:8080/email";
String title = nc.getTitle();
String description = nc.getDescription();
String email = nc.getEmail();
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost p = new HttpPost(url);
p.setEntity(new StringEntity("{\"title\":\"" + title + "\",\"description\":\"" + description + "\",\"email\":\""+ email +"\"}",
ContentType.create("application/json")));
try {
HttpResponse r = httpClient.execute(p);
System.out.println(r.getStatusLine().getStatusCode());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}