审查视图

src/com/espeed/tool/AnalysisHttpMail.java 7.1 KB
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
package com.espeed.tool;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.mail.BodyPart;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

import com.espeed.pojo.YxySendMailMaster;

/***
 * 
 * @author xieyong
 * HTTP解析邮件
 */
public  class AnalysisHttpMail {

	private  MimeMessage message;
	
	//HTTP解析邮件
	public YxySendMailMaster ParseHttpEmlFile(InputStream in,YxySendMailMaster masterinfo){
		try {
			//获取邮件文件
			Session mailSession = Session.getDefaultInstance(System.getProperties(), null);
			message = new MimeMessage(mailSession, in);
			//解析主题
			String subject=paxSubject();
			masterinfo.setSubject(subject);//主题
			//解析发件人
			List<String> senderList=paxSender();
			String sender="";
			String sendaddress="";
			if(senderList.get(0)!=null&&!senderList.get(0).equals("")){			
				masterinfo.setSender(senderList.get(0));//发件人姓名
			}
			if(senderList.get(1)!=null&&!senderList.get(1).equals("")){			
				masterinfo.setSend_email(senderList.get(1));//发件人地址
			}
			//解析回复人
			String replyer=paxReply();
			masterinfo.setReply_email(replyer);//回复地址
			
			//解析图片附件
			//masterinfo.setBody(httpbody);//内容
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return masterinfo;
	}
	
	//解析邮件主题
	public String paxSubject(){
    	String subject="";
    	try {  
    		if(message.getSubject()!=null){
    			subject=message.getSubject();
    			String newsubject=new String(subject.getBytes("ISO-8859-1"));	    			
    			if(newsubject.indexOf("??")==-1){
    				subject=newsubject;
    			}
    			if(message.getSubject().contains("=?")||message.getSubject().contains("?B")){
	    			subject=subject.replace(" ", "");
	    			String[] newStr=subject.split("=\\?");
	    			//是不是GB2312字符
	    			if (subject.toLowerCase().indexOf("=?gb2312?") != -1) {
	    				subject=subject.replaceAll("=\\?[gG][bB]2312\\?", " =?gbk?");
					}else if(subject.indexOf("?==?")!=-1){//多个组成
						StringBuffer newSubject=new StringBuffer();
						for(int i=0;i<newStr.length;i++){
							if(i==0){
								newSubject.append(MimeUtility.decodeText(newStr[i]));
							}else{
								newSubject.append(MimeUtility.decodeText("=?"+newStr[i]));
							}
						}
						subject=newSubject.toString();
					}else if(newStr[0]==null||newStr[0].equals("")){//以=?开头的
						subject=MimeUtility.decodeText(subject);
					}else if(newStr[0]!=null&&!newStr[0].equals("")){//不以=?开头的
						int end=subject.indexOf("=?");
						String indexstr=subject.substring(0,end);
						String endStr=subject.substring(end,subject.length());
						subject=indexstr+MimeUtility.decodeText(endStr);
					}
					subject=MimeUtility.decodeText(subject);
    			}
    		}else{
    			String[] subjectAttr=message.getHeader("subject");
    			if(subjectAttr==null||subjectAttr.equals("")){
    				subject = "无主题";
    			}else{
    				subjectAttr[0]=subjectAttr[0].replace("?=\r\n =?utf-8?B?", "");//特殊情况1
	    			if (subjectAttr[0].toLowerCase().indexOf("?gb2312?") != -1) {
	    				subject=subjectAttr[0].replaceAll("\\?[gG][bB]2312\\?", "?gbk?");
	    				subject=MimeUtility.decodeText(subject);
					}else{
						subject=MimeUtility.decodeText(subjectAttr[0]);
					}
	    			String newstr=subject.replace("?", "");
	    			newstr=new String(newstr.getBytes("iso-8859-1"));
	    			if(!newstr.contains("?")){
	    				subject=new String(message.getSubject().getBytes("iso-8859-1"));
	    			}
    			}		
    		}
		if(subject == null||subject.equals(""))   
			subject = "无主题"; 
        } catch (Exception e) {
        	e.printStackTrace();
        }   
        return subject;   
	}

	//解析发件人
	public List<String> paxSender(){
		 
		List<String> fromaddr=new ArrayList<String>();
		String sendName = "";
		String sendEmail = "";
		String str="";
		String[] Address;
		try {
 			Address=message.getHeader("From");			
 			if(Address.length>=2){//有姓名与地址
 			}else{//只有地址
 				if(Address[0]==null){
 					Address[0]="";
 					fromaddr.add(Address[0]);
 				}else{
 					String newAddress=new String(Address[0].getBytes("iso-8859-1"));
 					if(newAddress.contains("=")||newAddress.contains("?")){
 						newAddress=newAddress.replaceAll(" ", "");
 						newAddress=newAddress.replaceAll("\"", "");
 						if (newAddress.toLowerCase().indexOf("?gb2312?") != -1) {
 							newAddress=newAddress.replaceAll("\\?[gG][bB]2312\\?", "?gbk?");
 						}
 						if(newAddress.indexOf("?==?")!=-1){
 							String []newStrArry=newAddress.split("\\?==\\?");
 							
 							String newStr=newStrArry[0]+"?=";
 							str=MimeUtility.decodeText(newStr);
 							
 							newStr="=?"+newStrArry[1];
 							str+=MimeUtility.decodeText(newStr);
 							
 							
 						}else{
 							str=MimeUtility.decodeText(newAddress);
 						}
 						if(fromaddr.contains("=")||fromaddr.contains("?")){
 							str=Address[0];
 						}
 						
 						
 					}else{
 						str=newAddress;
 					}
 				}
 			}
 			str=str.replaceAll("\"", "");
			//分割发件人姓名与地址
			Address=str.split("<");
			if(Address.length>=2){
				Address[0]=Address[0].replaceAll("\\s", "");
				sendName=MimeUtility.decodeText(Address[0]);
				sendEmail=Address[1].replaceAll(">", "");
			}else{
				fromaddr.add("");
				fromaddr.add(Address[0]);
			}
			
			fromaddr.add(sendName);
			fromaddr.add(sendEmail);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fromaddr;
	    
	}

	//获取回复人
	public String paxReply(){		 
		String sendEmail = "";//发送人地址
		String str="";
		String[] Address;
		try {
 			Address=message.getHeader("Reply-To");
 			if(Address!=null){
 				if(Address[0]!=null){//是否存在回复地址	
 					//分割地址
 					Address=Address[0].split("<");
 					if(Address.length>=2){
 						sendEmail=Address[1].replaceAll(">", "");
 					}else{
 						sendEmail=Address[0];
 					}
 					
 				}
 			}
		} catch (Exception e) {
			e.printStackTrace();
		}
			
		return sendEmail;
	    
	}

	//解析图片与附件
	//public List<String> paxAttrPic(Part part){}
	
	//保存文件
	public void saveFile(String fileName,InputStream in,String savePath){
		OutputStream os=null;
		try {
			File file=new File(savePath);
			file.mkdirs();
			//将流生成文件
			File writeFile=new File(savePath+"\\"+fileName);
			os=new FileOutputStream(writeFile);
			byte buffer[]=new byte[4*1024];
			while((in.read(buffer))!=-1){
				os.write(buffer);
			}
		} catch (Exception e) {
			System.out.println("生成文件失败!");
		}finally{
			try {
				os.flush();
				os.close();
			} catch (IOException e) {
				System.out.println("流关闭失败!");
			}
		}
	}
}