spring发送email工具类



[html]  view plain  copy
  1. import java.io.File;  
  2. import java.util.List;  
  3. import java.util.Map;  
  4.   
  5. import javax.mail.MessagingException;  
  6. import javax.mail.internet.MimeMessage;  
  7.   
  8. import org.springframework.beans.factory.annotation.Autowired;  
  9. import org.springframework.core.io.ClassPathResource;  
  10. import org.springframework.core.io.FileSystemResource;  
  11. import org.springframework.core.io.Resource;  
  12. import org.springframework.mail.SimpleMailMessage;  
  13. import org.springframework.mail.javamail.JavaMailSender;  
  14. import org.springframework.mail.javamail.MimeMessageHelper;  
  15. import org.springframework.stereotype.Service;  
  16. import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;  
  17. import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;  
  18.   
  19. import freemarker.template.Template;  
  20.   
  21. @Service  
  22. public class MailService {  
  23.   
  24.     @Autowired  
  25.     private JavaMailSender sender;  
  26.   
  27.     @Autowired  
  28.     private FreeMarkerConfigurer freeMarkerConfigurer;  
  29.   
  30.    /**  
  31.     * 发送纯文本形式的email  
  32.     * @param toEmail 收件人邮箱  
  33.     * @param title 邮件标题  
  34.     * @param content 邮件内容  
  35.     */  
  36.     public void sendTextMail(String toEmail,String title,String content) {  
  37.         SimpleMailMessage msg = new SimpleMailMessage();  
  38.         msg.setFrom("px_miniportal@126.com");  
  39.         msg.setTo(toEmail);  
  40.         msg.setSubject(title);  
  41.         msg.setText(content);  
  42.         sender.send(msg);  
  43.     }  
  44.   
  45.     /**  
  46.      * 发送带有html的内容  
  47.      *   
  48.      * @param userId  
  49.      * @throws MessagingException  
  50.      */  
  51.     public void sendHtmlMail(String toEmail,String title,String htmlContent) throws MessagingException {  
  52.         MimeMessage msg = sender.createMimeMessage();  
  53.         MimeMessageHelper helper = new MimeMessageHelper(msg, false, "utf-8");  
  54.         helper.setFrom("px_miniportal@126.com");  
  55.         helper.setTo(toEmail);  
  56.         helper.setSubject(title);  
  57.         helper.setText(htmlContent, true);  
  58.         sender.send(msg);  
  59.     }  
  60.   
  61.     /**  
  62.      * 添加附件的email发送  
  63.      * @param toEmail 收件人地址  
  64.      * @param title 邮件标题  
  65.      * @param content 文本内容   
  66.      * @param aboutFiles 附件信息 每个子项都是一个文件相关信息的map  
  67.      *              Map<String,String>:  
  68.      *                  1.filePath   2.fileName  
  69.      * @throws Exception 异常  
  70.      */  
  71.     public void sendAttachmentMail(String toEmail,String title,String content,List<Map<String,String>> aboutFiles) throws Exception {  
  72.         MimeMessage msg = sender.createMimeMessage();  
  73.         MimeMessageHelper helper = new MimeMessageHelper(msg, true, "utf-8");  
  74.         helper.setFrom("px_miniportal@126.com");  
  75.         helper.setTo(toEmail);  
  76.         helper.setSubject(title);  
  77.         helper.setText(content);  
  78.         Resource resource = null;  
  79.         for(Map<String,String> file : aboutFiles){  
  80.             resource = new FileSystemResource(file.get("filePath"));  
  81.             if(resource.exists()){//是否存在资源   
  82.                 File attachmentFile = resource.getFile();  
  83.                 helper.addAttachment(file.get("fileName"), attachmentFile);  
  84.             }  
  85.         }  
  86.         sender.send(msg);  
  87.     }  
  88.       
  89.      
  90.   
  91.     /**  
  92.      * 使用freemarker模板进行发送  
  93.      * @param toEmail 收件人邮箱  
  94.      * @param title 标题  
  95.      * @param templateName 模板名称 模板都放在类路径下的mailtemp下  
  96.      * @param templateNameParams 模板中的参数,其中${key} 要使用map去进行替换  
  97.      * @throws MessagingException 异常  
  98.      */  
  99.     public void sendTemplateMail(String toEmail,String title,String templateName,Map<String,Object> templateNameParams) throws MessagingException {  
  100.         MimeMessage msg = sender.createMimeMessage();  
  101.         MimeMessageHelper helper = new MimeMessageHelper(msg, false, "utf-8");  
  102.         helper.setFrom("px_miniportal@126.com");  
  103.         helper.setTo(toEmail);  
  104.         helper.setSubject(title);  
  105.         String htmlText = getMailText(templateName,templateNameParams);  
  106.         helper.setText(htmlText, true);  
  107.         sender.send(msg);  
  108.     }  
  109.       
  110.       
  111.     /**  
  112.      * 使用freemarker模板+附件进行发送  
  113.      * @param toEmail 收件人邮箱  
  114.      * @param title 标题  
  115.      * @param templateName 模板名称 模板都放在类路径下的mailtemp下  
  116.      * @param templateNameParams 模板中的参数,其中${key} 要使用map去进行替换  
  117.      * @param aboutFiles 附件信息 每个子项都是一个文件相关信息的map  
  118.      *              Map<String,String>:  
  119.      *                  1.filePath   2.fileName  
  120.      * @throws Exception 异常  
  121.      */  
  122.     public void sendTemplateMail(String toEmail,String title,String templateName,Map<String,Object> templateNameParams,List<Map<String,String>> aboutFiles) throws Exception {  
  123.         MimeMessage msg = sender.createMimeMessage();  
  124.         MimeMessageHelper helper = new MimeMessageHelper(msg, true, "utf-8");  
  125.         helper.setFrom("px_miniportal@126.com");  
  126.         helper.setTo(toEmail);  
  127.         helper.setSubject(title);  
  128.         String htmlText = getMailText(templateName,templateNameParams);  
  129.         Resource resource = null;  
  130.         for(Map<String,String> file : aboutFiles){  
  131.             resource = new FileSystemResource(file.get("filePath"));  
  132.             if(resource.exists()){//是否存在资源   
  133.                 File attachmentFile = resource.getFile();  
  134.                 helper.addAttachment(file.get("fileName"), attachmentFile);  
  135.             }  
  136.         }  
  137.         helper.setText(htmlText, true);  
  138.         sender.send(msg);  
  139.     }  
  140.       
  141.     /**  
  142.      * 获取模板信息并且替换  
  143.      *   
  144.      * @param userId  
  145.      * @return  
  146.      */  
  147.     private String getMailText(String templateName,Map<String,Object> templateNameParams) {  
  148.         String htmlText = null;  
  149.         try {  
  150.             Template tpl = freeMarkerConfigurer.getConfiguration().getTemplate(templateName);  
  151.             htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(tpl,templateNameParams);  
  152.         } catch (Exception e) {  
  153.             throw new RuntimeException(e);  
  154.         }  
  155.         return htmlText;  
  156.     }  
  157.       
  158.       
  159.       
  160.       
  161.       
  162.       
  163.       
  164.       
  165.       
  166.       
  167.     /**  
  168.      * 发送带有图片内容的邮件 不建议使用这个  使用模板的那个就ok了、  
  169.      *   
  170.      * @throws MessagingException  
  171.      */  
  172.     @Deprecated  
  173.     public void sendInMIMEMail() throws MessagingException {  
  174.         MimeMessage msg = sender.createMimeMessage();  
  175.         MimeMessageHelper helper = new MimeMessageHelper(msg, true, "utf-8");  
  176.         helper.setFrom("px_miniportal@126.com");  
  177.         helper.setTo("masterspring3@gmail.com");  
  178.         helper.setSubject("注册成功");  
  179.         String htmlText = "<html><head>"  
  180.                 + "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">"  
  181.                 + "</head><body>" + "欢迎访问xxx论坛!</hr>"  
  182.                 + "<div><img src=\"cid:img01 \"></img></div>" // 特殊标示 cid:...  
  183.                 + "</body></html>";  
  184.         helper.setText(htmlText, true);  
  185.         ClassPathResource img = new ClassPathResource("bbt.gif");  
  186.         helper.addInline("img01 ", img);  
  187.         sender.send(msg);  
  188.     }  
  189. }  

