Spring邮件服务:Maven + Spring SMTP Mail

Spring邮件服务:Maven + Spring SMTP Mail

前言:当然,发 email 不一定要用 Spring,直接用 javax.mail 的API 就能实现。但是由于 Spring 提供了一个发送电子邮件的高级抽象层,
它向用户屏蔽了底层邮件系统的一些细节,同时负责低层次的代表客户端的资源处理。所以用 Spring 来发送 email 会省事很多和让发邮件变的简单许多。
Spring邮件抽象层的主要包为org.springframework.mail。它包括了发送电子邮件的主要接口MailSender和封装了简单邮件属性的值对象SimpleMailMessage。

环境:
Spring     2.5.6 
Javamail  1.4.4 
Maven     3.0.4 
Myeclipse 8.6.1

项目结构:
Spring邮件服务:Maven + Spring SMTP Mail_第1张图片

spring-smtp-mail.xml
< beans  xmlns =http://www.springframework.org/schema/beans  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" >
  
    
< bean  id ="mailSender"  class ="org.springframework.mail.javamail.JavaMailSenderImpl" >
       
<!--  服务器  -->
        
< property  name ="host"  value ="smtp.yeah.net"   />
        
<!--  端口号  -->
        
< property  name ="port"  value ="25"   />
        
<!--  用户名  -->
        
< property  name ="username"  value ="[email protected]"   />
        
<!--   密码    -->
        
< property  name ="password"  value ="********"   />
        
<!--  SMTP服务器验证  -->
        
< property  name ="javaMailProperties" >
            
< props >
                
<!--  验证身份  -->
               
< prop  key ="mail.smtp.auth" > true </ prop >
           
</ props >
        
</ property >
    
</ bean >
    
<!--  
       目前我用过的EMAIL账号都是网易的,下面列出网易的SMTP服务器名和端口号:
        网易邮箱          SMTP服务器     SMTP端口     POP3服务器       POP3端口
        @126.com     smtp.126.com      25          pop3.126.com      110
        @163.com     smtp.163.com      25          pop3.163.com      110
        @yeah.net    smtp.yeah.net      25          pop3.yeah.net     110
    
-->
    
    
< bean  id ="simpleMailMessage"  class ="org.springframework.mail.SimpleMailMessage" >
       
<!--  发件人email  -->
        
< property  name ="from"  value ="[email protected]"   />
        
<!--  
         收件人email
        <property name="to" value="[email protected]" />
        email主题(标题)
        <property name="subject" value="Subject" />
        email主题内容
        <property name="text">
          <value>ContentText</value>
        </property>
        
-->
    
</ bean >
    
    
< bean  id ="simpleMail"  class ="com.fancy.util.Email" >
        
< property  name ="mailSender"  ref ="mailSender"   />
        
< property  name ="simpleMailMessage"  ref ="simpleMailMessage"   />
    
</ bean >
    
</ beans >

Email.java
package  com.fancy.util;

import  org.springframework.mail.MailSender;
import  org.springframework.mail.SimpleMailMessage;
/**
 * -----------------------------------------
 * @文件: Email.java
 * @作者: fancy
 * @邮箱: [email protected]
 * @时间: 2012-6-11
 * @描述: 发送Email工具类
 * -----------------------------------------
 
*/

public   class  Email  {
    
    
private MailSender mailSender;
    
private SimpleMailMessage simpleMailMessage;
    
  
/**
     * @方法名: sendMail 
     * @参数名:
@param subject  邮件主题
     * @参数名:
@param content 邮件主题内容
     * @参数名:
@param to         收件人Email地址
     * @描述语: 发送邮件
     
*/

    
public void sendMail(String subject, String content, String to) {
        
        simpleMailMessage.setSubject(subject); 
//设置邮件主题
        simpleMailMessage.setTo(to);             //设定收件人
        simpleMailMessage.setText(content);  //设置邮件主题内容
        mailSender.send(simpleMailMessage); //发送邮件
    }

     
//Spring 依赖注入
    public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) {
        
this.simpleMailMessage = simpleMailMessage;
    }

     
//Spring 依赖注入
    public void setMailSender(MailSender mailSender) {
        
this.mailSender = mailSender;
    }

}


Junit Test:EmailTest.java
package  com.fancy.test;

import  junit.framework.TestCase;
import  org.springframework.context.ApplicationContext;
import  org.springframework.context.support.ClassPathXmlApplicationContext;
import  com.fancy.util.Email;
/**
 * -----------------------------------------
 * @文件: EmailTest.java
 * @作者: fancy
 * @邮箱: [email protected]
 * @时间: 2012-6-11
 * @描述: Junit测试,运行将发送一封email
 * -----------------------------------------
 
*/

public   class  EmailTest  extends  TestCase  {

    
public void testSendMail() {
        ApplicationContext context 
= new ClassPathXmlApplicationContext("spring-smtp-mail.xml");
        Email mail 
= (Email)context.getBean("simpleMail");
        mail.sendMail(
"Spring SMTP Mail Subject""Spring SMTP Mail Text""[email protected]");
        
//mail.sendMail("标题", "内容", "收件人邮箱");
    }


}


EmailAppTest.java 也是一个测试类,跟 Junit 的 EmailTest.java 是几乎一样的,这里就不贴出来了,再来看下 Maven:

pom.xml
< project  xmlns ="http://maven.apache.org/POM/4.0.0"  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation
="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
  
< modelVersion > 4.0.0 </ modelVersion >
  
< groupId > com.fancy </ groupId >
  
< artifactId > spring-mail-example </ artifactId >
  
< version > 1.0 </ version >
  
< packaging > jar </ packaging >
  
< name > spring-mail-example </ name >
  
< url > http://maven.apache.org </ url >
  
< properties >
    
< project .build.sourceEncoding > UTF-8 </ project.build.sourceEncoding >
  
</ properties >

  
< dependencies >
  
    
<!--  Spring framework  -->
    
< dependency >
      
< groupId > org.springframework </ groupId >
      
< artifactId > spring </ artifactId >
      
< version > 2.5.6 </ version >
    
</ dependency >
    
    
<!--  Javamail API  -->
    
< dependency >
      
< groupId > javax.mail </ groupId >
      
< artifactId > mail </ artifactId >
      
< version > 1.4.4 </ version >
    
</ dependency >
    
    
< dependency >
      
< groupId > junit </ groupId >
      
< artifactId > junit </ artifactId >
      
< version > 3.8.1 </ version >
      
< scope > test </ scope >
    
</ dependency >
    
  
</ dependencies >
</ project >

Run 一下 EmailTest.java,稍等一会就能收到一封 Email



Spring邮件服务:Maven + Spring SMTP Mail_第2张图片


OK,邮件发送成功。另外,在用网易邮箱测试的时候,发现不进行 SMTP验证 邮件也能发送成功,曾在 Shopxx 中提到,
如果是 Gmail 就必须配置SMTP服务器验证,否则抛出异常,发送会失败。


   [ 转载出处:http://www.blogjava.net/fancydeepin ]

你可能感兴趣的:(Spring邮件服务:Maven + Spring SMTP Mail)