1、配置文件
配置一个properties文件,放在src目录下
配置文件的内容如下:
2、获取配置文件内容
//获取配置文件内容
Properties props=new Properties();
props.load(this.getClass().getClassLoader().getResourceAsStream("email_template.properties"));
String host=props.getProperty("host");
String uname=props.getProperty("uname");
String pwd=props.getProperty("pwd");
String from=props.getProperty("from");
String to=form.getEmail();
String subject=props.getProperty("subject");
String content=props.getProperty("content");
//替换第一个占位符为code
content=MessageFormat.format(content,form.getCode());//替换{0}
3、发送邮件
javax.mail.Session session=MailUtils.createSession(host,uname, pwd);
Mail mail=new Mail(from, to, subject, content);
try
{
MailUtils.send(session, mail);//发邮件
}
catch (MessagingException e)
{
e.printStackTrace();
}