使用Java发送电子邮件。问题STARTTLS错误

使用Java发送电子邮件。问题STARTTLS错误,java,apache,smtp,apache-camel,Java,Apache,Smtp,Apache Camel,我知道以前有人问过这个问题,但我尝试了几种解决方案,问题仍然存在,因此,我们非常感谢您的帮助 我正在尝试配置通过gmail和smtp发送电子邮件。下面是我们的驼峰配置: <camel:camelContext xmlns="http://camel.apache.org/schema/spring"> <camel:route id="gmailRoute"> <camel:from uri="direct://gmail" />

我知道以前有人问过这个问题,但我尝试了几种解决方案,问题仍然存在,因此,我们非常感谢您的帮助

我正在尝试配置通过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****is@gmail.com?host=smtp.gmail.com&amp;port=587&amp;from=p****is@gmail.comi&amp;password=gl******" />
    </camel:route>
在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);

}
@覆盖
public void sendmail(字符串发件人、字符串[]收件人、字符串[]抄送、字符串[]密件抄送、字符串主题、字符串正文)
{
字符串端点=”direct://gmail";
字符串toString=null;
字符串ccString=null;
字符串bccString=null;
映射头=空;
toString=convertStringArrayToCSV(到);
ccString=convertStringArrayToCSV(cc);
bccString=convertstringarraytocv(bcc);
headers=newhashmap();
标题。放置(“主题”,主题);
if(toString!=null)
{
标题。放置(“To”,toString);
}
if(ccString!=null)
{
headers.put(“CC”,ccString);
}
if(bccString!=null)
{
标题。放置(“密件抄送”,密件抄送);
}
template.sendboyandheaders(端点、主体、标题);
}
问题是,当我们尝试使用sendmail方法发送电子邮件时,会出现以下错误:

捕获:com.sun.mail.smtp.SMTPSendFailedException:530 5.7.0必须首先发出STARTTLS命令


如果您有任何想法,我们将不胜感激。

您似乎正在使用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****is@gmail.com?host=smtp.gmail.com&amp;port=587&amp;from=p****is@gmail.comi&amp;password=gl******" />
  </camel:route>
</camel:camelContext>

对于Gmail来说,这似乎是一条出路:

smtp://smtp.gmail.com?port=587&username=USERNAME@gmail.com&password=PASSOWRD&mail.smtp.auth=true&mail.smtp.starttls.enable=true

最后一个参数就是您的错误消息所抱怨的参数。基本上,列出的Java代码中的所有自定义SMTP参数也需要位于Camel endpoint中。

要从Camel发送到Gmail,您需要以下内容:-

  • 密钥库中的Gmail SMTP证书(即OpenSSL或Python脚本以及Java密钥工具)
  • 允许不太安全的应用程序(不可取)
  • 或者使用应用程序密码为您的路线设置两步身份验证
  • 已配置SSLContextParameters
  • 您可以使用下面列出的说明来完成这些步骤:-

    在这种情况下,我只得到javax.net.ssl.SSLException:无法识别的ssl消息,纯文本连接?
    smtp://smtp.gmail.com?port=587&username=USERNAME@gmail.com&password=PASSOWRD&mail.smtp.auth=true&mail.smtp.starttls.enable=true