[html]  view plain  copy
  1.    <!-- 配置spring 的 email服务 -->  
  2. <bean id="sender" class="org.springframework.mail.javamail.JavaMailSenderImpl"  
  3.     p:host="smtp.126.com" p:username="<span style="color:#ff0000;">需要配置自己的邮箱</span>p:password="<span style="font-family:Arial, Helvetica, sans-serif;"><span style="color:#ff0000;">需要配置自己的邮箱密码</span></span><span style="font-family:Arial, Helvetica, sans-serif;">"></span>  
  4.     <property name="javaMailProperties">  
  5.         <props>  
  6.             <prop key="mail.smtp.auth">true</prop>  
  7.         </props>  
  8.     </property>  
  9. </bean>  
  10.   
  11. <!-- 配置使用freemarker模板技术  用于生成html -->  
  12. <bean id="freeMarkerConfigurer" class=" org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"  
  13. p:templateLoaderPath="classpath:mailtemp">  
  14.     <property name="freemarkerSettings">  
  15.         <props>  
  16.             <prop key="template_update_delay">1800</prop>  
  17.             <prop key="default_encoding">UTF-8</prop>  
  18.             <!-- <prop key="locale">zh_CN</prop> -->  
  19.         </props>  
  20.     </property>  
  21. </bean>  

