java velocity 邮件_邮件集成java mail + 读取邮件模板
項目做異地登錄提醒功能,通過java mail發送郵件。讀取郵件模板sendMail.vm文件。
1.郵件發送
import java.io.StringWriter;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
public class SendMailSenderServiceImpl implements IMailSenderService {
private static final Logger LOG = LoggerFactory.getLogger(SendMailSenderServiceImpl.class);
private String mailTitle;
private String mailContent;
private String smptHost;
private String mailServicePwd;
private String mailServiceUser;
private String sendMail;
private String transProtocol;
private String port;
private String auth;
@Async
public void sendMail(String address, String username, String date, String place) {
if(address.isEmpty()) {
LOG.debug("{} not bind email.",username);
return;
}
Properties props = new Properties();
props.setProperty("mail.smtp.host", smptHost);
props.setProperty("mail.transport.protocol", transProtocol);
props.setProperty("mail.smtp.auth", auth);
props.setProperty("mail.smtp.port", port);
Session session = Session.getInstance(props);
Message message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(sendMail));
message.setRecipient(RecipientType.TO, new InternetAddress(address));
message.setSubject(mailTitle);
message.setContent(getMailContent(mailContent, username, date, place), "text/html;charset=utf-8");
Transport transport = session.getTransport();
transport.connect(smptHost, mailServiceUser, mailServicePwd);
transport.sendMessage(message, message.getAllRecipients());
} catch (Exception e) {
LOG.error("Send mail failed.{}", e);
}
}
使用@Async做異步的調用,避免調用發送郵件的功能時阻塞服務的運行。
mail.smtp.host授權的郵件平臺:smtp.163.com
mail.transport.protocol 郵件傳輸協議:smtp
mail.smtp.auth 當前值默認為false,為false時可以不使用發件郵箱和密碼,設置為null。設置為true則必須校驗用戶名和密碼。
mail.smtp.port 郵件服務端口
mailtitle和mailcontent模板通過xml注入到bean中,記得寫set方法,直接使用的properties中的內容。
這里因為引用的sendMail.vm文件的模板內容,所以需要去將模板內容讀取出來。
模板內容很簡單,需要使用html語法來操作換行和樣式:
尊敬的用戶:
${name},您好!
您的用戶帳號${name}于${date},在${place}進行登錄,存在異地登錄行為。請確保是本人操作。
系統郵件自動發送,請勿回復!
讀取模板內容通過調用方法:
public String getMailContent(String mailContent, String name, String date, String place) {
StringWriter stringWriter = new StringWriter(); // velocity引擎
VelocityEngine velocityEngine = new VelocityEngine(); // 設置文件路徑屬性
Properties properties = new Properties();
String dir = SendMailSenderServiceImpl.class.getResource("/").getPath();
properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, dir); // 引擎初始化屬性配置
try {
velocityEngine.init(properties); // 加載指定模版
Template template = velocityEngine.getTemplate(mailContent, "utf-8"); // 填充模板內容
VelocityContext velocityContext = new VelocityContext();
velocityContext.put("name", name);
velocityContext.put("date", date);
velocityContext.put("place", place);
template.merge(velocityContext, stringWriter);
return stringWriter.toString();
} catch (Exception e) {
LOG.error("Get Mail Content failed.{}", e);
return "fail";
}
}
這里需要注意的就是文件路徑。代碼中的mailContent就是模板文件名sendMail.vm。再就是模板存放的位置,我的存放在classes下面。如果讀取不到,就要debug看一下是不是模板放錯位置了。
這只是簡單的發送簡單的格式的郵件的簡單代碼實現。
總結
以上是生活随笔為你收集整理的java velocity 邮件_邮件集成java mail + 读取邮件模板的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php编译支持mysql,编译php支持
- 下一篇: 群晖服务器性能测试,对群晖DS716+进