国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Spring發(fā)送郵件簡單實例

1、spring配置文件(src/applicationContext.xml ):

Xml代碼
  1. <bean id="mailSender"  
  2.     class="org.springframework.mail.javamail.JavaMailSenderImpl">  
  3.     <property name="host" value="222.173.82.207"></property>  
  4.     <property name="javaMailProperties">  
  5.         <props>  
  6.             <prop key="mail.smtp.auth">true</prop>  
  7.             <prop key="mail.smtp.timeout">25000</prop>  
  8.         </props>  
  9.     </property>  
  10.     <property name="username" value="tiandeqing@ecode.net.cn" />  
  11.     <property name="password" value="密碼" />  
  12. </bean>  
  13. <bean id="freeMarker"  
  14.     class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">  
  15.     <property name="templateLoaderPath" value="classpath:mail" /><!--指定模板文件目錄-->  
  16.     <property name="freemarkerSettings"><!-- 設(shè)置FreeMarker環(huán)境屬性-->  
  17.         <props>  
  18.             <prop key="template_update_delay">1800</prop><!--刷新模板的周期,單位為秒-->  
  19.             <prop key="default_encoding">UTF-8</prop><!--模板的編碼格式 -->  
  20.             <prop key="locale">zh_CN</prop><!-- 本地化設(shè)置-->  
  21.         </props>  
  22.     </property>  
  23. </bean>  
 

2、Java代碼:

給指定的單個用戶發(fā)送普通文本郵件:SpringMail.java

 

Java代碼
  1. package aaa;   
  2.   
  3. import java.io.StringWriter;   
  4. import java.util.HashMap;   
  5. import java.util.Map;   
  6.   
  7. import org.springframework.context.ApplicationContext;   
  8. import org.springframework.context.support.FileSystemXmlApplicationContext;   
  9. import org.springframework.mail.SimpleMailMessage;   
  10. import org.springframework.mail.javamail.JavaMailSender;   
  11.   
  12. import freemarker.template.Configuration;   
  13. import freemarker.template.Template;   
  14.   
  15. public class SpringMail {   
  16.     private Configuration cfg = new Configuration();     
  17.        
  18.  public static void main(String[] args) throws Exception {     
  19.              ApplicationContext ctx = new FileSystemXmlApplicationContext(     
  20.                      "src/applicationContext.xml");     
  21.              JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");     
  22.              SpringMail springMail = new SpringMail();     
  23.              springMail.sendMail(sender);     
  24.           
  25.          }     
  26.           
  27.          private void sendMail(JavaMailSender sender) throws Exception {     
  28.              SimpleMailMessage mail = new SimpleMailMessage();     
  29.              mail.setTo("tiandeqing@ecode.net.cn"); //接收人     
  30.              mail.setFrom("tiandeqing@ecode.net.cn"); //發(fā)送人     
  31.              mail.setSubject("test by amigo");     
  32.              //嵌入ftl模版     
  33.              cfg.setClassForTemplateLoading(getClass(), "/mail");     
  34.              Map root = new HashMap();     
  35.              root.put("username""sucre"); //模板變量     
  36.              Template t = cfg.getTemplate("notify-mail.ftl");     
  37.              StringWriter writer = new StringWriter();     
  38.              t.process(root, writer);     
  39.              //把模版內(nèi)容寫入郵件中     
  40.              mail.setText(writer.toString());     
  41.              sender.send(mail);     
  42.              System.out.println("郵件發(fā)送成功!");     
  43.          }     
  44.            
  45. }  

 

給指定的多個用戶發(fā)送含有附件的html郵件,SpringMail_Batch_Attach_HTML.java

