基于spring包的sendmail

  1 package com.isoftstone.iics.bizsupport.sms.restful.mail;
  2 
  3 import java.io.File;
  4 
  5 import java.util.HashMap;
  6 import java.util.Map;
  7 
  8 import javax.mail.internet.MimeMessage;
  9 import javax.mail.internet.MimeUtility;
 10 
 11 import org.apache.velocity.app.VelocityEngine;
 12 
 13 import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
 14 import org.junit.Ignore;
 15 import org.junit.Test;
 16 import org.springframework.beans.factory.annotation.Autowired;
 17 import org.springframework.beans.factory.annotation.Qualifier;
 18 import org.springframework.beans.factory.annotation.Value;
 19 
 20 import org.springframework.mail.javamail.JavaMailSender;
 21 import org.springframework.mail.javamail.MimeMessageHelper;
 22 import org.springframework.ui.velocity.VelocityEngineFactory;
 23 import org.springframework.ui.velocity.VelocityEngineUtils;
 24 
 25 
 26 
 27 //<h3>Hi ${data.name}, SIPMall occur exception!</h3>
 28 //<div>
 29 //   Your email address is <a href="mailto:${data.address}">${mailModel.emailAddress}</a>.
 30 //   <p>UserName : ${data.name}</p>
 31 //   <p>UserIP : ${data.ip}</p>
 32 //   <p>User RequestURL : ${data.url}</p>
 33 //   <div>
 34 //       Caused by:${data.message}
 35 
 36 /**
 37  * 
 38  * <P>邮件测试类</P>
 39  * @ClassName: MailDemo
 40  * @author 冯浩  2016年10月8日 下午6:03:41
 41  * @see TODO
 42  */
 43 public class MailDemo {
 44     
 45 //    static{
 46 //        ClassPathXmlApplicationContext context=null;
 47 //        context=new ClassPathXmlApplicationContext(new String[]{
 48 //            "classpath*:spring_echannel.xml",
 49 //            "classpath*:spring_echannel_dependence.xml"
 50 //        });
 51 //        context.start();
 52 //    } 
 53     ClassPathXmlApplicationContext context=null;
 54     public MailDemo(){
 55         context=new ClassPathXmlApplicationContext(new String[]{
 56                 "classpath*:spring_echannel.xml",
 57                 "classpath*:spring_echannel_dependence.xml"
 58             });
 59             context.start();
 60     }
 61     
 62     
 63 
 64 //    @Autowired
 65 //    @Qualifier("mailSender")
 66 //    private transient JavaMailSender sender;
 67     
 68     @Autowired
 69     @Qualifier("velocityEngine")
 70     private transient VelocityEngine velocityEngine;
 71     
 72     @Value("${mail.to.default}")
 73     private transient String defaultTo;
 74 
 75     @Value("${mail.from.default}")
 76     private transient String from;
 77 
 78     @Value("${mail.subject.default}")
 79     private transient String defaultSubject;
 80 
 81     @Value("${mail.defaultEncoding}")
 82     private transient String encoding;
 83     
 84 //    @Ignore
 85     @Test
 86     public void test(){
 87         MailDemo test=new MailDemo();
 88         JavaMailSender sender = (JavaMailSender)test.context.getBean("mailSender");
 89         test.simpleMail(sender);
 90     }
 91     
 92     public void simpleMail(JavaMailSender sender) {
 93 
 94 //        //模板model
 95 //        MailModel model = new MailModel();
 96 //        model.setSubject("测试专用");
 97 //        model.setTo(new String[]{"eqweqweq8509@qq.com"});
 98         
 99         //数据
100         Map data=new HashMap();
101         data.put("name", "213");
102         data.put("address", "山西");
103         data.put("ip", "192.183.9.32");
104         data.put("url", "http://baijdu.com");
105         data.put("message", "大家好啊!");
106         
107         //附件
108 //        Map attach=new HashMap(); 
109         String attle="121213123123123.xml";
110         try {
111              attle = MimeUtility.encodeText(attle, "utf-8", "Q");
112              File name=new File("D:/axtmp/20160129_120626-2524.xml");
113 //             attach.put(attle, new UrlResource("D:/axtmp/20160129_120626-2524.xml"));
114              
115              MimeMessage m = sender.createMimeMessage();
116              MimeMessageHelper helper=new MimeMessageHelper(m,true,"utf-8");
117              helper.addAttachment(attle, name);
118              helper.setTo("eqweqweq8509@qq.com");
119              helper.setSubject("测试专用");
120              helper.setFrom("******");//来源,使用的邮件服务器
121 //             VelocityEngine velocityEngine=VelocityEngine.;
122              VelocityEngineFactory factory=new VelocityEngineFactory();
123              VelocityEngine ve=null;
124              ve=factory.createVelocityEngine();
125              String url = MailDemo.class.getResource("/mail/template/").getPath();
126              String path="/mail/template/testTemplate.vm";
127              path=path.substring(1,path.length());
128 //             StringResource resource=new StringResource(path,"utf-8");
129 //             StringResourceRepository rep = StringResourceLoader.getRepository();
130 //             rep.putStringResource(path, "testTemplate.vm");
131 //             String path1="/mail/template/testTemplate.vm";
132              String message = VelocityEngineUtils.mergeTemplateIntoString(ve, path, data);
133              //使用Velocity模板的时候注意:使用相对路径时可能会报找不到资源的异常,模板放置到项目根路径想即可
134 //             String message="冯浩,你好,测试用的";
135              helper.setText(message);
136              sender.send(m);
137         } catch (Exception e) {
138             e.printStackTrace();
139         }
140        
141        
142 
143     }
144     
145     @Ignore
146     @Test
147     public void Test(){
148 //         String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
149 //         System.out.println("path "+path);
150 //         for (int i = 0; i < 2; i++) {
151 //             path=path.substring(0, path.lastIndexOf("/"));
152 //        }
153 //         System.out.println("path1 "+path);
154         String path2 = MailDemo.class.getClassLoader().getResource("").getPath();
155         System.out.println("classloader is "+path2);
156         String path = MailDemo.class.getResource("/mail/template/").getPath();
157         System.out.println("path is "+path);
158          
159     }
160     
161 }
View Code

1、基于spring-context-supportjar包写的邮件发送类

2、须有email.peoperties配置文件

3、javamailSender属性注入,在配置文件里,有spring加载

原文地址:https://www.cnblogs.com/nihaofenghao/p/5957633.html