使用JAVA和Play Framework 2.2.2发送电子邮件

时间:2014-03-31 15:13:54

标签: java email playframework smtp

我正在使用Play Framework使用我的Gmail帐户(smtp)发送电子邮件,但我收到了一些错误。我尝试了很多东西,但我不知道如何纠正它们。这是错误日志:

org.apache.commons.mail.EmailException: Sending the email to the following serve
r failed : smtp.gmail.com:465
        at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1242)
        at org.apache.commons.mail.Email.send(Email.java:1267)
        at logic.Validation.validateRegistration(Validation.java:89)
        at controllers.Application.registration(Application.java:46)
        at Routes$$anonfun$routes$1$$anonfun$applyOrElse$2$$anonfun$apply$2.appl
y(routes_routing.scala:65)
        at Routes$$anonfun$routes$1$$anonfun$applyOrElse$2$$anonfun$apply$2.appl
y(routes_routing.scala:65)
        at play.core.Router$HandlerInvoker$$anon$7$$anon$2.invocation(Router.sca
la: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.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.sc
ala:82)
        at play.core.j.FPromiseHelper$$anonfun$flatMap$1.apply(FPromiseHelper.sc
ala:82)
        at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:251)
        at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:249)
        at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
        at play.core.j.HttpExecutionContext$$anon$2.run(HttpExecutionContext.sca
la:37)
        at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:42)
        at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(Abst
ractDispatcher.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:19
79)
        at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThre
ad.java:107)
Caused by: javax.mail.AuthenticationFailedException
        at javax.mail.Service.connect(Service.java:319)
        at javax.mail.Service.connect(Service.java:169)
        at javax.mail.Service.connect(Service.java:118)
        at javax.mail.Transport.send0(Transport.java:188)
        at javax.mail.Transport.send(Transport.java:118)
        at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1232)
        ... 22 more
org.apache.commons.mail.EmailException: Sending the email to the following serve
r failed : smtp.gmail.com:465
        at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1242)
        at org.apache.commons.mail.Email.send(Email.java:1267)
        at logic.Validation.validateRegistration(Validation.java:88)
        at controllers.Application.registration(Application.java:46)
        at Routes$$anonfun$routes$1$$anonfun$applyOrElse$2$$anonfun$apply$2.appl
y(routes_routing.scala:65)
        at Routes$$anonfun$routes$1$$anonfun$applyOrElse$2$$anonfun$apply$2.appl
y(routes_routing.scala:65)
        at play.core.Router$HandlerInvoker$$anon$7$$anon$2.invocation(Router.sca
la: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.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.sc
ala:82)
        at play.core.j.FPromiseHelper$$anonfun$flatMap$1.apply(FPromiseHelper.sc
ala:82)
        at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:251)
        at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:249)
        at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
        at play.core.j.HttpExecutionContext$$anon$2.run(HttpExecutionContext.sca
la:37)
        at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:42)
        at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(Abst
ractDispatcher.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:19
79)
        at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThre
ad.java:107)
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.g
mail.com, port: 465, response: -1
        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1379)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:41
2)
        at javax.mail.Service.connect(Service.java:310)
        at javax.mail.Service.connect(Service.java:169)
        at javax.mail.Service.connect(Service.java:118)
        at javax.mail.Transport.send0(Transport.java:188)
        at javax.mail.Transport.send(Transport.java:118)
        at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1232)
        ... 22 more

我的代码是:

// Load SMTP configuration
        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" );
        // Render template
        //String body = email.render( created ).body();

    // Prepare email
    Email mail = new SimpleEmail();
    try {
        mail.setFrom("myemail@gmail.com");
        mail.setSubject("BláBlá");
        mail.setMsg("This is the message");
        mail.addTo("emailto@gmail.com");
    } catch (EmailException e) {
        e.printStackTrace();
    }


    // Application de la configuration SMTP
    mail.setHostName( smtpHost );
    if ( smtpPort != null && smtpPort > 1 && smtpPort < 65536 ) {
        mail.setSmtpPort( smtpPort );
       // mail.setSSL(true);
    }
    if ( ! smtpUser.isEmpty() ) {
        mail.setAuthentication( smtpUser, smtpPassword );
    }

    // And finally
    try {
        mail.send();
    } catch (EmailException e) {
        e.printStackTrace();
    }

我的Application.conf文件有:

    # Email Configurations
    smtp.host="smtp.gmail.com"
    smtp.port=465
    smtp.user="myemail@gmail.com"
    smtp.password="account_pass"

我正在关注此代码: https://groups.google.com/forum/#!topic/play-framework/qvCaTbqdI4Y

希望有人可以帮助我..谢谢!!

0 个答案:

没有答案