Jmail收发邮件时的连接验证类

发表于:2007-06-22来源:作者:点击数: 标签:
package com.sidiw.util.jmail; import java .util.Properties; import javax.mail.Session; import javax.mail.Store; import javax.mail.Transport; public class ConnectMail { private Session session; private Transport transport; private Store sto

   

package com.sidiw.util.jmail;

import java.util.Properties;

import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;

public class ConnectMail {

    private Session session;

    private Transport transport;

    private Store store;

    private Properties properties;

    /** 返回过的实例在验证通过后使用 */

    public Session getSession() {
        return session;
    }

    public Store getStore() {
        return store;
    }

    public Transport getTransport() {
        return transport;
    }

    public boolean connectSmtp(String smtpHost, String uid, String pwd) {
        try {
            properties = new java.util.Properties();
            properties.put("mail.smtp.host", smtpHost);

            properties.put("mail.smtp.auth", "true");
            Auth auth = new Auth(uid, pwd);
            session = Session.getInstance(properties, auth);

            transport = session.getTransport("smtp");
            transport.connect();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean connectPop3(String popHost, String uid, String pwd) {

        try {
            properties = new java.util.Properties();

            session = Session.getDefaultInstance(properties, null);
            store = session.getStore("pop3");
            store.connect(popHost, uid, pwd);

            return true;

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    } 
}

原文转自:http://www.ltesting.net