利用Javamail接收QQ邮箱和Gmail邮箱

QQ接收邮箱:

 

[java]  view plain  copy
  1. <p>package com.sam.mail;</p><p>import java.io.FileOutputStream;  
  2. import java.io.IOException;  
  3. import java.io.InputStream;  
  4. import java.io.OutputStream;  
  5. import java.io.UnsupportedEncodingException;  
  6. import java.security.Security;  
  7. import java.util.Enumeration;  
  8. import java.util.Properties;</p><p>import javax.mail.BodyPart;  
  9. import javax.mail.FetchProfile;  
  10. import javax.mail.Folder;  
  11. import javax.mail.Header;  
  12. import javax.mail.Message;  
  13. import javax.mail.MessagingException;  
  14. import javax.mail.Multipart;  
  15. import javax.mail.Session;  
  16. import javax.mail.Store;  
  17. import javax.mail.internet.InternetAddress;  
  18. import javax.mail.internet.MimeUtility;</p>  
  19. <p>import org.junit.Test;</p><p>import com.sun.mail.imap.IMAPMessage;</p>  
  20.   
  21.   
  22. <p>/** 
  23.  *  
  24.  * @author  1372940767@qq.com 
  25.  * 
  26.  */  
  27. </p>  
  28. public class EmailClient {  
  29.   
  30. <p> private static final String IMAP = "imap";  
  31.    
  32.  @Test  
  33.  public void testRece() throws Exception{  
  34.   reviceQQEmail(System.getProperty("email"), System.getProperty("password"));  
  35.  }  
  36.   </p><p> public void reviceQQEmail(String username, String password) throws Exception{  
  37.   String host = "imap.qq.com";     
  38.   String port = "143";  
  39.     
  40.   Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());  
  41.     
  42.   final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";  
  43.       
  44.   Properties props = System.getProperties();  
  45.   props.setProperty("mail.imap.socketFactory.class", SSL_FACTORY);  
  46. //  props.setProperty("mail.imap.socketFactory.fallback", "false");  
  47. //     props.setProperty("mail.imap.port", port);  
  48.      props.setProperty("mail.imap.socketFactory.port", port);  
  49.      
  50.     
  51.       props.setProperty("mail.store.protocol","imap");    
  52.    props.setProperty("mail.imap.host", host);    
  53.    props.setProperty("mail.imap.port", port);    
  54.    props.setProperty("mail.imap.auth.login.disable""true");  </p><p>  Session session = Session.getDefaultInstance(props,null);  
  55.   session.setDebug(false);  
  56.   Store store = session.getStore("imap");     
  57.         store.connect(host, username, password);</p><p>  Folder inbox = null;  
  58.   try {  
  59.      
  60.   inbox = store.getFolder("Inbox");  
  61.   inbox.open(Folder.READ_ONLY);  
  62.   FetchProfile profile = new FetchProfile();  
  63.   profile.add(FetchProfile.Item.ENVELOPE);  
  64.   Message[] messages = inbox.getMessages();  
  65.   inbox.fetch(messages, profile);  
  66.   System.out.println("收件箱的邮件数:" + messages.length);  
  67.     
  68.   IMAPMessage msg;  
  69.   for (int i = 0; i < messages.length; i++) {  
  70.    msg = (IMAPMessage)messages[i];  
  71.    String from = decodeText(msg.getFrom()[0].toString());  
  72.    InternetAddress ia = new InternetAddress(from);  
  73.    System.out.println("FROM:" + ia.getPersonal()+'('+ia.getAddress()+')');  
  74.    System.out.println("TITLE:" + msg.getSubject());  
  75.    System.out.println("SIZE:" + msg.getSize());  
  76.    System.out.println("DATE:" + msg.getSentDate());  
  77.     Enumeration headers = msg.getAllHeaders();     
  78.              System.out.println("----------------------allHeaders-----------------------------");     
  79.           while (headers.hasMoreElements()) {     
  80.                  Header header = (Header)headers.nextElement();    
  81.                  System.out.println(header.getName()+" ======= "+header.getValue());    
  82.              }  </p><p>         parseMultipart((Multipart) msg.getContent());   </p><p>  }  
  83.     
  84.   } finally {  
  85.    try {  
  86.     inbox.close(false);  
  87.    } catch (Exception e) {  
  88.    }  
  89.    try {  
  90.     store.close();  
  91.    } catch (Exception e) {  
  92.    }  
  93.   }  
  94.  }</p><p> protected String decodeText(String text) throws UnsupportedEncodingException {  
  95.   if (text == null)  
  96.    return null;  
  97.   if (text.startsWith("=?GB") || text.startsWith("=?gb"))  
  98.    text = MimeUtility.decodeText(text);  
  99.   else  
  100.    text = new String(text.getBytes("ISO8859_1"));  
  101.   return text;</p><p> }  
  102.    
  103.    
  104.     /**    
  105.      * 对复杂邮件的解析   
  106.      * @param multipart   
  107.      * @throws MessagingException   
  108.      * @throws IOException   
  109.      */    
  110.     public static void parseMultipart(Multipart multipart) throws MessagingException, IOException {     
  111.         int count = multipart.getCount();     
  112.         System.out.println("couont =  "+count);     
  113.         for (int idx=0;idx<count;idx++) {     
  114.             BodyPart bodyPart = multipart.getBodyPart(idx);     
  115.             System.out.println(bodyPart.getContentType());     
  116.             if (bodyPart.isMimeType("text/plain")) {     
  117.                 System.out.println("plain................."+bodyPart.getContent());     
  118.             } else if(bodyPart.isMimeType("text/html")) {     
  119.                 System.out.println("html..................."+bodyPart.getContent());     
  120.             } else if(bodyPart.isMimeType("multipart/*")) {     
  121.                 Multipart mpart = (Multipart)bodyPart.getContent();     
  122.                 parseMultipart(mpart);     
  123.                      
  124.             } else if (bodyPart.isMimeType("application/octet-stream")) {     
  125.                 String disposition = bodyPart.getDisposition();     
  126.                 System.out.println(disposition);     
  127.                 if (disposition.equalsIgnoreCase(BodyPart.ATTACHMENT)) {     
  128.                     String fileName = bodyPart.getFileName();     
  129.                     InputStream is = bodyPart.getInputStream();     
  130.                     copy(is, new FileOutputStream("D:\\"+fileName));     
  131.                 }     
  132.             }     
  133.         }     
  134.     }     
  135.          
  136.     /**     
  137.      * 文件拷贝,在用户进行附件下载的时候,可以把附件的InputStream传给用户进行下载     
  138.      * @param is     
  139.      * @param os     
  140.      * @throws IOException     
  141.      */     
  142.     public static void copy(InputStream is, OutputStream os) throws IOException {     
  143.         byte[] bytes = new byte[1024];     
  144.         int len = 0;     
  145.         while ((len=is.read(bytes)) != -1 ) {     
  146.             os.write(bytes, 0, len);     
  147.         }     
  148.         if (os != null)     
  149.             os.close();     
  150.         if (is != null)     
  151.             is.close();     
  152.     }   </p><p>}  
  153. </p>  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值