从Properties文件中读取配置并发送邮件

来源:互联网 发布:不常用的英文单词知乎 编辑:程序博客网 时间:2024/06/10 03:07

本例子将读取properties文件并发送邮件;

1 创建properties实例

 Properties prop =new Properties();

2
获取这个文件

prop.load(this.getClass().getClassLoader().getResourceAsStream("email_template.properties"));

3将各文件存入变量中

String host=prop.getProperty("host");      String name=prop.getProperty("username");      String pass=prop.getProperty("password");String   content=MessageFormat.format(prop.getProperty("content"), user.getActivationCode());       String from=prop.getProperty("from");       String to=user.getEmail();String subject=prop.getProperty("subject");

4创建SEssion 和邮件

 Session session=MailUtils.createSession(host, name, pass);  Mail mail=new Mail(from,to,subject,content);

5完成发送邮件

.` try {        MailUtils.send(session, mail);    } catch (MessagingException e) {        throw new RuntimeException (e);    } catch (IOException e) {        throw new RuntimeException (e);    }  }`