Spring 整合javamail 用 gmail 发送邮件

      Spring整合javamail成功之后,把大量的代码配置到spring的配置文件里了,从而降低了Javamail对邮箱、端口、协议、主题、内容…… 的耦合。在客户端上只需要调用一行代码就可以发送邮件了。

    首先不需要往项目的lib里导入jar,但是需要在tomcatlib里导入3jar文件activation.jar jspsmartupload.jarmail.jar 导入这3jar文件,这样tomcat就不会报错了。


 SendManagerService .java

package  com.cgt.itspiderman.service.email;

public   interface  SendManagerService {
    
public   void  sendMail(String email); 
}

 定义一个发送邮件的接口.


SendManagerServiceImp.java

package  com.cgt.itspiderman.service.email;

import  javax.mail.Message;
import  javax.mail.internet.InternetAddress;
import  javax.mail.internet.MimeMessage;

import  org.springframework.mail.SimpleMailMessage;
import  org.springframework.mail.javamail.JavaMailSender;
import  org.springframework.mail.javamail.MimeMessagePreparator;

public   class SendManagerServiceImp implements  SendManagerService {

    
private  JavaMailSender mailSender;
    
    
private  SimpleMailMessage message;
        
    
public   void  sendMail( final  String email) {
        
//  TODO Auto-generated method stub
        
        MimeMessagePreparator preparator 
=   new  MimeMessagePreparator()
        {
            
public   void  prepare(MimeMessage mimeMessage)  throws  Exception {
                
                mimeMessage.setRecipient(Message.RecipientType.TO, 
new  InternetAddress(email));
                mimeMessage.setFrom(
new  InternetAddress(message.getFrom()));
                mimeMessage.setSubject(message.getSubject(),
" gbk " );
                mimeMessage.setText(message.getText(),
" gbk " );
            }
        };
        mailSender.send(preparator);
    
    }
    
    
public  JavaMailSender getMailSender() {
        
return  mailSender;
    }

    
public   void  setMailSender(JavaMailSender mailSender) {
        
this .mailSender  =  mailSender;
    }

    
public  SimpleMailMessage getMessage() {
        
return  message;
    }

    
public   void  setMessage(SimpleMailMessage message) {
        
this .message  =  message;
    }

}

发送邮件的实现类.

 然后在WEB-INF下面创建一个文件messages.properties    以后修改配置信息直接在这里修改就可以了。

host = smtp.gmail.com
username
= [email protected]
password
=xxxxxx
port
= 465
mail.smtp.auth
= true
mail.smtp.timeout
= 25000
mail.smtp.starttls.enable
= true
mail.smtp.socketFactory.
class = javax.net.ssl.SSLSocketFactory
mail.smtp.socketFactory.fallback
= false
from
= [email protected]
subject
= /u6807/u9898
text
= /u5185/u5BB9/u554A


applicationContext.xml

  .......................

<
bean id = " propertyConfigurer "   class = " org.springframework.beans.factory.config.PropertyPlaceholderConfigurer " >
      
< property name = " locations " >
        
< list >
           
< value > WEB - INF / mail.properties </ value >
        
</ list >  
      
</ property >  
    
</ bean >

    
<!--  gmail 发送邮箱的配置文件  -->
       
< bean id = " mailSender "   class = " org.springframework.mail.javamail.JavaMailSenderImpl " >
           
< property name = " host " >
               
< value > ${host} </ value >
           
</ property >        
           
< property name = " username " >
               
< value > ${username} </ value >
           
</ property >
           
< property name = " password " >
               
< value > ${password} </ value >
           
</ property >
           
< property name = " port " >
               
< value > ${port} </ value >
           
</ property >
           
< property name = " javaMailProperties " >
               
< props >
                   
< prop key = " mail.smtp.auth " > ${mail.smtp.auth} </ prop >
                   
< prop key = " mail.smtp.timeout " > ${mail.smtp.timeout} </ prop >
                   
< prop key = " mail.smtp.starttls.enable " > ${mail.smtp.starttls.enable} </ prop >
                   
< prop key = " mail.smtp.socketFactory.class " > ${mail.smtp.socketFactory. class } </ prop >
                   
< prop key = " mail.smtp.socketFactory.fallback " > ${mail.smtp.socketFactory.fallback} </ prop >  
               
</ props >
           
</ property >
       
</ bean >
       
< bean id = " mailMessage "   class = " org.springframework.mail.SimpleMailMessage " >
           
< property name = " from " >
               
< value > ${from} </ value >
           
</ property >
           
< property name = " subject " >
               
< value > ${subject} </ value >
           
</ property >
           
< property name = " text " >
               
< value > ${text} </ value >
           
</ property >
       
</ bean >
       
< bean id = " sendmail "   class = " com.cgt.itspiderman.service.email.SendManagerServiceImp " >
           
< property name = " mailSender "  ref = " mailSender " ></ property >
           
< property name = " message "  ref = " mailMessage " ></ property >
       
</ bean >
       
<!--  end  -->  

............................

 

public   static   void  main(String[] args) {
        
        ApplicationContext context
=   null ;
        context 
=   new  ClassPathXmlApplicationContext( " applicationContext.xml " );
        SendManagerService sms
=     (SendManagerService)context.getBean( " sendmail " );
        sms.sendMail(
" [email protected] " );
    }



 如果是在struts2里使用的话需要在Action里加上private SendManagerService sendemail;(加上get和set),然后在spring里配置这个Action的时候加上<property name="sendemail" ref="sendmail"></property>
然后在Action里调用下面这一行代码就可以发送了。
sendemail.sendMail("[email protected]");
  写到这里大家会问,如果需要动态的修改这个邮件的内容呢?方法很简单
     

  WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
        ((SimpleMailMessage) context.getBean(
" mailMessage " )).setText( " 我现在修改了邮件的内容 呵呵呵 " );

你可能感兴趣的:(Spring 整合javamail 用 gmail 发送邮件)