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","emailAccount", "emailPwd");

         //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------");
    }
}
相關文章
相關標籤/搜索