2014-04-23 30 views
2

我知道這個問題已經被問過,但我嘗試了幾個解決方案,問題仍然存在,所以和幫助將不勝感激。發送電子郵件與駱駝Java。 ISSUE STARTTLS錯誤

我想配置發送電子郵件throu gmail和smtp。下面是我們的駱駝配置:

<camel:camelContext xmlns="http://camel.apache.org/schema/spring"> 

    <camel:route id="gmailRoute"> 
     <camel:from uri="direct://gmail" /> 
     <camel:to 
      uri="smtp://p****[email protected]?host=smtp.gmail.com&amp;port=587&amp;from=p****[email protected]&amp;password=gl******" /> 
    </camel:route> 

對我們的服務實現的init方法,我們有以下幾點:

log.info("Initializing Email Service"); 

    Properties props = new Properties(); 
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.starttls.enable", "true"); 
    props.put("mail.smtp.host", "smtp.googlemail.com"); 
    props.put("mail.smtp.port", "587"); 
    Authenticator auth = null; 
     props.put("mail.smtp.auth", "true"); 
     auth = new Authenticator() { 
      protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication("******[email protected]", "gl*****s"); 
      } 
     }; 
    Session t = Session.getInstance(props, auth); 

    template = camelContext.createProducerTemplate(); 

和sendmail的方法有:

@Override 
public void sendEmail(String from, String[] to, String[] cc, String[] bcc, String subject, String body) 
{ 
String endpoint = "direct://gmail"; 
    String toString = null; 
    String ccString = null; 
    String bccString = null; 
    Map<String, Object> headers = null; 

    toString = convertStringArrayToCSV(to); 
    ccString = convertStringArrayToCSV(cc); 
    bccString = convertStringArrayToCSV(bcc); 

    headers = new HashMap<String, Object>(); 
    headers.put("Subject", subject); 
    if (toString != null) 
    { 
     headers.put("To", toString); 
    } 

    if (ccString != null) 
    { 
     headers.put("CC", ccString); 
    } 

    if (bccString != null) 
    { 
     headers.put("BCC", bccString); 
    } 

    template.sendBodyAndHeaders(endpoint, body, headers); 

} 

問題當我們嘗試使用sendmail方法發送電子郵件時,我們得到以下錯誤:

caught: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first.

任何想法將不勝感激。

回答

0

看起來您正在使用TLS安全來發送您的電子郵件,但使用的是非安全版本的郵件組件。你可以嘗試改變smtp smtps?

<camel:camelContext xmlns="http://camel.apache.org/schema/spring"> 
    <camel:route id="gmailRoute"> 
    <camel:from uri="direct://gmail" /> 
    <camel:to uri="smtps://p****[email protected]?host=smtp.gmail.com&amp;port=587&amp;from=p****[email protected]&amp;password=gl******" /> 
    </camel:route> 
</camel:camelContext> 
+0

在這種情況下,我只是得到javax.net.ssl.SSLException:無法識別的SSL消息,純文本連接? – kaqqao

1

對於Gmail,這似乎是要走的路:一個你的錯誤信息是在抱怨

smtp://smtp.gmail.com?port=587&[email protected]&password=PASSOWRD&mail.smtp.auth=true&mail.smtp.starttls.enable=true 

這最後一個參數。基本上,您列出的所有Java代碼中的所有自定義SMTP參數都需要位於Camel端點中。

0

從駱駝發送到Gmail您需要: -

    在密鑰庫
  1. Gmail的SMTP認證(即OpenSSL的或與Java keytool的Python腳本一起)
  2. 要麼允許安全性較低的應用程序(不可取的)
  3. 或使用應用程序密碼路線設置兩步身份驗證
  4. SSLContextParameters配置

您可以用上市來實現這些步驟如下指令: -
https://mpashworth.wordpress.com/2017/05/03/sending-email-to-gmail-in-apache-camel/