在Spring MVC中未发现异常,但邮件未发送

4

我是新手在spring MVC中,在使用spring发送邮件时遇到了问题。没有异常抛出,但邮件无法发送。

我的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");

        }

    }
}

感谢您的提前帮助。不会出现任何异常,但邮件没有发送?

你尝试过使用简单的Java程序发送邮件吗?http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/ - Subodh Joshi
在你的实际代码中,你是否用实际的邮件ID替换senderreceiver - Bond - Java Bond
我使用实际邮件 ID 运行。 - Ajmal Muhammad
1个回答

7

我们曾经遇到过相同的问题,使用Spring Boot 1.2.5版本。似乎最新的Java Mail版本需要另外一个属性进行设置:spring.mail.properties.mail.smtp.ssl.enable 需要设置为 true。详情请参见此帖子

此外,在测试应用程序时,我发现仅提供常规的gmail密码不再起作用了。我需要一个双重验证账户并且必须使用应用程序密码


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接