作者 : darcy   时间 : 2015-09-05   浏览 : ...

spring发送邮件已经有实现类,可以在方法中直接使用或通过配置bean使用。

首先导入依赖的jar包:
<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1</version>
</dependency>

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


在方法中直接使用:
public static void main(String[] args){
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.setHost("smtp.163.com");
    mailSender.setUsername("seaxiang");
    mailSender.setPassword("******");
    
    SimpleMailMessage smm = new SimpleMailMessage();
    smm.setFrom("seaxiang_dev@163.com");
    smm.setTo("seaxiang@163.com");
    smm.setSubject("hi");
    smm.setText("hi,this is a test!");
    mailSender.send(smm);
}    


通过配置bean的使用方法:
<!-- 邮件工具类 -->
<bean id="mailSender" class="com.seaxiang.utils.MailSender"></bean>    

<!--邮件发送实现类-->
<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="smtp.163.com"/>
    <property name="username" value="seaxiang_dev"/>
    <property name="password" value="***"/>
    <property name="javaMailProperties">
        <props >
            <prop key="mail.smtp.auth">true</prop>
        </props>
    </property>
</bean>

<bean id="simpleMailMessage" class="org.springframework.mail.SimpleMailMessage">
    <property name="from" value="seaxiang@163.com" />
     <property name="to" value="***@qq.com" />
    <property name="subject" value="seaxiang" />
</bean>

 <!--线程池异步发送邮件-->
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="5" />
    <property name="maxPoolSize" value="10" />
    <property name="queueCapacity" value="25" />
</bean>
public class MailSender {
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private SimpleMailMessage simpleMailMessage;
    @Autowired
    private TaskExecutor taskExecutor;  

    /**
     * 发送邮件
     * @param to        收件人邮箱
     * @param subject   邮件主题
     * @param content   邮件内容
     */
    public void send(String to, String subject, String content) {
        this.taskExecutor.execute(new SendMailThread(to, subject, content));
    }

    //内部线程类,利用线程池异步发邮件。
    private class SendMailThread implements Runnable {
        private String to;
        private String subject;
        private String content;
        private SendMailThread(String to, String subject, String content) {
            super();
            this.to = to;
            this.subject = subject;
            this.content = content;
        }
        
        public void run() {
            sendMail(to, subject, content);
        }
        
        /**
         * 发送邮件
         * @param to        收件人邮箱
         * @param subject   邮件主题
         * @param content   邮件内容
         */
        private void sendMail(String to, String subject, String content) {
            if(to != null){
                simpleMailMessage.setTo(to);//收件地址
            }
            if(subject != null){
                simpleMailMessage.setSubject(subject);
            }
            simpleMailMessage.setText(content);//内容
            javaMailSender.send(simpleMailMessage);
        }
    }
}



原创文章如转载,请注明本文链接: http://www.seaxiang.com/blog/spring-send-email
seaxiang  All Rights Reserved   粤ICP备15083047号-1