SpringMvc整合JavaMail的实践

0、maven中添加依赖文件


        javax.mail
        mail
        1.4.7

1、添加mail.properties文件

mail.host=smtp.mxhichina.com
mail.username=xxxxxx
mail.password=xxxxxxx
mail.auth=true
mail.timeout=30000
mail.debug=true

2、添加spring-mail.xml



    
      
     
    
    
    
        
        
        
        
            
                ${mail.auth}
                ${mail.timeout}
                ${mail.debug}
                
            
        
    
    
      
          
          
    
    

3、实现MailUtil类

package com.ewen.utils;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Service;

@Service
public class MailUtil {
    
    @Resource
    private MailSender mailSender; 
    
    @Resource
    private SimpleMailMessage simpleMailMessage;  
     
    /** 
     * 单发 
     * 
     * @param recipient 收件人 
     * @param subject 主题 
     * @param content 内容 
     */  
    public void send(String recipient,String subject,String content){  
        simpleMailMessage.setTo(recipient);  
        simpleMailMessage.setSubject(subject);  
        simpleMailMessage.setText(content);  
        mailSender.send(simpleMailMessage);  
    }  
      
    /** 
     * 群发 
     * 
     * @param recipients 收件人 
     * @param subject 主题 
     * @param content 内容 
     */  
    public void send(List recipients,String subject,String content){  
        simpleMailMessage.setTo(recipients.toArray(new String[recipients.size()]));  
        simpleMailMessage.setSubject(subject);  
        simpleMailMessage.setText(content);  
        mailSender.send(simpleMailMessage);  
    }  
}

4、在spring-context.xml中引入spring-mail.xml


你可能感兴趣的:(SpringMvc整合JavaMail的实践)