我在类路径下放了一个mailtemp的包,存放所有的ftl模板。



看我的测试类:

[html]  view plain  copy
  1. import java.util.ArrayList;  
  2. import java.util.HashMap;  
  3. import java.util.List;  
  4. import java.util.Map;  
  5.   
  6. import javax.mail.MessagingException;  
  7.   
  8. import org.junit.Test;  
  9. import org.junit.runner.RunWith;  
  10. import org.springframework.beans.factory.annotation.Autowired;  
  11. import org.springframework.core.io.FileSystemResource;  
  12. import org.springframework.core.io.Resource;  
  13. import org.springframework.test.context.ContextConfiguration;  
  14. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
  15.   
  16. import com.richsoft.miniportal.core.utils.MailService;  
  17.   
  18. @RunWith(SpringJUnit4ClassRunner.class)  
  19. @ContextConfiguration("classpath:applicationContext-*.xml")  
  20. public class MailServiceTest {  
  21.     @Autowired  
  22.     MailService ms;  
  23.     @Test  
  24.     public void sendTextMailTest(){  
  25.     }  
  26.       
  27.     @Test  
  28.     public void sendAttachmentMailTest(){  
  29.         List<Map<String,String>> aboutFiles = new ArrayList<Map<String,String>>();  
  30.         Map<String,String> itemnew HashMap<String,String>();  
  31.         item.put("filePath", "D:\\jcrop.html");  
  32.         item.put("fileName", "附件啦啦啦.html");  
  33.         aboutFiles.add(item);  
  34.         try {  
  35.             ms.sendAttachmentMail("dreamfly@126.com", "我是普迅啦啦啦啦", "非常不错", aboutFiles);  
  36.         } catch (Exception e) {  
  37.             e.printStackTrace();  
  38.         }  
  39.     }  
  40.       
  41.     @Test  
  42.     public void sendTemplateMailTest(){  
  43.         Map<String,Object> params = new HashMap<String, Object>();  
  44.         params.put("mark", "1234543543");  
  45.         try {  
  46.             ms.sendTemplateMail("349466315@qq.com", "我只是一个模板信息", "registerSucc.ftl", params);  
  47.         } catch (MessagingException e) {  
  48.             e.printStackTrace();  
  49.         }  
  50.     }  
  51.       
  52.     @Test  
  53.     public void sendTemplateMailAttachmentTest(){  
  54.         Map<String,Object> params = new HashMap<String, Object>();  
  55.         params.put("mark", "1234543543");  
  56.           
  57.         //附件参数  
  58.         List<Map<String,String>> aboutFiles = new ArrayList<Map<String,String>>();  
  59.         Map<String,String> itemnew HashMap<String,String>();  
  60.         item.put("filePath", "D:\\jcrop.html");  
  61.         item.put("fileName", "附件啦啦啦.html");  
  62.         aboutFiles.add(item);  
  63.           
  64.         try {  
  65.             ms.sendTemplateMail("349466315@qq.com", "我只是一个模板信息", "registerSucc.ftl", params,aboutFiles);  
  66.         } catch (Exception e) {  
  67.             e.printStackTrace();  
  68.         }  
  69.     }  
  70. }  
