返回顶部

收藏

SpringMVC 邮件发送配置及代码

更多

首先是spring中的配置:(里面使用了FreeMarker模板引擎)

<!--邮件发送实现类-->
    <bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.qq.com"/>
        <property name="port" value="25"/>
        <property name="username" value="你的邮箱"/>
        <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="发件人" />
        <property name="subject" value="邮件主题" />
        <!--
        <property name="text" value="邮件内容" />
        -->
    </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>
    <!--FreeMarker模板-->
    <bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value="classpath:template" />
        <!--<property name="templateLoaderPath" value="/template" />-->
        <property name="freemarkerSettings">
            <props>
                <prop key="locale">zh_CN</prop>
                <prop key="default_encoding">UTF-8</prop>
            </props>
        </property>
    </bean>

发送邮件的工具类:(采用注解形式注入管理邮件发送的类)

public class MailSender {
    @Inject
    private JavaMailSender javaMailSender;
    @Inject
    private SimpleMailMessage simpleMailMessage;
    @Inject
    private FreeMarkerConfigurer freeMarkerConfigurer;
    @Inject
    private TaskExecutor taskExecutor;
    /**
     * 构建邮件内容,发送邮件。
     * @param user  用户
     * @param url   链接
     */
    public void send(User user, String url,String email) {
        String nickname = user.getNickname();
        String to = email;
        String text = "";
        Map<String, String> map = new HashMap<String, String>(1);
        map.put("url", url);
        try {
//            从FreeMarker模板生成邮件内容
            Template template = freeMarkerConfigurer.getConfiguration().getTemplate("register_mail.ftl");
//            模板中用${XXX}站位,map中key为XXX的value会替换占位符内容。
            text = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
        this.taskExecutor.execute(new SendMailThread(to,null,text));
    }
    //    内部线程类,利用线程池异步发邮件。
    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;
        }
        @Override
        public void run() {
            sendMail(to, subject, content);
        }
    }
    /**
     * 发送邮件
     * @param to        收件人邮箱
     * @param subject   邮件主题
     * @param content   邮件内容
     */
    public void sendMail(String to, String subject, String content) {
        try {
            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper messageHelper = new MimeMessageHelper(message, true, "UTF-8");
            messageHelper.setFrom(simpleMailMessage.getFrom());
            if (subject != null) {
                messageHelper.setSubject(subject);
            } else {
                messageHelper.setSubject(simpleMailMessage.getSubject());
            }
            messageHelper.setTo(to);
            messageHelper.setText(content, true);
           javaMailSender.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}

服务层调用:

1、要注入发送邮件工具类

    @Inject
    private MailSender mailSender;

2、调用代码

        User user = (User)request.getSession().getAttribute("user");
        String url = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+
                request.getContextPath();
        mailSender.send(user,url,email);

标签:java,springMVC

收藏

0人收藏

支持

0

反对

0

发表评论