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

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","[email protected]", "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------");
    }
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。