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

Ajm*_*mad 2 java email spring 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> 
Run Code Online (Sandbox Code Playgroud)

我的控制器类

@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");

        }

    }
}
Run Code Online (Sandbox Code Playgroud)

在此先感谢..不会发生任何例外.但是邮件没发送?

San*_*jay 5

使用Spring Boot 1.2.5,我们在某个时候遇到了同样的问题.看起来使用最新版本的Java Mail,现在需要另一个属性 - spring.mail.properties.mail.smtp.ssl.enable设置为true.有关详细信息,请参阅此帖

此外,当我测试我的应用程序时,我看到只是提供常规的Gmail密码不再工作了.我需要一个2步验证帐户,并且必须使用应用程序密码.