[html]  view plain  copy
  1. import java.io.File;  
  2. import java.util.List;  
  3. import java.util.Map;  
  4.   
  5. import javax.mail.MessagingException;  
  6. import javax.mail.internet.MimeMessage;  
  7.   
  8. import org.springframework.beans.factory.annotation.Autowired;  
  9. import org.springframework.core.io.ClassPathResource;  
  10. import org.springframework.core.io.FileSystemResource;  
  11. import org.springframework.core.io.Resource;  
  12. import org.springframework.mail.SimpleMailMessage;  
  13. import org.springframework.mail.javamail.JavaMailSender;  
  14. import org.springframework.mail.javamail.MimeMessageHelper;  
  15. import org.springframework.stereotype.Service;  
  16. import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;  
  17. import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;  
  18.   
  19. import freemarker.template.Template;  
  20.   
  21. @Service  
  22. public class MailService {  
  23.   
  24.     @Autowired  
  25.     private JavaMailSender sender;  
  26.   
  27.     @Autowired  
  28.     private FreeMarkerConfigurer freeMarkerConfigurer;  
  29.   
  30.    /**  
  31.     * 发送纯文本形式的email  
  32.     * @param toEmail 收件人邮箱  
  33.     * @param title 邮件标题  
  34.     * @param content 邮件内容  
  35.     */  
  36.     public void sendTextMail(String toEmail,String title,String content) {  
  37.         SimpleMailMessage msg = new SimpleMailMessage();  
  38.         msg.setFrom("px_miniportal@126.com");  
  39.         msg.setTo(toEmail);  
  40.         msg.setSubject(title);  
  41.         msg.setText(content);  
  42.         sender.send(msg);  
  43.     }  
  44.   
  45.     /**  
  46.      * 发送带有html的内容  
  47.      *   
  48.      * @param userId  
  49.      * @throws MessagingException  
  50.      */  
  51.     public void sendHtmlMail(String toEmail,String title,String htmlContent) throws MessagingException {  
  52.         MimeMessage msg = sender.createMimeMessage();  
  53.         MimeMessageHelper helper = new MimeMessageHelper(msg, false, "utf-8");  
  54.         helper.setFrom("px_miniportal@126.com");  
  55.         helper.setTo(toEmail);  
  56.         helper.setSubject(title);  
  57.         helper.setText(htmlContent, true);  
  58.         sender.send(msg);  
  59.     }  
  60.   
  61.     /**  
  62.      * 添加附件的email发送  
  63.      * @param toEmail 收件人地址  
  64.      * @param title 邮件标题  
  65.      * @param content 文本内容   
  66.      * @param aboutFiles 附件信息 每个子项都是一个文件相关信息的map  
  67.      *              Map<String,String>:  
  68.      *                  1.filePath   2.fileName  
  69.      * @throws Exception 异常  
  70.      */  
  71.     public void sendAttachmentMail(String toEmail,String title,String content,List<Map<String,String>> aboutFiles) throws Exception {  
  72.         MimeMessage msg = sender.createMimeMessage();  
  73.         MimeMessageHelper helper = new MimeMessageHelper(msg, true, "utf-8");  
  74.         helper.setFrom("px_miniportal@126.com");  
  75.         helper.setTo(toEmail);  
  76.         helper.setSubject(title);  
  77.         helper.setText(content);  
  78.         Resource resource = null;  
  79.         for(Map<String,String> file : aboutFiles){  
  80.             resource = new FileSystemResource(file.get("filePath"));  
  81.             if(resource.exists()){//是否存在资源   
  82.                 File attachmentFile = resource.getFile();  
  83.                 helper.addAttachment(file.get("fileName"), attachmentFile);  
  84.             }  
  85.         }  
  86.         sender.send(msg);  
  87.     }  
  88.       
  89.      
  90.   
  91.     /**  
  92.      * 使用freemarker模板进行发送  
  93.      * @param toEmail 收件人邮箱  
  94.      * @param title 标题  
  95.      * @param templateName 模板名称 模板都放在类路径下的mailtemp下  
  96.      * @param templateNameParams 模板中的参数,其中${key} 要使用map去进行替换  
  97.      * @throws MessagingException 异常  
  98.      */  
  99.     public void sendTemplateMail(String toEmail,String title,String templateName,Map<String,Object> templateNameParams) throws MessagingException {  
  100.         MimeMessage msg = sender.createMimeMessage();  
  101.         MimeMessageHelper helper = new MimeMessageHelper(msg, false, "utf-8");  
  102.         helper.setFrom("px_miniportal@126.com");  
  103.         helper.setTo(toEmail);  
  104.         helper.setSubject(title);  
  105.         String htmlText = getMailText(templateName,templateNameParams);  
  106.         helper.setText(htmlText, true);  
  107.         sender.send(msg);  
  108.     }  
  109.       
  110.       
  111.     /**  
  112.      * 使用freemarker模板+附件进行发送  
  113.      * @param toEmail 收件人邮箱  
  114.      * @param title 标题  
  115.      * @param templateName 模板名称 模板都放在类路径下的mailtemp下  
  116.      * @param templateNameParams 模板中的参数,其中${key} 要使用map去进行替换  
  117.      * @param aboutFiles 附件信息 每个子项都是一个文件相关信息的map  
  118.      *              Map<String,String>:  
  119.      *                  1.filePath   2.fileName  
  120.      * @throws Exception 异常  
  121.      */  
  122.     public void sendTemplateMail(String toEmail,String title,String templateName,Map<String,Object> templateNameParams,List<Map<String,String>> aboutFiles) throws Exception {  
  123.         MimeMessage msg = sender.createMimeMessage();  
  124.         MimeMessageHelper helper = new MimeMessageHelper(msg, true, "utf-8");  
  125.         helper.setFrom("px_miniportal@126.com");  
  126.         helper.setTo(toEmail);  
  127.         helper.setSubject(title);  
  128.         String htmlText = getMailText(templateName,templateNameParams);  
  129.         Resource resource = null;  
  130.         for(Map<String,String> file : aboutFiles){  
  131.             resource = new FileSystemResource(file.get("filePath"));  
  132.             if(resource.exists()){//是否存在资源   
  133.                 File attachmentFile = resource.getFile();  
  134.                 helper.addAttachment(file.get("fileName"), attachmentFile);  
  135.             }  
  136.         }  
  137.         helper.setText(htmlText, true);  
  138.         sender.send(msg);  
  139.     }  
  140.       
  141.     /**  
  142.      * 获取模板信息并且替换  
  143.      *   
  144.      * @param userId  
  145.      * @return  
  146.      */  
  147.     private String getMailText(String templateName,Map<String,Object> templateNameParams) {  
  148.         String htmlText = null;  
  149.         try {  
  150.             Template tpl = freeMarkerConfigurer.getConfiguration().getTemplate(templateName);  
  151.             htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(tpl,templateNameParams);  
  152.         } catch (Exception e) {  
  153.             throw new RuntimeException(e);  
  154.         }  
  155.         return htmlText;  
  156.     }  
  157.       
  158.       
  159.       
  160.       
  161.       
  162.       
  163.       
  164.       
  165.       
  166.       
  167.     /**  
  168.      * 发送带有图片内容的邮件 不建议使用这个  使用模板的那个就ok了、  
  169.      *   
  170.      * @throws MessagingException  
  171.      */  
  172.     @Deprecated  
  173.     public void sendInMIMEMail() throws MessagingException {  
  174.         MimeMessage msg = sender.createMimeMessage();  
  175.         MimeMessageHelper helper = new MimeMessageHelper(msg, true, "utf-8");  
  176.         helper.setFrom("px_miniportal@126.com");  
  177.         helper.setTo("masterspring3@gmail.com");  
  178.         helper.setSubject("注册成功");  
  179.         String htmlText = "<html><head>"  
  180.                 + "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">"  
  181.                 + "</head><body>" + "欢迎访问xxx论坛!</hr>"  
  182.                 + "<div><img src=\"cid:img01 \"></img></div>" // 特殊标示 cid:...  
  183.                 + "</body></html>";  
  184.         helper.setText(htmlText, true);  
  185.         ClassPathResource img = new ClassPathResource("bbt.gif");  
  186.         helper.addInline("img01 ", img);  
  187.         sender.send(msg);  
  188.     }  
  189. }  

