利用Jmail发带附件的邮件

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

package com.sidiw.util.jmail;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeMessage.RecipientType;

import sun.misc.BASE64Encoder;

public class SendMail {
    /** 文本编码 */
    PRivate String encode;

    /** 文本还是Html内容 */
    private boolean HTML;

    private Session session;

    private Transport transport;

    private BASE64Encoder enc = new sun.misc.BASE64Encoder();

    public SendMail() {
        this.encode = "GBK";
        this.HTML = true;
    }

    public SendMail(String encode, boolean html) {
        this.encode = encode;
        this.HTML = html;
    }

    public boolean connect(String smtpHost, String uid, String pwd) {
        boolean result = false;

        ConnectMail connect = new ConnectMail();
        result = connect.connectSmtp(smtpHost, uid, pwd);
        this.session = connect.getSession();
        this.transport = connect.getTransport();

        return result;
    }

    public void close() {
        if (this.transport != null) {
            try {
                this.transport.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }