import java.io.Serializable;
public class EmailVO implements Serializable{
private static final long serialVersionUID = 1L;
private String mail;
private String toName;
private String subject;
private String body;
private String showName;
private String recommend;
public String getRecommend() {
return recommend;
}
public void setRecommend(String recommend) {
this.recommend = recommend;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getToName() {
return toName;
}
public void setToName(String toName) {
this.toName = toName;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getShowName() {
return showName;
}
public void setShowName(String showName) {
this.showName = showName;
}
}
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
public class EmailUtil {
@Autowired
private JavaMailSender javaMailSender;
/**
* 邮件发送<br>
* subject 邮件主题<br>
* text 邮件正文<br>
* to 收件人邮箱<br>
*/
public void sendHtmlEmail(String subject, String text, String to) throws MessagingException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, "UTF-8");
String from = PropertyUtil.getProp("EMAILUSERNAME");
if (null == to || "".equals(to.trim())) {
to = PropertyUtil.getProp("TOEMAILUSERNAME");
}
if (StringUtil.isEmpty(from)) {
throw new MessagingException(PropertyUtil.getProp("SEND_EMAIL_FROM_IS_NOT_NULL"));
}
if (StringUtil.isEmpty(to)) {
throw new MessagingException(PropertyUtil.getProp("SEND_EMAIL_TO_IS_NOT_NULL"));
}
if (StringUtil.isEmpty(subject)) {
throw new MessagingException(PropertyUtil.getProp("SEND_EMAIL_SUBJECT_IS_NOT_NULL"));
}
if (StringUtil.isEmpty(text)) {
throw new MessagingException(PropertyUtil.getProp("SEND_EMAIL_TEXT_IS_NOT_NULL"));
}
if (!StringUtil.isEmail(from)) {
throw new MessagingException(PropertyUtil.getProp("SEND_EMAIL_FROM_IS_NOT_EMAIL"));
}
String[] tos = to.split(",");
for (String t : tos) {
if (!StringUtil.isEmail(t)) {
throw new MessagingException(PropertyUtil.getProp("SEND_EMAIL_TO_IS_NOT_EMAIL"));
}
}
mimeMessageHelper.setFrom(from);
mimeMessageHelper.setTo(tos);
mimeMessageHelper.setText(text, true);
mimeMessageHelper.setSubject(subject);
javaMailSender.send(mimeMessage);
}
}