SMTPSendFailedException

SMTPSendFailedException

本文介绍了com.sun.mail.smtp.SMTPSendFailedException:530 5.7.0必须首先发出STARTTLS命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我的代码

        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();

   }

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

但是我要例外了

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$1$$anonfun$applyOrElse$11$$anonfun$apply$11.apply(routes_routing.scala:185)
    at Routes$$anonfun$routes$1$$anonfun$applyOrElse$11$$anonfun$apply$11.apply(routes_routing.scala:185)
    at play.core.Router$HandlerInvoker$$anon$7$$anon$2.invocation(Router.scala:183)
    at play.core.Router$Routes$$anon$1.invocation(Router.scala:377)
    at play.core.j.JavaAction$$anon$1.call(JavaAction.scala:56)
    at play.db.jpa.TransactionalAction$1.apply(TransactionalAction.java:20)
    at play.db.jpa.TransactionalAction$1.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$3.apply(JavaAction.scala:91)
    at play.core.j.JavaAction$$anon$3.apply(JavaAction.scala:90)
    at play.core.j.FPromiseHelper$$anonfun$flatMap$1.apply(FPromiseHelper.scala:82)
    at play.core.j.FPromiseHelper$$anonfun$flatMap$1.apply(FPromiseHelper.scala:82)
    at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:278)
    at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:274)
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:29)
    at play.core.j.HttpExecutionContext$$anon$2.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

我该如何解决这个问题?

解决方案

我认为您必须先指定要发送TLS电子邮件,然后再发送电子邮件.

mail.setTLS(true);

我不确定100%,但是我认为它可以解决问题.

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

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

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

# 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$1$$anonfun$applyOrElse$11$$anonfun$apply$11.apply(routes_routing.scala:185)
    at Routes$$anonfun$routes$1$$anonfun$applyOrElse$11$$anonfun$apply$11.apply(routes_routing.scala:185)
    at play.core.Router$HandlerInvoker$$anon$7$$anon$2.invocation(Router.scala:183)
    at play.core.Router$Routes$$anon$1.invocation(Router.scala:377)
    at play.core.j.JavaAction$$anon$1.call(JavaAction.scala:56)
    at play.db.jpa.TransactionalAction$1.apply(TransactionalAction.java:20)
    at play.db.jpa.TransactionalAction$1.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$3.apply(JavaAction.scala:91)
    at play.core.j.JavaAction$$anon$3.apply(JavaAction.scala:90)
    at play.core.j.FPromiseHelper$$anonfun$flatMap$1.apply(FPromiseHelper.scala:82)
    at play.core.j.FPromiseHelper$$anonfun$flatMap$1.apply(FPromiseHelper.scala:82)
    at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:278)
    at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:274)
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:29)
    at play.core.j.HttpExecutionContext$$anon$2.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?

解决方案

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

mail.setTLS(true);

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

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

这篇关于com.sun.mail.smtp.SMTPSendFailedException:530 5.7.0必须首先发出STARTTLS命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 05:38