springmvc發送郵件

<!--郵件發送實現類-->


    <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();


        }


    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章