Converting problem: header X-MS-TNEF-Correlator is empty, so there is an exception
lacivert opened this issue · comments
I don't know if this is an issue of the project or the email server provider, but there is a problem on converting MimeMessage to Email.
I try to use https://ethereal.email to test emails. I use this code to fetch and get the emails via IMAP:
public static void main(String[] args) {
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imaps");
props.setProperty("mail.debug", "true");
Folder inbox = null;
Store store = null;
try {
// Connect to the server
System.out.println("Connect to the server");
Session session = Session.getInstance(props);
String host = "imap.ethereal.email";
String username = "qvvaodrw3lvfxboy@ethereal.email";
String password = "4qvcgpNsPhwnQDNgzE";
String provider = "imaps";
store = session.getStore(provider);
store.connect(host, username, password);
// open the inbox folder
System.out.println("open the inbox folder");
inbox = store.getFolder("INBOX");
UIDFolder uf = (UIDFolder) inbox;
inbox.open(Folder.READ_ONLY);
SearchTerm term = new SearchTerm() {
private static final long serialVersionUID = 1L;
public boolean match(Message message) {
Calendar c = Calendar.getInstance();
c.add(Calendar.DAY_OF_MONTH, -1);
Date when = c.getTime();
try {
if (message.getReceivedDate().after(when)) {
return true;
}
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
};
Message[] searchMessages = inbox.search(term);
for (Message msg : searchMessages) {
long uid = uf.getUID(msg);
System.out.println("uid: " + uid);
MimeMessage mimeMsg = (MimeMessage) msg;
Email email = EmailConverter.mimeMessageToEmail(mimeMsg);
String html = email.getTextHTML();
String text = html.substring(0, html.indexOf(DELIMITER));
System.out.println(text);
}
}
catch (Exception ex) {
System.out.println("Thread has a problem...");
ex.printStackTrace();
}
finally {
// close the inbox folder but do not
// remove the messages from the server
try {
inbox.close(false);
} catch (MessagingException e) {
e.printStackTrace();
}
try {
store.close();
} catch (MessagingException e) {
e.printStackTrace();
}
System.out.println("Thread is closing...");
}
}
I got this exception:
java.lang.IllegalArgumentException: value is required
at org.simplejavamail.internal.util.Preconditions.checkNonEmptyArgument(Preconditions.java:15)
at org.simplejavamail.email.Email.addHeader(Email.java:519)
at org.simplejavamail.converter.EmailConverter.fillEmailFromMimeMessage(EmailConverter.java:245)
at org.simplejavamail.converter.EmailConverter.mimeMessageToEmail(EmailConverter.java:56)
I debugged the code and I got that the key of the header is: X-MS-TNEF-Correlator
The value is an empty string.
At this point, only because of a missing header value, I cannot get the email message converted. Isn't it some weird? I can get the other parts of the email by using all the other methods. But here, I think simple-mail should let me have the rest of the email converted even if there are some missing header parts (which I don't even know if we need or not).
You're right, it should be able to convert the email without issue. I'll look into it.
Released as 5.0.0.rc1-SNAPSHOT. Add OSS' snapshots repo to find it in Maven.