[html]  view plain  copy
  1.    <!-- 配置spring 的 email服务 -->  
  2. <bean id="sender" class="org.springframework.mail.javamail.JavaMailSenderImpl"  
  3.     p:host="smtp.126.com" p:username="<span style="color:#ff0000;">需要配置自己的邮箱</span>p:password="<span style="font-family:Arial, Helvetica, sans-serif;"><span style="color:#ff0000;">需要配置自己的邮箱密码</span></span><span style="font-family:Arial, Helvetica, sans-serif;">"></span>  
  4.     <property name="javaMailProperties">  
  5.         <props>  
  6.             <prop key="mail.smtp.auth">true</prop>  
  7.         </props>  
  8.     </property>  
  9. </bean>  
  10.   
  11. <!-- 配置使用freemarker模板技术  用于生成html -->  
  12. <bean id="freeMarkerConfigurer" class=" org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"  
  13. p:templateLoaderPath="classpath:mailtemp">  
  14.     <property name="freemarkerSettings">  
  15.         <props>  
  16.             <prop key="template_update_delay">1800</prop>  
  17.             <prop key="default_encoding">UTF-8</prop>  
  18.             <!-- <prop key="locale">zh_CN</prop> -->  
  19.         </props>  
  20.     </property>  
  21. </bean>  

