问题确认
确认JavaMail以及后续的Jakarta Mail均存在解码问题。主要存在于邮件标题的解码部分。涉及到一下几个主要情况
- 同一个中文文字在标题栏编码后进行了换行方案:先解码,最终合并再转换为正确的字符串
- 在编码前已经存在了部分文字方案:编码申明之前的数据需要保存下来,而不是直接将整个字符串认为是原始数据。可以使用正则匹配来识别需要保存的编码等。
- 还有一种就是标题本身没有额外编码,但是没有说明编码导致了问题
- 使用apache-mime4j来进行解码,但其可应对的情况优先,生需要自己额外处理
- 有些邮件编码没有使用mime编码,但是又不是UTF8编码,这时需要使用
juniversalchardet
识别编码
解决方案:
注意:以下代码可能存在资源泄漏,仅作为demo演示。
示例代码,关注其中的decodeString函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
package org.example; import java.io.FileOutputStream; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.lang.reflect.Array; import java.lang.reflect.Method; import java.nio.charset.Charset; import java.util.*; import jakarta.mail.*; import jakarta.mail.internet.MimeBodyPart; import jakarta.mail.internet.MimeMessage; import jakarta.mail.internet.MimeMultipart; import jakarta.mail.internet.MimePart; import javafx.util.Pair; import org.apache.james.mime4j.codec.DecodeMonitor; import org.apache.james.mime4j.codec.DecoderUtil; import org.eclipse.angus.mail.util.BASE64DecoderStream; import org.mozilla.universalchardet.UniversalDetector; import java.util.regex.Matcher; import java.util.regex.Pattern; public class GetRawEmail { static Method mReplaceUnderscores; static Method mDecodeQuotedPrintable; static Method mDecodeBase64; static Method mGetFileName; public static void initDecoder() throws NoSuchMethodException{ mReplaceUnderscores = DecoderUtil.class.getDeclaredMethod("replaceUnderscores", String.class); mReplaceUnderscores.setAccessible(true); //if security settings allow this mDecodeQuotedPrintable = DecoderUtil.class.getDeclaredMethod("decodeQuotedPrintable", String.class, DecodeMonitor.class); mDecodeQuotedPrintable.setAccessible(true); //if security settings allow this mDecodeBase64 = DecoderUtil.class.getDeclaredMethod("decodeBase64", String.class, DecodeMonitor.class); mDecodeBase64.setAccessible(true); mGetFileName = MimeBodyPart.class.getDeclaredMethod( "getFileName", MimePart.class); mGetFileName.setAccessible(true); } public static Pair<String, byte[]> decodeStringImp(String encoded){ try{ String patternStr = "=\\?(\\S+)\\?(\\S)\\?(\\S*)\\?="; Pattern pattern = Pattern.compile(patternStr); Matcher matcher = pattern.matcher(encoded); ArrayList<ArrayList<String> > matches = new ArrayList<>(); while (matcher.find()){ ArrayList<String> items = new ArrayList<>(); for (int i = 1; i <= matcher.groupCount(); i++){ items.add( matcher.group(i) ); } if( items.size() == 3) matches.add( items); } int len = 0; ArrayList<byte[]> bytesArray = new ArrayList<>(); String encode = matches.get(0).get(0); for ( ArrayList<String> items: matches) { String type = items.get(1); String buf = items.get(2); String encodedText = (String)mReplaceUnderscores.invoke(null, buf); if( type.equals("B") || type.equals("b")){ byte[] decoded = (byte[])mDecodeBase64.invoke(null, encodedText, DecodeMonitor.SILENT); len += decoded.length; bytesArray.add(decoded); } else if( type.equals("Q") || type.equals("q")){ byte[] decoded = (byte[])mDecodeQuotedPrintable.invoke(null, encodedText, DecodeMonitor.SILENT); len += decoded.length; bytesArray.add(decoded); } } if( bytesArray.isEmpty()) return new Pair( "", encoded.getBytes()); byte[] decodedBytes = new byte[len]; int index = 0; for ( byte[] bytes: bytesArray) { System.arraycopy( bytes, 0, decodedBytes, index, bytes.length); index += bytes.length; } return new Pair(encode, decodedBytes); } catch (Exception ex){ System.out.println(ex.getMessage()); } return new Pair( "", encoded.getBytes()); } public static String decodeString_1(String encoded) { try{ Pair<String, byte[]> p = decodeStringImp(encoded); String decodedString = new String( p.getValue(), p.getKey()); return decodedString; } catch(Exception ex){ System.out.println(ex.getMessage()); } return encoded; } public static String decodeString(String encoded){ String prefix = "=?"; String seperator = "?"; String suffix = "?="; try{ // 分割字符串 int from_index = 0; String prefix_string = ""; ArrayList<String> lines = new ArrayList<>(); while(true){ // 查找示例 "=?GBK?Q?6057013719-=D6=D0=C8=DA-=C8=D5=BD=F8=B6=B7?=" int line_from = encoded.indexOf(prefix, from_index); if( line_from == -1) break; if( lines.isEmpty()){ // 第一行 prefix_string = encoded.substring(from_index, line_from); } int seperator_index = encoded.indexOf(seperator, line_from+2); if( seperator_index == -1) return encoded; seperator_index = encoded.indexOf(seperator, seperator_index+1); if( seperator_index == -1) return encoded; int line_to = encoded.indexOf(suffix, seperator_index+1); if( line_to == -1) return encoded; from_index = line_to+2; lines.add( encoded.substring(line_from, line_to+2)); } if( lines.isEmpty()) return encoded; ArrayList< byte[]> bytesArray = new ArrayList<>(); ArrayList< String> encodings = new ArrayList<>(); int len = 0; for ( String line: lines) { Pair<String, byte[]> p = decodeStringImp(line); len += p.getValue().length; bytesArray.add(p.getValue()); encodings.add( p.getKey()); } String rev = ""; byte[] decodedBytes = new byte[len]; int index = 0; int last_decode_from = 0; int last_decode_index = 0; for(int i = 0; i < bytesArray.size(); i++ ){ byte[] bytes = bytesArray.get(i); System.arraycopy(bytes, 0, decodedBytes, index, bytes.length); index += bytes.length; } rev += prefix_string; String decodedString = new String( decodedBytes, encodings.get(0)); rev += decodedString; return rev; } catch( Exception ex){ System.out.println(ex.getMessage()); } return encoded; } public static String DecodeBString(String bstring) { return decodeString(bstring); } public static String DecodeQString(String qstring) { return decodeString(qstring); } public static String GetStringByGuessEncoding(String subjectString){ try { byte[] latin1 = subjectString.getBytes("Latin1"); UniversalDetector ud = new UniversalDetector(); ud.handleData(latin1); ud.dataEnd(); String charset = ud.getDetectedCharset(); return new String(latin1, charset); } catch (UnsupportedEncodingException e) { System.out.println(e.getMessage()); } return subjectString; } public static void GetEmail(String[] args) throws Exception { //创建连接属性 Properties props = new Properties(); props.setProperty("mail.store.protocol", "imap"); props.setProperty("mail.imap.host", "imap.263.net"); props.setProperty("mail.imap.port", "993"); props.setProperty("mail.imap.ssl.enable", "true"); //props.setProperty("mail.imap.starttls.enable", "true"); //创建Session Session session = Session.getInstance(props); //创建Store Store store = session.getStore("imap"); store.connect("imap.263.net", 993,"xujinping@zritc.com", "123@zrt"); //打开收件箱 Folder inbox = store.getFolder("INBOX"); inbox.open(Folder.READ_ONLY); //获取最新的邮件 int count = inbox.getMessageCount(); //Message message = messages[messages.length - 1]; //for(int i = count -1; i >=0; i--) for(int i = 34795; i >=0; i--) { if( i % 100 == 0) System.out.println( "processing " + String.valueOf(i)); Message message = inbox.getMessage(i); MimeMessage mimeMessage = (MimeMessage) message; String subjects[] = mimeMessage.getHeader("Subject"); if( true || subjects[0].contains("?GBK?B?") ) { System.out.println( "message index:" + String.valueOf(i) ); String subject = GetStringByGuessEncoding( subjects[0]); Enumeration<String> rrr = mimeMessage.getAllHeaderLines(); subject = decodeString(subject); System.out.println("Origin: " + subjects[0]); System.out.println("Subject: " + subject); String mimeFilename = (String)mGetFileName.invoke(null, mimeMessage); } MimeMultipart content = (MimeMultipart)mimeMessage.getContent(); for(int bpi = 0; bpi != content.getCount(); bpi++){ BodyPart bp = content.getBodyPart(bpi); String filename = bp.getFileName(); if( filename != null && !filename.isEmpty()){ FileOutputStream bpf = new FileOutputStream(filename); InputStream base64S = (InputStream)bp.getContent(); byte[] buff = new byte[1024*1024]; for(int rlen = 0; rlen != -1; ){ rlen = base64S.read(buff); if( rlen != -1) bpf.write(buff, 0, rlen); } System.out.println( "BodyPart[" + String.valueOf(bpi) + "]: " + filename); } } } //关闭连接 inbox.close(false); store.close(); } public static void main(String[] args) throws Exception { initDecoder(); GetEmail(args); } } |
其中使用到的packages
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<dependencies> <dependency> <groupId>jakarta.mail</groupId> <artifactId>jakarta.mail-api</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>org.eclipse.angus</groupId> <artifactId>angus-mail</artifactId> <version>2.0.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.james</groupId> <artifactId>apache-mime4j-james-utils</artifactId> <version>0.8.9</version> </dependency> <dependency> <groupId>com.github.albfernandez</groupId> <artifactId>juniversalchardet</artifactId> <version>2.4.0</version> </dependency> </dependencies> |