在邮件处理的时候,一般情况下使用JavaMail就可以处理好发件人和收件人了。但是遇到一些畸形邮件,比如发件人和收件人的格式不太规范的问题,使用JavaMail处理就可能出问题了。这就需要自己在邮件处理流程上增加一些判断和异常处理。
捕捉JavaMail在进行邮件解析产生的异常,然后根据不同异常调用自己写的邮件地址处理函数。就可以把这一类畸形邮件正确处理。邮件内容提取函数的部分代码片段如下:
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
| private void parseEmail() throws Exception { java.util.Properties properties = System.getProperties(); Session session = Session.getDefaultInstance(properties); InputStream fis = null; try { fis = Files.newInputStream(emailFile); email = new MimeMessage(session, fis); try { set_bcc(email.getRecipients(RecipientType.BCC)); } catch (MessagingException e) { if(e.getMessage().indexOf("Missing \'<\'")!=-1) {
set_bcc(parse(email.getHeader("Bcc",","))); } else { log.error("BCC Exception : "+e.getMessage()); } } try { set_cc(email.getRecipients(RecipientType.CC)); } catch (MessagingException e) { if(e.getMessage().indexOf("Missing \'<\'")!=-1) {
set_cc(parse(email.getHeader("Cc",","))); } else { log.error("CC Exception : "+e.getMessage()); } } try { set_to(email.getRecipients(RecipientType.TO)); } catch (MessagingException e) { if(e.getMessage().indexOf("Missing \'<\'")!=-1) {
set_to(parse(email.getHeader("To",","))); } else { log.error("TO Exception : "+e.getMessage()); } } try { set_from(email.getFrom()); } catch (MessagingException e) { if(e.getMessage().indexOf("Missing \'<\'")!=-1) {
set_from(parse(email.getHeader("From",","))); } else { log.error("From Exception : "+e.getMessage()); } }
|
这是我自己写的异常邮件处理函数:(遇到异常的时候就调用我自己写的这个函数进行邮件收发人信息提取):
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
| public static InternetAddress[] parse(String s){ int start_personal = -1; int end_personal = -1; int start = -1; int end = -1; int length = s.length(); boolean res = false; boolean sep1 = false; boolean sep2 = false; boolean sep3 = false; int loc1 = -1; Vector v = new Vector(); InternetAddress ma; IdentityHashMap<String,String> tmpresult = new IdentityHashMap<>(); for(int index=0;index<length;index++){ char c = s.charAt(index); switch (c){ case '\t': case '\n': case '\r': case ' ': case ',': break; case '\"': if(start!=-1) sep3=true; break; case '<': loc1 = index; int nameindex = loc1; index++; lableemail: for(;index<length;++index) { c = s.charAt(index); switch (c){ case '<': sep1 = true; nameindex = index; break; case '>': if((index<length-1)&&(c==s.charAt(index+1))) sep2=true; res = true; break lableemail; } } if(sep1) start_personal = loc1+1; else start_personal = start; if(sep3) end_personal = nameindex-2; else end_personal = nameindex; start = nameindex+1; end = index; break; default: if(start==-1){ start=index; } } if(res){ String name = s.substring(start_personal,end_personal).trim(); String email = s.substring(start,end).trim(); if(tmpresult.size()==0) { tmpresult.put(name,email); } else { for(Map.Entry<String,String> entry:tmpresult.entrySet()){ if ((entry.getKey().equals(name)!=true)||(entry.getValue().equals(email)!=true)){ tmpresult.put(name,email); break; } } } res = false; start_personal = -1; end_personal = -1; start = -1; end = -1; sep1=false; sep2=false; sep3=false; } if(sep2) index++; } for(Map.Entry<String,String> entry:tmpresult.entrySet()){ ma = new InternetAddress(); ma.setAddress(entry.getValue()); try { ma.setPersonal(entry.getKey()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } v.addElement(ma); } InternetAddress[] var = new InternetAddress[v.size()]; v.copyInto(var); return var; }
|
基本上,我这里项目ß出现的To、From、Cc、Bcc不符合规范的畸形邮件就可以处理好了。