Spring发送html邮件

本文介绍如何在SpringMVC框架下实现邮件发送功能,包括配置邮件服务器、编写邮件发送工具类及发送HTML格式邮件的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   本文基于Spring MVC 注解,让Spring跑起来

        容器:tomcat6

        (1) 导入jar包mail.jar、activation.jar和org.springframework.comtext.support.jar,其中mail.jar来自于javaMail,activation.jar来自于jaf,最好都使用最新版。

        (2) 编写MailUtil类作为邮件发送的工具类:

[java]  view plain copy
  1. /** 
  2.  * 
  3.  * @author geloin 
  4.  * @date 2012-5-8 上午11:02:41 
  5.  */  
  6. package com.embest.ruisystem.util;  
  7.   
  8. import java.util.Properties;  
  9.   
  10. import javax.mail.MessagingException;  
  11. import javax.mail.Session;  
  12. import javax.mail.internet.MimeMessage;  
  13.   
  14. import org.springframework.mail.javamail.JavaMailSenderImpl;  
  15. import org.springframework.mail.javamail.MimeMessageHelper;  
  16.   
  17. /** 
  18.  *  
  19.  * @author geloin 
  20.  * @date 2012-5-8 上午11:02:41 
  21.  */  
  22. public class MailUtil {  
  23.   
  24.     /** 
  25.      * 发送html邮件 
  26.      *  
  27.      * @author geloin 
  28.      * @date 2012-5-8 上午11:38:44 
  29.      * @param toEmail 
  30.      * @param subject 
  31.      * @param htmlContent 
  32.      */  
  33.     public static void sendMail(String toEmail, String subject,  
  34.             String htmlContent) {  
  35.   
  36.         JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();  
  37.   
  38.         // 发送邮箱的邮件服务器  
  39.         senderImpl.setHost(Constants.emailHost);  
  40.   
  41.         // 建立邮件消息,发送简单邮件和html邮件的区别  
  42.         MimeMessage mailMessage = senderImpl.createMimeMessage();  
  43.         // 为防止乱码,添加编码集设置  
  44.         MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,  
  45.                 "UTF-8");  
  46.   
  47.         try {  
  48.             // 接收方邮箱  
  49.             messageHelper.setTo(toEmail);  
  50.         } catch (MessagingException e) {  
  51.             throw new RuntimeException("收件人邮箱地址出错!");  
  52.         }  
  53.         try {  
  54.             // 发送方邮箱  
  55.             messageHelper.setFrom(Constants.emailFrom);  
  56.         } catch (MessagingException e) {  
  57.             throw new RuntimeException("发件人邮箱地址出错!");  
  58.         }  
  59.         try {  
  60.             messageHelper.setSubject(subject);  
  61.         } catch (MessagingException e) {  
  62.             throw new RuntimeException("邮件主题出错!");  
  63.         }  
  64.         try {  
  65.             // true 表示启动HTML格式的邮件  
  66.             messageHelper.setText(htmlContent, true);  
  67.         } catch (MessagingException e) {  
  68.             throw new RuntimeException("邮件内容出错!");  
  69.         }  
  70.   
  71.         Properties prop = new Properties();  
  72.         // 将这个参数设为true,让服务器进行认证,认证用户名和密码是否正确  
  73.         prop.put("mail.smtp.auth""true");  
  74.         // 超时时间  
  75.         prop.put("mail.smtp.timeout""25000");  
  76.   
  77.         // 添加验证  
  78.         MyAuthenticator auth = new MyAuthenticator(Constants.emailUsername,  
  79.                 Constants.emailPassword);  
  80.   
  81.         Session session = Session.getDefaultInstance(prop, auth);  
  82.         senderImpl.setSession(session);  
  83.   
  84.         // senderImpl.setJavaMailProperties(prop);  
  85.         // 发送邮件  
  86.         senderImpl.send(mailMessage);  
  87.   
  88.     }  
  89.   
  90. }  

        以上代码中,发送邮箱的邮件服务器、发送方邮箱、发送方用户名、发送方邮箱密码均来自Constants类,该类从资源文件中获取相关数据并设为类中常量的值,资源环境中的名值对如下所示:

[java]  view plain copy
  1. email.from=abc@163.com  
  2. email.host=smtp.163.com  
  3. email.username=abc  
  4. email.password=abcdefg  
        其中,email.host为发送方邮箱的发送服务器,163邮箱为smtp.163.com,若使用别的邮箱而未知服务器,则可使用foxmail等工具测试得出结果,或者问渡娘。

        上述代码中很重要的片段如下:

[java]  view plain copy
  1. // 添加验证  
  2. MyAuthenticator auth = new MyAuthenticator(Constants.emailUsername, Constants.emailPassword);  
  3. Session session = Session.getDefaultInstance(prop, auth);  
  4. senderImpl.setSession(session);  
        上述代码的作用为添加验证,在使用邮箱服务器发送邮件时,需要验证发送方的用户名和密码,验证成功后,邮箱服务器才允许发送邮件。若不加上此代码,则会出现AuthenticationFailedException异常。
        MyAuthenticator为自定义的验证类,该类代码如下所示:
[java]  view plain copy
  1. /** 
  2.  * 
  3.  * @author geloin 
  4.  * @date 2012-5-8 下午2:48:25 
  5.  */  
  6. package com.embest.ruisystem.util;  
  7.   
  8. import javax.mail.Authenticator;  
  9. import javax.mail.PasswordAuthentication;  
  10.   
  11. /** 
  12.  *  
  13.  * @author geloin 
  14.  * @date 2012-5-8 下午2:48:25 
  15.  */  
  16. public class MyAuthenticator extends Authenticator {  
  17.     private String username;  
  18.     private String password;  
  19.   
  20.     /** 
  21.      *  
  22.      * @author geloin 
  23.      * @date 2012-5-8 下午2:48:53 
  24.      * @param username 
  25.      * @param password 
  26.      */  
  27.     public MyAuthenticator(String username, String password) {  
  28.         super();  
  29.         this.username = username;  
  30.         this.password = password;  
  31.     }  
  32.   
  33.     protected PasswordAuthentication getPasswordAuthentication() {  
  34.         return new PasswordAuthentication(username, password);  
  35.     }  
  36. }  

        (3) 发送邮件
[java]  view plain copy
  1. /** 
  2.      *  
  3.      * @author geloin 
  4.      * @date 2012-3-31 下午4:28:30 
  5.      * @param args 
  6.      */  
  7.     public static void main(String[] args) {  
  8.         String url = Constants.basePath + "/background/index.html";  
  9.   
  10.         // 发送邮件  
  11.         // 主题  
  12.         String subject = "RUI密码信息";  
  13.         // 正文  
  14.         StringBuilder builder = new StringBuilder();  
  15.         builder.append("<html><head>");  
  16.         builder.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");  
  17.         builder.append("</head><body>");  
  18.         builder.append("您好,张三:<br />");  
  19.         builder.append("\t系统已为您重置了RUI密码,账户信息如下:<br />");  
  20.         builder.append("用户账户:zhangsan<br />用户密码:123456<br />您可以点击以下链接登录RUI:");  
  21.         builder.append("<a href=\"");  
  22.         builder.append(url);  
  23.         builder.append("\">");  
  24.         builder.append(url);  
  25.         builder.append("</a>");  
  26.         builder.append("</body></html>");  
  27.         String htmlContent = builder.toString();  
  28.   
  29.         MailUtil.sendMail("to@163.com", subject, htmlContent);  
  30.     }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值