使用Spring Mail发送邮件

1.所需的Jar包:

Spring.jar

log4jXXX.jar

commons-logging.jar

以上是spring所需要的Jar包。

freemarker.jar

这个是使用模版必须的Jar包。

mail.jar

activation.jar

这两个是java mail需要的Jar包。

 

ApplicationContext.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-2.5.xsd
                    ">
 <bean id="mailSender"   
     class="org.springframework.mail.javamail.JavaMailSenderImpl">   
     <property name="host" value="smtp.qq.com"></property>   
     <property name="javaMailProperties">   
         <props>   
             <prop key="mail.smtp.auth">true</prop>   
             <prop key="mail.smtp.timeout">25000</prop>   
         </props>   
     </property>   
     <property name="username" value="[email protected]" />   
     <property name="password" value="XXX" />   
 </bean>   

</beans>

发送程序(SpringMail.java):

import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;


import freemarker.template.Configuration;
import freemarker.template.Template;


public class SpringMail {    
    private Configuration cfg = new Configuration();      
        
 public static void main(String[] args) throws Exception {      
            
            SpringMail springMail = new SpringMail();   
//            springMail.sendMailWithAnnex();
             springMail.sendMail();  
         }      
           
         private void sendMail() throws Exception {    
          ApplicationContext ctx = new FileSystemXmlApplicationContext(      
             "src/applicationContext.xml");      
          JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");      
             SimpleMailMessage mail = new SimpleMailMessage();      
             mail.setTo("[email protected]"); //接收人      
             mail.setFrom("[email protected]"); //发送人      
             mail.setSubject("这是一个Spring发送邮件的例子");      
             //嵌入ftl模版      
             cfg.setClassForTemplateLoading(getClass(), "/mail");      
             Map root = new HashMap();      
             root.put("username", "李波"); //模板变量      
             Template t = cfg.getTemplate("notify-mail.ftl");      
             StringWriter writer = new StringWriter();      
             t.process(root, writer);      
             //把模版内容写入邮件中      
             mail.setText(writer.toString());      
             sender.send(mail);      
             System.out.println("邮件发送成功!");      
         }  
         
         // 带附件
         public void sendMailWithAnnex() throws Exception {
         // TODO Auto-generated method stub
       ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
       JavaMailSender mailSender= (JavaMailSender) context.getBean("mailSender");
       MimeMessage mime = mailSender.createMimeMessage();
       MimeMessageHelper helper;
       try {
        helper = new MimeMessageHelper(mime,true,"utf-8");
        helper.setFrom("[email protected]");
        helper.setTo("[email protected]");
        helper.setSubject(" 测试spring Mail的附件功能");
        //需要将附件显示在html中
        //在标签中用cid:xx 标记,使用helper.addInline()方法添加
        helper.setText("<html><body>javaeye是个好网站:<br>"+
          "<a href='http://www.javaeye.com'>" +
          "<img src='cid:logo'></a></body></html>",true);
        helper.addInline("logo", new ClassPathResource("logo.gif"));
        helper.addAttachment("javaeye.gif", new ClassPathResource("javaeye.gif"));
        
       } catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
      
       mailSender.send(mime);
       System.out.println("带附件的邮件发送成功!");
         }
            
}  

notify-mail.ftl:

<body>
欢迎加入!      
      
亲爱的${username}      
      
请点击链接完成注册:      <a href='http://www.javaeye.com'>http://www.javaeye.com</a>
      
如果您的email程序不支持链接点击,请将上面的地址拷贝至您的浏览器(如IE)的地址栏进入****。      
      
您可以在***:      
      
查看最新的影视资料,查看各种相关消费产品,在这里交友,灌水……;      
      
希望您在**度过快乐的时光!      
      
-       
      
(这是一封自动产生的email,请勿回复。)     
</body>

 

你可能感兴趣的:(spring)