JavaMail应用实例说明:模板邮件

编程技术  /  houtizong 发布于 3年前   121

首先在你的class或src下面建一个properties文件,我新建了一个内容如下:

emailTemplate = <html> <BODY><h2>Hi\uff0c</h2><b> {0} </b><br/>\u4f60\u7684\u7528\u6237\u540d\u4e3a\uff1a{1}\uff0c\u5bc6\u7801\u662f\uff1a{2}\u6b22\u8fce\u8bbf\u95ee\uff1a<a href="http://zmx.iteye.com">\u6211\u7684IT\u6280\u672f\u6742\u8c08</a></BODY></HTML>

 原文是:Hi,{0}你的用户名为:{1},密码是:{2} 欢迎访问:我的IT技术杂谈

 

注册页面如下:

<form action="SendTemplate">  <table align="center">  <tr>  <td align="center">用户名:</td>  <td>  <input type="text" name="uname">  </td>  </tr>  <tr>  <td align="center">Email:</td>  <td>  <input type="text" name="uemail">  </td>  </tr>  <tr>  <td align="center" colspan="2">  <input type="submit" value="注册">  </td>  </tr>  </table>  </form>
 

 注册的servlet如下:

 

public class SendTemplate extends HttpServlet {/** *  */private static final long serialVersionUID = -7597646570845508429L;public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {this.doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("GBK");// 获得邮件模板信息ResourceBundle mailTemplateRb = ResourceBundle.getBundle("emailTemplate");// 由管理员发送String from = "[email protected]";// 注册的用户邮箱String to = request.getParameter("uemail");// 设置主题String subject = "欢迎来到http://zmx.iteye.com";// 注册用户的用户名String uname = request.getParameter("uname");// 注册用户的密码String upassword = "123456";// 服务器地址String mailServer = "PC200904071715";// 模板参数Object[] args = new Object[] { uname, uname, upassword };MessageFormat formater = new MessageFormat("");formater.applyPattern(mailTemplateRb.getString("emailTemplate"));String messageText = formater.format(args);// 设置邮件的传输协议信息Properties transProp = System.getProperties();// 邮件服务器地址transProp.put("mail.smtp.host", mailServer);// 邮件传输协议中的接收协议:smtptransProp.put("mail.transport.protocol", "smtp");// 是否通过验证transProp.put("mail.smtp.auth", "true");// 服务器端口transProp.put("mail.smtp.port", "25");// 默认端口25Session mailSession = Session.getDefaultInstance(transProp,new Authenticator(){@Overrideprotected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication("admin","");}}); Message mailMessage = new MimeMessage(mailSession);try {mailMessage.setFrom(new InternetAddress(from));mailMessage.setRecipient(RecipientType.TO, new InternetAddress(to));mailMessage.setSubject(subject);mailMessage.setSentDate(new Date());Multipart mp = new MimeMultipart();MimeBodyPart mbp = new MimeBodyPart();mbp.setContent(messageText, "text/html;charset=GB2312");mp.addBodyPart(mbp);mailMessage.setContent(mp);Transport.send(mailMessage);} catch (AddressException e) {e.printStackTrace();} catch (MessagingException e) {e.printStackTrace();}response.sendRedirect("ok.jsp");}}
 
下一篇:很搞笑啊

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客