簡體   English   中英

沒有發現異常,但郵件未在Spring MVC中發送

[英]No exception found but mail not send in Spring MVC

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

        }

    }
}

在此先感謝..不會發生任何異常。 但是郵件不發送?

有時候,Spring Boot 1.2.5遇到了同樣的問題。 看起來與最新版本的Java Mail類似,現在需要另一個屬性spring.mail.properties.mail.smtp.ssl.enable可以設置為true 有關詳細信息,請參見此帖子

另外,當我測試我的應用程序時,我發現僅提供常規的gmail密碼已不再起作用。 我需要一個經過兩步驗證的帳戶,並且必須使用應用程序密碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM