发送带附件的邮件

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class EmailUtils {
    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.put("from", "xxxx@163.com");
        properties.put("password", "****");
        String content = "this is a test email content";
        String subject = "this is a test email subject";
        List<String> filepathList = new ArrayList<String>();
        filepathList.add("C:\Documents and Settings\Administrator\桌面\测试.docx");
        List<String> toList = new ArrayList<String>();
        toList.add("xxxx@163.com");
        send(properties, subject, content, filepathList, toList);
    }
    
    public static boolean send(Properties properties,String subject,String content,List<String> filepathList,List<String> toList){
        String from = properties.getProperty("from");
        String passwd = properties.getProperty("password");
        String smtpServer = null;
        if(!properties.containsKey("smtpServer")){
            smtpServer = getTriableSmtpServer(from);
        }else{
            smtpServer = properties.getProperty("smtpServer");
        }
        return send(from, passwd, subject, content, smtpServer, filepathList, toList);
        
    }
    
    public static boolean send(String from ,String passwd,String subject,String content,List<String> filepathList,
            List<String> toList){
        String smtpServer = getTriableSmtpServer(from);
        return send(from, passwd, subject, content, smtpServer, filepathList, toList);
    }

    private static String getTriableSmtpServer(String from) {
        int begin = from.indexOf("@");
        int end = from.indexOf(".");
        String smtpServer = "smtp."+from.substring(begin+1,end)+".com";
        return smtpServer;
    }

    public static boolean send(String from, String passwd, String subject,
            String content, String smtpServer, List<String> filepathList,
            List<String> toList) {
        Properties properties = System.getProperties();
        properties.setProperty("mail.smtp.auth", "false");
        Session session = Session.getInstance(properties);
        session.setDebug(true);
        MimeMessage message = new MimeMessage(session);
        try {
            InternetAddress fromAddress = new InternetAddress(from);
            message.setFrom(fromAddress);

            InternetAddress[] toAddressArray = new InternetAddress[toList
                    .size()];
            for (int i = 0; i < toAddressArray.length; i++) {
                toAddressArray[i] = new InternetAddress(toList.get(i));
            }
            message.setRecipients(Message.RecipientType.TO, toAddressArray);
            message.setSubject(subject);

            if (null == filepathList || filepathList.size() == 0) {
                message.setContent(content, "text/html;charset=GBK");
            } else {
                MimeBodyPart mimeBodyPart = new MimeBodyPart();
                mimeBodyPart.setContent(content, "text/html;charset=GBK");
                Multipart multipart = new MimeMultipart();
                multipart.addBodyPart(mimeBodyPart);

                for (String filepath : filepathList) {
                    mimeBodyPart = new MimeBodyPart();
                    DataSource ds = new FileDataSource(filepath);
                    mimeBodyPart.setDataHandler(new DataHandler(ds));
                    filepath = new String(new File(filepath).getName().getBytes(), "GBK");
                    mimeBodyPart.setFileName(filepath);
                    // mimeBodyPart.setFileName(new File(filepath).getName());
                    multipart.addBodyPart(mimeBodyPart);
                }
                message.setContent(multipart);
            }
            message.saveChanges();
            Transport transport = session.getTransport("smtp");
            transport.connect(smtpServer, from, passwd);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        } catch (AddressException e) {
            e.printStackTrace();
            return false;
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
原文地址:https://www.cnblogs.com/dreammyle/p/5156537.html