JavaMail发送多联系人多附件邮件

一.准备

1.需要的Jar包

需要的Jar包有

activation1.1.jar
javax.mail.1.4.7.jar

Maven方式:

<properties>
    <javamail.version>1.4.7</javamail.version>
</properties>
...
...
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>${javamail.version}</version>
</dependency>

普通方式:

手动找吧~

二.代码

1.工具类

package com.xxxx.xxxx.util;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

/**
 * NAME   :  EmailSystem/com.amayadream.emailsystem.util
 * Author :  Amayadream
 * Date   :  2015.12.04 14:00
 * TODO   :  邮件发送工具类
 */
public class MailUtil {

    // -- 验证要素 -- //
    private String sendhost;    //邮件服务器
    private String sendport;    //端口号
    private String sendmail;    //发信邮箱
    private String sendpass;    //发信密码

    //-- 邮件内容 -- //
    private String subject;     //邮件主题
    private String sendname;    //发信昵称
    private String content;     //邮件内容
    private String[] receiver;  //收件人,类型为数组
    private String[] cc;        //抄送,类型为数组
    private String[] bcc;       //密送,类型为数组
    private File[] files;       //附件,类型为数组

    private MimeMessage message;
    private Session session;
    private Transport transport;

    /**
     * 初始化
     */
    public void init(String sendhost, String sendport, String sendmail, String sendpass, String subject,
                        String sendname, String content, String[] receiver, String[] cc, String[] bcc, File[] files){
        this.sendhost = sendhost;
        this.sendport = sendport;
        this.sendmail = sendmail;
        this.sendpass = sendpass;
        this.subject = subject;
        this.sendname = sendname;
        this.content = content;
        this.receiver = receiver;
        this.cc = cc;
        this.bcc = bcc;
        this.files = files;
    }


    /**
     * 获取session对象
     * @param debug     是否开启debug模式
     * @return
     */
    public Session getSession(boolean debug){
        Properties properties = new Properties();
        properties.put("mail.smtp.host",sendhost);
        properties.put("mail.smtp.port",sendport);
        properties.put("mail.sender.username",sendmail);
        properties.put("mail.sender.password",sendpass);
        session = Session.getInstance(properties);
        session.setDebug(debug);// 开启后有调试信息
        return session;
    }

    /**
     * 设置发信的内容
     * @param session
     * @return
     */
    public MimeMessage getMessage(){
        message = new MimeMessage(session);
        try {
            // 邮件主题
            message.setSubject(subject);
            // 发信时间
            message.setSentDate(new Date());
            // 发件人
            if(sendname != null){
                InternetAddress from = new InternetAddress(sendmail, sendname);
                message.setFrom(from);
            }else{
                InternetAddress from = new InternetAddress(sendmail);
                message.setFrom(from);
            }
            // 收件人
            InternetAddress[] to = new InternetAddress[receiver.length];
            for (int i = 0; i < receiver.length; i++) {
                to[i] = new InternetAddress(receiver[i]);
            }
            message.setRecipients(Message.RecipientType.TO, to);
            if (cc != null) {
                InternetAddress[] add_cc = new InternetAddress[cc.length];
                for (int i = 0; i < cc.length; i++) {
                    add_cc[i] = new InternetAddress(cc[i]);
                }
                message.setRecipients(Message.RecipientType.CC, add_cc);
            }
            if (bcc != null) {
                InternetAddress[] add_bcc = new InternetAddress[bcc.length];
                for (int i = 0; i < bcc.length; i++) {
                    add_bcc[i] = new InternetAddress(bcc[i]);
                }
                message.setRecipients(Message.RecipientType.BCC, add_bcc);
            }
            // 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
            Multipart multipart = new MimeMultipart();
            // 添加邮件正文
            BodyPart contentPart = new MimeBodyPart();
            contentPart.setContent(content, "text/html;charset=UTF-8");
            multipart.addBodyPart(contentPart);
            if (files != null) {
                for (int i = 0; i < files.length; i++) {
                    BodyPart attachmentBodyPart = new MimeBodyPart();
                    DataSource source = new FileDataSource(files[i]);
                    attachmentBodyPart.setDataHandler(new DataHandler(source));

                    //MimeUtility.encodeWord可以避免文件名乱码
                    attachmentBodyPart.setFileName(MimeUtility.encodeWord(files[i].getName()));
                    multipart.addBodyPart(attachmentBodyPart);
                }
            }
            // 将multipart对象放到message中
            message.setContent(multipart);
            // 保存邮件
            message.saveChanges();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (AddressException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return message;
    }

    /**
     * 发送邮件
     * @param message
     * @param session
     * @return
     */
    public String sendMail(boolean debug){
        session = getSession(debug);
        message = getMessage();
        try {
            transport = session.getTransport("smtp");
            // smtp验证,就是你用来发邮件的邮箱用户名密码
            transport.connect(sendhost, sendmail, sendpass);
            transport.sendMessage(message, message.getAllRecipients());
            return "success";
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
            return "providerError";
        } catch (MessagingException e) {
            e.printStackTrace();
            return "messageError";//密码错误在这里触发,网络连接失败也在这里触发
        } finally {
            if (transport != null) {
                try {
                    transport.close();
                } catch (MessagingException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

2.使用方法

package com.amayadream.emailsystem.util;

import java.io.File;

/**
 * NAME   :  EmailSystem/com.amayadream.emailsystem.util
 * Author :  Amayadream
 * Date   :  2015.12.04 17:25
 * TODO   :
 */
public class SendEmail {
    public static void main(String[] args) {
        MailUtil mailUtil = new MailUtil();
        File file = new File("C:\\Users\\Administrator\\Desktop\\QQ截图20151203152914.png");
        File file2 = new File("C:\\Users\\Administrator\\Desktop\\QQ截图20151203152706.png");
        File[] files = new File[] {file,file2};   //多附件
        String[] sendto = {"[email protected]","[email protected]"};//收信人
        String[] sendcc = {"[email protected]"};//抄送
        String[] sendbcc = {"[email protected]"};//密送
        String sendhost = "smtp.xx.com";     //邮件服务器
        String sendport = "25";              //端口号
        String sendmail = "xxx.xx.com";      //发信邮箱
        String sendpass = "********";        //邮箱密码
        String subject = "听说明天天气不错"; //邮件主题
        String sendname = "宇宙大魔王";      //发信昵称
        String content = "<h2>明天一起出来玩</h2>";    //邮件内容

        //初始化赋值,其中最后三项(抄送.密送和附件是可选项,如果不需要则直接赋值为null即可)
        mailUtil.init(sendhost,sendport,sendmail,sendpass,subject,sendname,content,sendto,sendcc,sendbcc,files);
        //发送邮件,true为开启debug模式,如果不需要赋值为false即可
        mailUtil.sendMail(true);
    }
}

这里存在的问题就是无法判断发送过程中出现什么错误….待修复

猜你喜欢

转载自blog.csdn.net/Amayadream/article/details/50177813
今日推荐