我在类路径下放了一个mailtemp的包,存放所有的ftl模板。



看我的测试类:

[html]  view plain  copy
  1. import java.util.ArrayList;  
  2. import java.util.HashMap;  
  3. import java.util.List;  
  4. import java.util.Map;  
  5.   
  6. import javax.mail.MessagingException;  
  7.   
  8. import org.junit.Test;  
  9. import org.junit.runner.RunWith;  
  10. import org.springframework.beans.factory.annotation.Autowired;  
  11. import org.springframework.core.io.FileSystemResource;  
  12. import org.springframework.core.io.Resource;  
  13. import org.springframework.test.context.ContextConfiguration;  
  14. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
  15.   
  16. import com.richsoft.miniportal.core.utils.MailService;  
  17.   
  18. @RunWith(SpringJUnit4ClassRunner.class)  
  19. @ContextConfiguration("classpath:applicationContext-*.xml")  
  20. public class MailServiceTest {  
  21.     @Autowired  
  22.     MailService ms;  
  23.     @Test  
  24.     public void sendTextMailTest(){  
  25.     }  
  26.       
  27.     @Test  
  28.     public void sendAttachmentMailTest(){  
  29.         List<Map<String,String>> aboutFiles = new ArrayList<Map<String,String>>();  
  30.         Map<String,String> itemnew HashMap<String,String>();  
  31.         item.put("filePath", "D:\\jcrop.html");  
  32.         item.put("fileName", "附件啦啦啦.html");  
  33.         aboutFiles.add(item);  
  34.         try {  
  35.             ms.sendAttachmentMail("dreamfly@126.com", "我是普迅啦啦啦啦", "非常不错", aboutFiles);  
  36.         } catch (Exception e) {  
  37.             e.printStackTrace();  
  38.         }  
  39.     }  
  40.       
  41.     @Test  
  42.     public void sendTemplateMailTest(){  
  43.         Map<String,Object> params = new HashMap<String, Object>();  
  44.         params.put("mark", "1234543543");  
  45.         try {  
  46.             ms.sendTemplateMail("349466315@qq.com", "我只是一个模板信息", "registerSucc.ftl", params);  
  47.         } catch (MessagingException e) {  
  48.             e.printStackTrace();  
  49.         }  
  50.     }  
  51.       
  52.     @Test  
  53.     public void sendTemplateMailAttachmentTest(){  
  54.         Map<String,Object> params = new HashMap<String, Object>();  
  55.         params.put("mark", "1234543543");  
  56.           
  57.         //附件参数  
  58.         List<Map<String,String>> aboutFiles = new ArrayList<Map<String,String>>();  
  59.         Map<String,String> itemnew HashMap<String,String>();  
  60.         item.put("filePath", "D:\\jcrop.html");  
  61.         item.put("fileName", "附件啦啦啦.html");  
  62.         aboutFiles.add(item);  
  63.           
  64.         try {  
  65.             ms.sendTemplateMail("349466315@qq.com", "我只是一个模板信息", "registerSucc.ftl", params,aboutFiles);  
  66.         } catch (Exception e) {  
  67.             e.printStackTrace();  
  68.         }  
  69.     }  
  70. }  
