zoukankan      html  css  js  c++  java
  • 邮件的解码

    标准规定,Email的头部和邮件体只允许有us-ascii的字符。因此中文的邮件客户端,在发送邮件之前会将邮件的头部和邮件体进行编码,编码的方式有多种,

    比如base64,QP,8-bit等等。在对邮件数据包进行解析的时候,要还原这些解码方式,最方便的方式还是使用javamail。javamail的MimeUtility类封装了很多实用的解码方法,其decode系列方法更是支持很多编码方式。详见javamail-api(http://javamail.kenai.com/nonav/javadocs/javax/mail/internet/MimeUtility.html

         我在做的项目,邮件头是用base64进行编码的,邮件体使用QP编码的,很多邮件客户端是这么做的。解码的代码附上,以供复用。

    public static String getFromBASE64(String s) {
    		if(s == null)
    			return null;
    		BASE64Decoder decoder = new BASE64Decoder();
    		try {
    			byte[] b = decoder.decodeBuffer(s);
    			return new String(b);
    		} catch(Exception e) {
    			System.out.println("wrong decoding.");
    			return null;
    		}
    	}
    	
    	public static String getFromQP(String s) throws IOException,MessagingException {
    		InputStream is = new ByteArrayInputStream(s.getBytes());
    		InputStream rss= MimeUtility.decode(is, "quoted-printable");
    		String rs = inputStream2String(rss);
    		return rs;
    	}
    	
    	private static String inputStream2String(InputStream is) throws IOException{
    		   BufferedReader in = new BufferedReader(new InputStreamReader(is));
    		   StringBuffer buffer = new StringBuffer();
    		   String line = "";
    		   while ((line = in.readLine()) != null){
    		     buffer.append(line);
    		   }
    		   return buffer.toString();
    		}
    
  • 相关阅读:
    cs61b project1
    CS61b lab5
    leetcode DP
    Leetcode 还未解决的bug
    Git使用总结
    Mac TensorFlow Anaconda
    eclipse C++ ld: 1 duplicate symbol for architecture x86_64
    Leetcode Hashtable 1-5
    EC 601 PYTHONPATH
    EC 601 OpenCV Install
  • 原文地址:https://www.cnblogs.com/ITEagle/p/2088828.html
Copyright © 2011-2022 走看看