Java代碼
  1. package aaa;   
  2.   
  3. import java.io.File;   
  4. import java.io.StringWriter;   
  5. import java.io.UnsupportedEncodingException;   
  6. import java.util.ArrayList;   
  7. import java.util.HashMap;   
  8. import java.util.List;   
  9. import java.util.Map;   
  10.   
  11. import javax.mail.Message;   
  12. import javax.mail.MessagingException;   
  13. import javax.mail.internet.InternetAddress;   
  14. import javax.mail.internet.MimeMessage;   
  15. import javax.mail.internet.MimeUtility;   
  16.   
  17. import org.springframework.context.ApplicationContext;   
  18. import org.springframework.context.support.FileSystemXmlApplicationContext;   
  19. import org.springframework.mail.SimpleMailMessage;   
  20. import org.springframework.mail.javamail.JavaMailSender;   
  21. import org.springframework.mail.javamail.MimeMessageHelper;   
  22. import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;   
  23. import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;   
  24.   
  25. import freemarker.template.Configuration;   
  26. import freemarker.template.Template;   
  27.   
  28. public class SpringMail_Batch_Attach_HTML {   
  29.     private Configuration cfg = new Configuration();   
  30.   
  31.     private static JavaMailSender sender = null;   
  32.   
  33.     private static FreeMarkerConfigurer freeMarker = null;   
  34.   
  35.     public static void main(String[] args) throws Exception {   
  36.         // init   
  37.         ApplicationContext ctx = new FileSystemXmlApplicationContext(   
  38.                 "src/applicationContext.xml");   
  39.         JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");   
  40.         SpringMail_Batch_Attach_HTML.sender = sender;   
  41.         SpringMail_Batch_Attach_HTML.freeMarker = (FreeMarkerConfigurer) ctx   
  42.                 .getBean("freeMarker");   
  43.   
  44.         SpringMail_Batch_Attach_HTML springMail = new SpringMail_Batch_Attach_HTML();   
  45.         //發(fā)送簡單郵件   
  46.         springMail.sendMail(sender);   
  47.         //給某個人發(fā)送郵件,基于模板   
  48.         springMail.sendMessage("灑江河風(fēng)景阿嫂法拉""wytdq@126.com");   
  49.   
  50.         //給某些人發(fā)送郵件,帶附件   
  51.         List toList = new ArrayList();   
  52.         toList.add("xxx@sina.COM");   
  53.         toList.add("xxx@163.COM");   
  54.         toList.add("TIANDEQING@ECODE.NET.CN");   
  55.         springMail.sendBatchEmail("郵件批量發(fā)送測試", toList);   
  56.     }   
  57.   
  58.     // 發(fā)送一封文本郵件給一個收件人   
  59.     private void sendMail(JavaMailSender sender) throws Exception {   
  60.         SimpleMailMessage mail = new SimpleMailMessage();   
  61.         mail.setTo("tiandeqing@ecode.net.cn"); // 接收人   
  62.         mail.setFrom("tiandeqing@ecode.net.cn"); // 發(fā)送人   
  63.         mail.setSubject("test by amigo");   
  64.         // 嵌入ftl模版   
  65.         cfg.setClassForTemplateLoading(getClass(), "/mail");   
  66.         Map root = new HashMap();   
  67.         root.put("username""sucre"); // 模板變量   
  68.         Template t = cfg.getTemplate("notify-mail.ftl");   
  69.         StringWriter writer = new StringWriter();   
  70.         t.process(root, writer);   
  71.         // 把模版內(nèi)容寫入郵件中   
  72.         mail.setText(writer.toString());   
  73.         sender.send(mail);   
  74.         System.out.println("郵件發(fā)送成功!");   
  75.     }   
  76.   
  77.     /**  
  78.      * 發(fā)送帶模板的單個html格式郵件  
  79.      *   
  80.      * @throws Exception  
  81.      */  
  82.     public void sendMessage(String content, String address) throws Exception {   
  83.         MimeMessage msg = sender.createMimeMessage();   
  84.         // 設(shè)置utf-8或GBK編碼,否則郵件會有亂碼,true表示為multipart郵件   
  85.         MimeMessageHelper helper = new MimeMessageHelper(msg, true"utf-8");   
  86.         helper.setTo(address); // 郵件接收地址   
  87.         helper.setFrom("tiandeqing@ecode.net.cn"); // 郵件發(fā)送地址,這里必須和xml里的郵件地址相同一致   
  88.         helper.setSubject("測試ccc"); // 主題   
  89.         String htmlText = getMailText(content); // 使用模板生成html郵件內(nèi)容   
  90.         helper.setText(htmlText, true); // 郵件內(nèi)容,注意加參數(shù)true,表示啟用html格式   
  91.         sender.send(msg); // 發(fā)送郵件   
  92.         System.out.println("sendMessage(String content,String address) OK");   
  93.     }   
  94.   
  95.     /**  
  96.      * 批量發(fā)送多媒體郵件  
  97.      * */  
  98.     public void sendBatchEmail(String content, List address)   
  99.             throws MessagingException, UnsupportedEncodingException {   
  100.         MimeMessage msg = sender.createMimeMessage();   
  101.         MimeMessageHelper helper = new MimeMessageHelper(msg, true"utf-8");   
  102.            
  103.         StringBuffer html = new StringBuffer();   
  104.         html.append("<html>");   
  105.         html.append("<head>");   
  106.         html.append("<meta http-equiv='Content-Type' content='text/html; charset=gbk'>");   
  107.         html.append("</head>");   
  108.         html.append("<body bgcolor='#ccccff'>");   
  109.         html.append("<center>");   
  110.         html.append("<h1>你好,Jarry</h1>").append(content);   
  111.         html.append("<img src='cid:myimg'>");//顯示圖片   
  112.         html.append("<p>logo:");   
  113.         html.append("<img src='cid:vedio'>");   
  114.         html.append("</center>");   
  115.         html.append("</body>");   
  116.         html.append("</html>");   
  117.            
  118.         String toList = getMailList(address);   
  119.         InternetAddress[] iaToList = new InternetAddress().parse(toList);   
  120.         msg.setRecipients(Message.RecipientType.TO, iaToList);   
  121.         helper.setFrom("tiandeqing@ecode.net.cn");   
  122.         helper.setSubject("批量發(fā)送測試");   
  123.         helper.setText(html.toString(), true);   
  124.         // 添加內(nèi)嵌文件,第1個參數(shù)為cid標(biāo)識這個文件,第2個參數(shù)為資源   
  125.         helper.addInline("myimg"new File("D:\\My Documents\\My Pictures\\tian.JPG")); // 附件內(nèi)容   
  126.         helper.addInline("vedio"new File("D:\\My Documents\\My Pictures\\vedio.JPG"));   
  127.         File file = new File("D:\\My Documents\\My Pictures\\hibernate框架.jpg");   
  128.         // 這里的方法調(diào)用和插入圖片是不同的(插入到最后,并且直接顯示),使用MimeUtility.encodeWord()來解決附件名稱的中文問題   
  129.         helper.addAttachment(MimeUtility.encodeWord(file.getName()), file);   
  130.         // 如果主題出現(xiàn)亂碼,可以使用該函數(shù),也可以使用下面的函數(shù)   
  131.         // helper.setSubject(MimeUtility.encodeText(String text,String   
  132.         // charset,String encoding))   
  133.         // 第2個參數(shù)表示字符集,第三個為目標(biāo)編碼格式。   
  134.         // MimeUtility.encodeWord(String word,String charset,String encoding)   
  135.         sender.send(msg);   
  136.         System.out.println("sendBatchEmail OK");   
  137.     }   
  138.   
  139.     /**  
  140.      * 集合轉(zhuǎn)換字符串  
  141.      */  
  142.     public String getMailList(List<String> to) {   
  143.         StringBuffer toList = new StringBuffer();   
  144.         int length = to.size();   
  145.         if (to != null && length < 2) {   
  146.             toList.append(to.get(0));   
  147.         } else {   
  148.             for (int i = 0; i < length; i++) {   
  149.                 toList.append(to.get(i));   
  150.                 if (i != (length - 1)) {   
  151.                     toList.append(",");   
  152.                 }   
  153.             }   
  154.         }   
  155.         return toList.toString();   
  156.     }   
  157.   
  158.     // 通過模板構(gòu)造郵件內(nèi)容,參數(shù)content將替換模板文件中的${content}標(biāo)簽。   
  159.     private String getMailText(String content) throws Exception {   
  160.         String htmlText = "";   
  161.         // 通過指定模板名獲取FreeMarker模板實例   
  162.         Template tpl = freeMarker.getConfiguration().getTemplate(   
  163.                 "registerUser.ftl");   
  164.         Map map = new HashMap(); // FreeMarker通過Map傳遞動態(tài)數(shù)據(jù)   
  165.         map.put("content", content); // 注意動態(tài)數(shù)據(jù)的key和模板標(biāo)簽中指定的屬性相匹配   
  166.         // 解析模板并替換動態(tài)數(shù)據(jù),最終content將替換模板文件中的${content}標(biāo)簽。   
  167.         htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(tpl, map);   
  168.         return htmlText;   
  169.     }   
  170. }  
 

 

