Javaweb之发送邮件

来源:互联网 发布:数据概况英文翻译 编辑:程序博客网 时间:2024/06/06 03:37

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");//替换第一个占位符为codecontent=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();}
0 0
原创粉丝点击