2017-03-08 105 views
0

發送郵件通過電子郵件發送我無法使用Spring框架

org.springframework.mail.MailSendException: Failed messages: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. b10sm22671312wmi.34 - gsmtp

例外上述

代碼,我用來發送郵件就像

MailRequest mailRequest = new MailRequest(); 
mailRequest.setSubject(messageByLocale.getMessage("mail.subject.forgetpassword")); 
mailRequest.setTemplateName(messageByLocale.getMessage("mail.template.forgetpassword")); 
mailRequest.setToEmail(tbNstyleloyalty.getEmail()); 
Map<String, Object> map = new HashMap<>(); 
map.put("tbNstyleloyalty", tbNstyleloyalty); 
mailingConfig.sendEmail(mailRequest, map); 

和我的sendmail的方法就像

@Async 
public void sendEmail(MailRequest mailRequest, Map<String, Object> model) { 
    MimeMessagePreparator preparator = new MimeMessagePreparator() 
    { 
     @Override 
     public void prepare(MimeMessage mimeMessage) throws Exception { 
       MimeMessageHelper message = new MimeMessageHelper(mimeMessage); 
       message.setTo(mailRequest.getToEmail()); 
       message.setSubject(mailRequest.getSubject()); 
       message.setText(VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,templatePath + mailRequest.getTemplateName() + ".vm", ApplicationConstants.CHARSET_UTF8, model),true); 
     } 
    }; 
    this.javaMailSender.send(preparator); 
} 

請幫我克服這個問題。謝謝

+0

您使用的是哪個Spring版本?你使用SpringBoot嗎?你能提供一些像依賴和application.properties的上下文嗎? – JeanValjean

+0

是的,我使用的是春季啓動,你想要郵件屬性和依賴關係嗎? –

+0

節省時間並使用[電子郵件工具庫](https://github.com/ozimov/spring-boot-email-tools) – JeanValjean

回答

0

測試值,謝謝你們對你的答案被你給出所有的答案是正確的,但我無法理解的是,我需要把這個屬性然後最後我添加此

spring.mail.properties.mail.smtp.starttls.enable = true 

我的財產文件,然後我成功了。

0

也許你缺少要麼2點:

  1. 設置以下屬性在您的電子郵件請求。

props.put("mail.smtp.starttls.enable", "true");

  • 你可能嘗試使用端口25的郵件服務器將郵件傳遞到第三方在未經認證的connection.You需要問你的SMTP客戶端連接到已認證的連接。 (看,如果你的端口號是TLS連接)
  • +0

    謝謝你的回答,我正在使用587端口發送郵件 –

    0

    我認爲下面的代碼是很簡單的用於郵件功能

    //headers 
    import javax.mail.Message; 
    import javax.mail.Message.RecipientType; 
    import javax.mail.MessagingException; 
    import javax.mail.Session; 
    import javax.mail.Transport; 
    import javax.mail.internet.AddressException; 
    import javax.mail.internet.InternetAddress; 
    import javax.mail.internet.MimeBodyPart; 
    import javax.mail.internet.MimeMessage; 
    import javax.mail.internet.MimeMultipart; 
    //code 
        Session mailSession = new SmtpImpl().getSession(); 
        Message simpleMessage = new MimeMessage(mailSession); 
        MimeMultipart multipart = new MimeMultipart("related"); 
        BodyPart messageBodyPart = new MimeBodyPart(); 
        try { 
         simpleMessage.setFrom(new InternetAddress("from address"))); 
         simpleMessage.addRecipient(RecipientType.TO, new InternetAddress("to address")); 
    
         String SENDCC_GET = "[email protected],person2.gmail.com"; 
         String [] SENDCC = SENDCC_GET.split(","); 
         InternetAddress[] addressCC = new InternetAddress[SENDCC.length]; 
         for (int i = 0; i < SENDCC.length; i++) { 
          addressCC[i] = new InternetAddress(SENDCC[i]); 
         } 
         //for bcc 
         simpleMessage.setRecipients(Message.RecipientType.BCC, addressCC); 
         //for cc 
         //simpleMessage.setRecipients(Message.RecipientType.CC, addressCC); 
    
         String message = null; 
         String subject = null; 
    
         subject = "email subject"; 
         simpleMessage.setSubject(subject); 
         message = "message body"; 
         messageBodyPart.setContent(message, "text/html; charset=utf-8"); 
         multipart.addBodyPart(messageBodyPart); 
         simpleMessage.setContent(multipart); 
         Transport.send(simpleMessage); 
    
        } catch (AddressException e) { 
         LOGGER.error("******Error while sending - Email: Address Exception::: " + e); 
        } catch (MessagingException e) { 
         LOGGER.error("******Error while sending - Email: Messaging Exception::: " + e); 
        } catch (Exception e) { 
         LOGGER.error(e.getMessage(), e); 
        } 
    
    
        //SmtpImpl.java 
    
        public Session getSession(){ 
         Properties props = new Properties(); 
         props.put("mail.smtp.host", EmailUtilityParam.getSmtpHostName()); 
         props.put("mail.smtp.port", EmailUtilityParam.getSmtpPort()); 
         props.put("mail.smtp.user", EmailUtilityParam.getAuthenticationId()); 
         props.put("mail.smtp.password", EmailUtilityParam.getAuthenticationPassword()); 
         props.put("mail.debug", "false"); 
         Session mailSession = Session.getDefaultInstance(props, 
           new javax.mail.Authenticator() { 
          protected PasswordAuthentication getPasswordAuthentication() { 
           return new PasswordAuthentication(EmailUtilityParam 
             .getAuthenticationId(), EmailUtilityParam 
             .getAuthenticationPassword()); 
          } 
         }); 
         return mailSession; 
        } 
    

    SMTP開發工具可用於開發和保持它在默認情況下它是否有效

    HOST = localhost 
    PORT = 25 
    USER(authentication id) = //empty string 
    PASSWORD(authentication password) = //empty string