3、模板文件src/mail/notify-mail.ftl

 
Text代碼
  1. 歡迎加入!     
  2.      
  3. 親愛的${username}     
  4.      
  5. 請點擊鏈接完成注冊:     
  6.      
  7. 如果您的email程序不支持鏈接點擊,請將上面的地址拷貝至您的瀏覽器(如IE)的地址欄進入****。     
  8.      
  9. 您可以在***:     
  10.      
  11. 查看最新的影視資料,查看各種相關(guān)消費產(chǎn)品,在這里交友,灌水……;     
  12.      
  13. 希望您在**度過快樂的時光!     
  14.      
  15. -      
  16.      
  17. (這是一封自動產(chǎn)生的email,請勿回復(fù)。)    

  模板文件src/mail/registerUser.ftl

Txt代碼
  1. <html>       
  2.    <head>       
  3.       <meta http-equiv="content-type" content="text/html;charset=utf8">       
  4.    </head>       
  5.    <body>       
  6.        恭喜您成功注冊!您的用戶名為:<font color='red' size='30'>${content}</font>       
  7.    </body>       
  8. </html>     
 

 

4、例子使用了FreeMarker。

FreeMarker就是一種用Java編寫的模板引擎,它根據(jù)模板輸出多種規(guī)格的文本。

特別指出的是,F(xiàn)reeMarker與Web應(yīng)用框架無關(guān),它同樣可以應(yīng)用在非Web應(yīng)用程序環(huán)境中。

 程序目錄結(jié)構(gòu):

 

 

注意在執(zhí)行代碼前,請確認已經(jīng)將activation.jar,commons-logging-1.0.4.jar,mail.jar和spring.jar載入工程,并且服務(wù)器的25端口已開啟。

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
shiro安全框架擴展教程--如何動態(tài)控制頁面節(jié)點元素的權(quán)限
freemarker報 java.io.FileNotFoundException:及Te...
Spring框架集成FreeMarker
freeMarker模板引擎將表格中的數(shù)據(jù)導(dǎo)出成Excel
java如何生成word文檔_使用Java生成word文檔(附源碼)
如何能讓Java生成復(fù)雜Word文檔(1)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服