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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27331080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:11:00  来源:igfitidea点击:

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

javaemailexceptionplayframeworksmtp

提问by silentprogrammer

I am creating an app in play 2.2.1and trying to add email facility to it.For that I have added dependency in my build.sbt file.But getting an exception explained below

我正在创建一个应用程序play 2.2.1并尝试向其中添加电子邮件功能。为此,我在 build.sbt 文件中添加了依赖项。但在下面解释了异常

my code

我的代码

        String smtpHost = Play.application().configuration().getString("smtp.host");
        Integer smtpPort = Play.application().configuration().getInt("smtp.port");
        String smtpUser = Play.application().configuration().getString("smtp.user");
        String smtpPassword = Play.application().configuration().getString("smtp.password");

        Email mail = new SimpleEmail();
        try {
            mail.setFrom("[email protected]");
            mail.setSubject("hi");
            mail.setMsg("This is the message");
            mail.addTo("[email protected]");
        } catch (Exception e) {
            e.printStackTrace();
        }



        mail.setHostName(smtpHost);
        if (smtpPort != null && smtpPort > 1 && smtpPort < 65536) {
            mail.setSmtpPort(smtpPort);

        }
        if (!smtpUser.isEmpty()) {
            mail.setAuthentication(smtpUser, smtpPassword);
        }


        try {
            mail.send();
        } catch (Exception e) {
            e.printStackTrace();

   }

Included code in application.conf

application.conf 中包含的代码

# Email Configuration
smtp.host=smtp.gmail.com
smtp.port=587
smtp.ssl=yes
smtp.user="[email protected]"
smtp.password="123456"
smtp.auth=true
smtp.STARTTLS.enable=true

But I am getting an exception

但我得到了一个例外

org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.com:587
    at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1410)
    at org.apache.commons.mail.Email.send(Email.java:1437)
    at controllers.SendMail.registrationSuccessful(SendMail.java:53)
    at controllers.JobseekerController.registerJobseeker(JobseekerController.java:62)
    at Routes$$anonfun$routes$$anonfun$applyOrElse$$anonfun$apply.apply(routes_routing.scala:185)
    at Routes$$anonfun$routes$$anonfun$applyOrElse$$anonfun$apply.apply(routes_routing.scala:185)
    at play.core.Router$HandlerInvoker$$anon$$anon.invocation(Router.scala:183)
    at play.core.Router$Routes$$anon.invocation(Router.scala:377)
    at play.core.j.JavaAction$$anon.call(JavaAction.scala:56)
    at play.db.jpa.TransactionalAction.apply(TransactionalAction.java:20)
    at play.db.jpa.TransactionalAction.apply(TransactionalAction.java:18)
    at play.db.jpa.JPA.withTransactionAsync(JPA.java:177)
    at play.db.jpa.TransactionalAction.call(TransactionalAction.java:15)
    at play.core.j.JavaAction$$anon.apply(JavaAction.scala:91)
    at play.core.j.JavaAction$$anon.apply(JavaAction.scala:90)
    at play.core.j.FPromiseHelper$$anonfun$flatMap.apply(FPromiseHelper.scala:82)
    at play.core.j.FPromiseHelper$$anonfun$flatMap.apply(FPromiseHelper.scala:82)
    at scala.concurrent.Future$$anonfun$flatMap.apply(Future.scala:278)
    at scala.concurrent.Future$$anonfun$flatMap.apply(Future.scala:274)
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:29)
    at play.core.j.HttpExecutionContext$$anon.run(HttpExecutionContext.scala:37)
    at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:42)
    at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:386)
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
    at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
Caused by: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. cq6sm31661301pad.30 - gsmtp

    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2057)
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1580)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1097)
    at javax.mail.Transport.send0(Transport.java:195)
    at javax.mail.Transport.send(Transport.java:124)
    at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1400)
    ... 26 more

How can I solve this problem?

我怎么解决这个问题?

采纳答案by asvni

I think you have to specify that you are sending a TLS email before sending the email.

我认为您必须在发送电子邮件之前指定您要发送 TLS 电子邮件。

mail.setTLS(true);

I am not 100% sure but I think it might solve the problem.

我不是 100% 确定,但我认为它可能会解决问题。

Also for more info you can refer to this user guide: https://commons.apache.org/proper/commons-email/userguide.html

此外,有关更多信息,您可以参考本用户指南:https: //commons.apache.org/proper/commons-email/userguide.html

回答by Justin Lee

At the very first I had the same problem that you've run into.

一开始我遇到了你遇到的同样问题。

And when I changed smtp.STARTTLS.enable=trueto smtp.starttls.enable=true, everything works.

当我更改smtp.STARTTLS.enable=true为 时smtp.starttls.enable=true,一切正常。

回答by Justin Lee

I found that the working solution to this problem.

我发现这个问题的有效解决方案。

Here's the code:

这是代码:

Properties properties = new Properties();
properties.put("mail.smtp.host", mailAccount.getMailHost());
properties.put("mail.smtp.port", mailAccount.getPort());
properties.put("mail.smtp.auth", mailAccount.isAuth());
properties.put("mail.smtp.starttls.enable",mailAccount.isStartTls());