/* * JCatalog Project */ package com.hexiang.utils; import java.util.List; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.Properties; import javax.mail.Session; import javax.mail.Transport; import javax.mail.Message; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.hexiang.exception.CatalogException; /** * Utility class to send email. * * @author <a href="380595305@qq.com">hexiang</a> */ public class EmailUtil { //the logger for this class private static Log logger = LogFactory.getLog("com.hexiang.util.EmailUtil"); /** * Send email to a single recipient. * * @param smtpHost the SMTP email server address * @param senderAddress the sender email address * @param senderName the sender name * @param receiverAddress the recipient email address * @param sub the subject of the email * @param msg the message content of the email */ public static void sendEmail(String smtpHost, String senderAddress, String senderName, String receiverAddress, String sub, String msg) throws CatalogException { List<String> recipients = new ArrayList<String>(); recipients.add(receiverAddress); sendEmail(smtpHost, senderAddress, senderName, recipients, sub, msg); } /** * Send email to a list of recipients. * * @param smtpHost the SMTP email server address * @param senderAddress the sender email address * @param senderName the sender name * @param recipients a list of receipients email addresses * @param sub the subject of the email * @param msg the message content of the email */ public static void sendEmail(String smtpHost, String senderAddress, String senderName, List<String> recipients, String sub, String msg) throws CatalogException { if (smtpHost == null) { String errMsg = "Could not send email: smtp host address is null"; logger.error(errMsg); throw new CatalogException(errMsg); } try { Properties props = System.getProperties(); props.put("mail.smtp.host", smtpHost); Session session = Session.getDefaultInstance(props, null ); MimeMessage message = new MimeMessage( session ); message.addHeader("Content-type", "text/plain"); message.setSubject(sub); message.setFrom(new InternetAddress(senderAddress, senderName)); for (Iterator<String> it = recipients.iterator(); it.hasNext();) { String email = (String)it.next(); message.addRecipients(Message.RecipientType.TO, email); } message.setText(msg); message.setSentDate( new Date() ); Transport.send(message); } catch (Exception e) { String errorMsg = "Could not send email"; logger.error(errorMsg, e); throw new CatalogException("errorMsg", e); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值