java 发送邮件类

摘要

java发送邮件功能是最常见的功能,下面是一个完整邮件功能代码,包含邮件发送,抄送,密送,发送附件,发送图片,详细内容请参考下面代码。

maven配置:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>4.2.7.RELEASE</version>
</dependency>
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.7</version>
</dependency>


mailUtail.java

com.xx.xx.biz.utils;

com.xx.xx.common.dataobj.EmailDO;
org.springframework.core.io.FileSystemResource;
org.springframework.mail.javamail.JavaMailSenderImpl;
org.springframework.mail.javamail.MimeMessageHelper;

javax.mail.Session;
javax.mail.internet.MimeMessage;
java.io.File;
java.util.*;


MailUtil {

    sendEmail(EmailDO bo,String[] toEmailArray,String[] ccEmailArray) Exception {
        (Constants..equals() ||Constants..equals()
                || Constants..equals()
                || Constants..equals()) {
            RuntimeException();
        }

        JavaMailSenderImpl senderImpl = JavaMailSenderImpl();

        senderImpl.setHost(Constants.);

        MimeMessage mailMessage = senderImpl.createMimeMessage();

        MimeMessageHelper messageHelper = ;
        messageHelper = MimeMessageHelper(mailMessage, , );
        messageHelper.setFrom(Constants.);

        List<String> toEmailList = ArrayList<String>();
        (== toEmailArray || toEmailArray.<= ) {
            RuntimeException();
        } {
            (String s : toEmailArray) {
                s = DataUtil.(s);
                (!s.equals()) {
                    toEmailList.add(s);
                }
            }
            (== toEmailList || toEmailList.size() <= ) {
                RuntimeException();
            } {
                toEmailArray = String[toEmailList.size()];
                (i = ; i < toEmailList.size(); i++) {
                    toEmailArray[i] = toEmailList.get(i);
                }
            }
        }
        messageHelper.setTo(toEmailArray);
        (!=ccEmailArray && ccEmailArray.>){
            messageHelper.setCc(ccEmailArray);
        }

        messageHelper.setSubject(bo.getSubject());

        messageHelper.setText(bo.getContent(), );

        (!= bo.getPictures()) {
            (Iterator<Map.Entry<String, String>> it = bo.getPictures().entrySet()
                    .iterator(); it.hasNext();) {
                Map.Entry<String, String> entry = it.next();
                String cid = entry.getKey();
                String filePath = entry.getValue();
                (== cid || == filePath) {
                    RuntimeException();
                }

                File file = File(filePath);
                (!file.exists()) {
                    RuntimeException(+ filePath + );
                }

                FileSystemResource img = FileSystemResource(file);
                messageHelper.addInline(cid, img);
            }
        }

        (!= bo.getAttachments()) {
            (Iterator<Map.Entry<String, String>> it = bo.getAttachments()
                    .entrySet().iterator(); it.hasNext();) {
                Map.Entry<String, String> entry = it.next();
                String cid = entry.getKey();
                String filePath = entry.getValue();
                (== cid || == filePath) {
                    RuntimeException();
                }

                File file = File(filePath);
                (!file.exists()) {
                    RuntimeException(+ filePath + );
                }

                FileSystemResource fileResource = FileSystemResource(file);
                messageHelper.addAttachment(cid, fileResource);
            }
        }

        Properties prop = Properties();
        prop.put(, ); prop.put(, );
        MyAuthenticator auth = MyAuthenticator(Constants., Constants.);

        Session session = Session.(prop, auth);
        senderImpl.setSession(session);

        senderImpl.send(mailMessage);
    }

    main(String[] args) Exception {
        MailUtil mu = MailUtil();
        }

}
Constants.java

/**
 * xx.com Inc.
 * copyright (c) 2015-2016 All Rights Reserved.
 */

package com.xxx.xxx.biz.utils;


import org.apache.log4j.Logger;

import java.util.Map;

/**
 *@author wgy
 *@version Id:Constants.java, v 0.1 2016/9/6 9:00 wgy
 */
public class Constants {
    private static Logger logger = Logger.getLogger(Constants.class);
    /**
     * 发件人邮箱服务器
     */
    public static String emailHost;
    /**
     * 发件人邮箱
     */
    public static String emailFrom;

    /**
     * 发件人用户名
     */
    public static String emailUserName;

    /**
     * 发件人密码
     */
    public static String emailPassword;

    /**
     * 收件人邮箱,多个邮箱以“;”分隔
     */
    public static String toEmails;

    /***
     * 抄送人
     */
    public static String ccEmails;
    /**
     * 邮件主题
     */
    public static String subject;
    /**
     * 邮件内容
     */
    public static String content;
    /**
     * 邮件中的图片,为空时无图片。map中的key为图片ID,value为图片地址
     */
    public static Map<String, String> pictures;
    /**
     * 邮件中的附件,为空时无附件。map中的key为附件ID,value为附件地址
     */
    public static Map<String, String> attachments;



    public void setEmailHost(String emailHost) {
        this.emailHost = emailHost;
    }

    public String getEmailHost() {
        return emailHost;
    }

    public void setEmailFrom(String emailFrom) {
        this.emailFrom = emailFrom;
    }

    public String getEmailFrom() {
        return emailFrom;
    }

    public void setEmailUserName(String emailUserName) {
        this.emailUserName = emailUserName;
    }

    public String getEmailUserName() {
        return emailUserName;
    }

    public void setEmailPassword(String emailPassword) {
        this.emailPassword = emailPassword;
    }

    public String getEmailPassword() {
        return emailPassword;
    }

    public void setToEmails(String toEmails) {
        this.toEmails = toEmails;
    }

    public String getToEmails() {
        return toEmails;
    }

    public void setCcEmails(String ccEmails) {
        this.ccEmails = ccEmails;
    }

    public String getCcEmails() {
        return ccEmails;
    }


    public void init() {
        logger.info("emailHost=========:" + emailHost);

    }

}

EmailDO.java

/**
 * xxx.com Inc.
 * copyright (c) 2015-2016 All Rights Reserved.
 */

package com.xxx.xxxx.common.dataobj;


import java.util.Map;

/**
 *@author wgy
 *@version Id:EmailDO.java, v 0.1 2016/9/7 19:56 wgy
 */
public class EmailDO {

    private String subject;
    private String content;
    private Map<String, String> pictures;
    private Map<String, String> attachments;

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Map<String, String> getPictures() {
        return pictures;
    }

    public void setPictures(Map<String, String> pictures) {
        this.pictures = pictures;
    }

    public Map<String, String> getAttachments() {
        return attachments;
    }

    public void setAttachments(Map<String, String> attachments) {
        this.attachments = attachments;
    }
}
IT家园
IT家园

网友最新评论 (0)