美文网首页
SpringMvc整合JavaMail的实践

SpringMvc整合JavaMail的实践

作者: Peter520 | 来源:发表于2017-11-23 22:35 被阅读10次

0、maven中添加依赖文件

<dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.7</version>
</dependency>

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

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    
    <!-- 引入属性文件 -->  
    <context:property-placeholder location="classpath:mail.properties" /> 
    
    <!-- smtp.mxhichina.com smtp.126.com  -->
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="${mail.host}" />
        <property name="username" value="${mail.username}" />
        <property name="password" value="${mail.password}" />
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">${mail.auth}</prop>
                <prop key="mail.smtp.timeout">${mail.timeout}</prop>
                <prop key="mail.debug">${mail.debug}</prop>
                <!-- <prop key="mail.smtp.starttls.enable">true</prop> -->
            </props>
        </property>
    </bean>
    
    <bean id="simpleMailMessage" class="org.springframework.mail.SimpleMailMessage">  
        <!-- 发件人email -->  
        <property name="from" value="${mail.username}" />  
    </bean>
    
</beans>

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<String> 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

<import resource="classpath:spring/spring-mail.xml" />

相关文章

网友评论

      本文标题:SpringMvc整合JavaMail的实践

      本文链接:https://www.haomeiwen.com/subject/bebtbxtx.html