Skip to content

Commit ab90a71

Browse files
committed
Use the date from the From_ line if possible
1 parent 33d23d6 commit ab90a71

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/main/java/org/apache/commons/net/examples/mail/IMAPImportMbox.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public static void main(String[] args) throws IOException
135135
loaded++;
136136
}
137137
} catch (IOException e) {
138-
System.out.println(imap.getReplyString());
138+
System.out.println("Error processing msg: " + total + " " + imap.getReplyString());
139139
e.printStackTrace();
140140
System.exit(10);
141141
return;
@@ -151,15 +151,27 @@ private static boolean startsWith(String input, Pattern pat) {
151151
return m.lookingAt();
152152
}
153153

154+
private static String getDate(String msg) {
155+
final Pattern FROM_RE = Pattern.compile("From \\S+ +\\S+ (\\S+) ?(\\S+) (\\S+) (\\S+)"); // From SENDER Fri Sep 13 17:04:01 2019
156+
// [Fri] Sep 13 HMS 2019
157+
// output date: 13-Sep-2019 17:04:01 +0000
158+
String date = null;
159+
Matcher m = FROM_RE.matcher(msg);
160+
if (m.lookingAt()) {
161+
date = m.group(2)+"-"+m.group(1)+"-"+m.group(4)+" "+m.group(3)+" +0000";
162+
}
163+
return date;
164+
}
165+
154166
private static boolean process(final StringBuilder sb, final IMAPClient imap, final String folder
155167
,final int msgNum) throws IOException {
156168
final int length = sb.length();
157169
boolean haveMessage = length > 2;
158170
if (haveMessage) {
159171
System.out.println("MsgNum: " + msgNum +" Length " + length);
160-
sb.setLength(length-2); // drop trailing CRLF
172+
sb.setLength(length-2); // drop trailing CRLF (mbox format has trailing blank line)
161173
String msg = sb.toString();
162-
if (!imap.append(folder, null, null, msg)) {
174+
if (!imap.append(folder, null, getDate(msg), msg)) {
163175
throw new IOException("Failed to import message: " + msgNum + " " + imap.getReplyString());
164176
}
165177
}

0 commit comments

Comments
 (0)