码迷,mamicode.com
首页 > 编程语言 > 详细

Java 连接读取Exchange Server邮件服务器

时间:2014-10-06 16:01:10      阅读:412      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   java   sp   div   

import java.security.GeneralSecurityException;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;

import com.sun.mail.util.MailSSLSocketFactory;

public class App 
{
    public static void main( String[] args ) throws MessagingException, GeneralSecurityException
    {
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true); 
        Properties properties = new Properties();
        
        //the host you can give the IP address or the url of Exchange server
         properties.put("mail.imap.host", "10.100.1.x");
         properties.put("mail.imap.starttls.enable", "true");
         properties.put("mail.imap.ssl.socketFactory", sf);
        
         properties.setProperty("mail.imap.starttls.enable", "true");
         properties.setProperty("ssl.SocketFactory.provider", "my.package.name.ExchangeSSLSocketFactory");
         properties.setProperty("mail.imap.socketFactory.class", "my.package.name.ExchangeSSLSocketFactory");
      

         Session emailSession = Session.getDefaultInstance(properties,null);
         emailSession.setDebug(true);
         Store store = emailSession.getStore("imap");

         store.connect("10.100.1.x","cloud_qa@wesoft.com", "ldap4$qa");

         //create the folder object and open it
         Folder emailFolder = store.getFolder("INBOX");
         emailFolder.open(Folder.READ_ONLY);

       
        //get the mail count. or you can do others in here
        Message[] msg = emailFolder.getMessages();
        System.out.println(msg.length);
        System.out.println("----finished------");
    }
}

 

Java 连接读取Exchange Server邮件服务器

标签:style   blog   color   io   os   ar   java   sp   div   

原文地址:http://www.cnblogs.com/andy-zhao/p/4008227.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!