码迷,mamicode.com
首页 > 编程语言 > 详细

spring 发送邮件验证不通过问题

时间:2016-01-05 21:01:45      阅读:1055      评论:0      收藏:0      [点我收藏+]

标签:

  1. 首先要保证用户名密码正确,且邮箱支持smtp服务,特别注意的是QQ邮箱第三方登录需要用授权码来作为密码而不是QQ密码,授权码在邮箱设置>账户中可以获得
  2. smtp服务端口号需要以SLL加密端口号465 链接邮箱,在spring-mail配置文件中需要如下配置
    <beans xmlns="http://www.springframework.org/schema/beans"  
     xmlns:context="http://www.springframework.org/schema/context"  
     xmlns:p="http://www.springframework.org/schema/p"  
     xmlns:mvc="http://www.springframework.org/schema/mvc"  
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
     xmlns:aop="http://www.springframework.org/schema/aop"  
     xmlns:tx="http://www.springframework.org/schema/tx"  
     xsi:schemaLocation="http://www.springframework.org/schema/beans  
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
          http://www.springframework.org/schema/context  
          http://www.springframework.org/schema/context/spring-context.xsd  
          http://www.springframework.org/schema/tx   
          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
          http://www.springframework.org/schema/aop  
          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
          http://www.springframework.org/schema/mvc  
          http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <!-- 配置javamailsend --> <bean id="javamailsend" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="${mail.host}"></property> <property name="port" value="${mail.port}"></property> <property name="username" value="${mail.username}"></property> <property name="password" value="${mail.password}"></property> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> <!-- 要采用加密方式连接,所以需要加下面这行配置 --> <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop> <prop key="mail.smtp.timeout">25000</prop> </props> </property> </bean>
    <!-- 配置freemark --> <bean id="freemarker" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="WEB-INF/template/"></property> <property name="freemarkerSettings"><!-- 设置FreeMarker环境属性--> <props> <prop key="template_update_delay">1800</prop><!--刷新模板的周期,单位为秒--> <prop key="default_encoding">UTF-8</prop><!--模板的编码格式 --> <prop key="locale">zh_CN</prop><!-- 本地化设置--> </props> </property> </bean> <bean id="mailtemplate" class="com.fcq.util.MailTemplate"> <property name="freemarker" ref="freemarker"></property> <property name="javamailsend" ref="javamailsend"></property> </bean> </beans>

     

    3.常规生成模板,发送邮件工具类MailTemplate.java
    package com.fcq.util;
    
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.mail.MessagingException;
    import javax.mail.internet.MimeMessage;
    
    import org.springframework.mail.MailException;
    import org.springframework.mail.MailMessage;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.mail.javamail.MimeMailMessage;
    import org.springframework.mail.javamail.MimeMessageHelper;
    import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
    import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
    
    import freemarker.template.Template;
    import freemarker.template.TemplateException;
    
    public class MailTemplate {
        
        private JavaMailSender javamailsend;
        private FreeMarkerConfigurer freemarker;
        
        public JavaMailSender getJavamailsend() {
            return javamailsend;
        }
        public void setJavamailsend(JavaMailSender javamailsend) {
            this.javamailsend = javamailsend;
        }
        public FreeMarkerConfigurer getFreemarker() {
            return freemarker;
        }
        public void setFreemarker(FreeMarkerConfigurer freemarker) {
            this.freemarker = freemarker;
        }
        
        //将freemarker模板转换成 String类型的邮件模板
        public String getMailTemplate(String name){
            String mailtemplate = "";
            Date date = new Date();
            SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd");
            String time = sFormat.format(date);
            Map<String,String> map = new HashMap<String,String>();
            try {
                Template template = freemarker.getConfiguration().getTemplate("mailTemplate.ftl");
                map.put("username",name);
                map.put("currenttime",time);
                mailtemplate = FreeMarkerTemplateUtils.processTemplateIntoString(template,map);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (TemplateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return mailtemplate;
        }
        
        public boolean sendMail(String mail){
            Boolean sendFlag = false; 
            try {
                MimeMessage message = javamailsend.createMimeMessage();
                MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
                helper.setSubject("注册-模板邮件");
                helper.setFrom("*****@qq.com");
                helper.setTo(mail);
                helper.setText(getMailTemplate(mail));
                javamailsend.send(message);
                sendFlag = true;
            } catch (MailException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (MessagingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return sendFlag;
        }
        
    }

     

spring 发送邮件验证不通过问题

标签:

原文地址:http://www.cnblogs.com/fcq709032083/p/5103645.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!