📄 mailengine.java
字号:
package com.szmx.component.mail;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.VelocityException;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.ui.velocity.VelocityEngineUtils;
import javax.mail.internet.MimeMessage;
import javax.mail.MessagingException;
import java.util.Map;
import java.io.File;
/**
* ================================================================
* Copyright 2006 szmx
* <p/>
* Change Revision
* ---------------------------------------------------------------
* Date Author Remarks
* Mar 20, 2006 BZhang Create class com.szmx.component.mail.MailEngine
* ================================================================
*/
public class MailEngine {
protected static final Log logger = LogFactory.getLog(MailEngine.class);
private JavaMailSender javaMailSender;
private VelocityEngine velocityEngine;
//================================ Setter ===================================
public void setJavaMailSender(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}
public void setVelocityEngine(VelocityEngine velocityEngine) {
this.velocityEngine = velocityEngine;
}
//================================ MimeMail =====================================
public boolean sendMessage(String from, String to, String subject,
boolean isHtml, File[] attachmentFiles,
String velocityTemplateLocation, Map model) {
String textMsg;
try {
textMsg = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, velocityTemplateLocation, model);
} catch (VelocityException e) {
logger.error("Mail Template Error :\n" + e);
return false;
}
try {
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true, "GB2312");
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(textMsg, isHtml);
if (attachmentFiles != null) {
for(int i=0; i<attachmentFiles.length; i++) {
File attFile = attachmentFiles[i];
if (attFile != null && attFile.exists()) {
helper.addAttachment(attFile.getName(), attFile);
}
}
}
javaMailSender.send(message);
logger.info("Sending mail to [" + to +"]");
} catch (MessagingException e) {
logger.error("Mime Message Error :\n" + e);
return false;
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -