介绍:spring mvc 实现邮件的发送原理很简单,就是依赖邮件服务器POP3/SMTP协议,只要配置好,一切就很简单,自己也摸索了好久才搞定。
第一步:创建邮件实体类Email
public class Email {
/**
* 发件人邮箱服务器
*/
private String emailHost;
/**
* 发件人邮箱
*/
private String emailFrom;
/**
* 发件人用户名
*/
private String emailUserName;
/**
* 发件人邮件密码
*/
private String emailPassword;
/**
* 收件人邮箱,多个邮箱以“;”分隔
*/
private String toEmails;
/**
* 邮件主题
*/
private String subject;
/**
* 邮件内容
*/
private String content;
/**
* 邮件中的图片,为空时无图片。map中的key为图片ID,value为图片地址
*/
private Map<String, String> pictures;
/**
* 邮件中的附件,为空时无附件。map中的key为附件ID,value为附件地址
*/
private Map<String, String> attachments;
//setter and getter
}
第二步:创建邮件工具类
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
public class EmailUtil(){
private static Logger log = Logger.getLogger(EmailUtil.class);
private String excelPath = "d://";
private JavaMailSender javaMailSender;
private SimpleMailMessage simpleMailMessage;
public