没有发现异常,但邮件没有发送到Spring MVC

时间:2015-08-14 06:13:30

标签: java spring email spring-mvc

我是spring MVC的新人我在春季发送电子邮件时遇到了问题。没有例外,但邮件不发送。

我的applicationContext.xml

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">  
        <property name="host" value="smtp.gmail.com" />  

        <property name="username" value="uname" />  
        <property name="password" value="pass" />  
        <property name="javaMailProperties">  
            <props>  
               <prop key="mail.smtp.auth">true</prop>  
              <prop key="mail.smtp.socketFactory.port">465</prop>  
              <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>  
              <prop key="mail.smtp.port">465</prop>  
            </props>  
        </property>  
    </bean> 

我的控制器类

@Controller
public class WebController {

//    System.out.println("suceees");
    @Autowired
     private JavaMailSender mailSender;  

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {

        return "index";

    }

    @RequestMapping(value = "/redirect", method = RequestMethod.GET)
    public String redirect() {

         sendMail();
        return "redirect:finalPage";
    }

    @RequestMapping(value = "/finalPage", method = RequestMethod.GET)
    public String finalPage() {


        return "final";
    }

    public void sendMail() {

        try {
            MimeMessage message = mailSender.createMimeMessage();

            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom("sender");
            helper.setTo("receiver");
            helper.setSubject("hi");
            helper.setText("welcome");

            // attach the file
            FileSystemResource file = new FileSystemResource(new File("/home/ajmal/Pictures/messi.jpg"));
            helper.addAttachment("messi.jpg", file);//image will be sent by this name

            mailSender.send(message);
 } catch (MailException | MessagingException ex) {

            System.err.println("error");

        }

    }
}

提前感谢..不会发生异常。但是邮件没发送?

1 个答案:

答案 0 :(得分:5)

我们在Spring Boot 1.2.5中遇到了同样的问题。使用最新版本的Java Mail,现在需要另一个属性 - <form method="POST" action="/auth/register" class="ui large form"> {!! csrf_field() !!} <div class="two fields dirright alignright"> <div class="field" > <div class="ui right icon input"> <i class="user icon"></i> {!! Form::text( 'first_name', Input::old('first_name'), array( 'class' => 'dirright alignright fontfamily', 'placeholder' => 'نام' ) ) !!} </div> </div> <div class="field" > <div class="ui right icon input"> <i class="user icon"></i> {!! Form::text( 'last_name', Input::old('last_name'), array( 'class' => 'dirright alignright fontfamily', 'placeholder' => 'نام خانوادگی' ) ) !!} </div> </div> </div> <div class="field"> <div class="ui left icon input latintext"> <i class="mail icon"></i> {!! Form::email( 'email', Input::old('email'), array( 'class' => 'latintext', 'placeholder' => 'E-mail address' ) ) !!} </div> </div> <div class="field"> <div class="ui left icon input latintext"> <i class="lock icon"></i> {!! Form::password( 'password', Input::old('password'), array( 'class' => 'latintext', 'placeholder' => 'Password' ) ) !!} </div> </div> <div class="ui fluid large primary submit button">ثبت نام</div> <div class="ui error message alignright"></div> </form> 设置为spring.mail.properties.mail.smtp.ssl.enable。有关详细信息,请参阅this post

此外,当我测试我的应用程序时,我看到仅仅提供常规gmail密码不再有效。我需要一个2-step verified帐户,并且必须使用application password