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

1/5/2008来源:Java教程人气:7525

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;