Help us understand the problem. What is going on with this article?

Playframework2.3.x play-plugins-mailerでGmailを使うときにハマったこと

More than 5 years have passed since last update.

Playframework2.3.1でメール送信を実装。
play-plugins-mailerでGmailを使う手順です。

①build.sbtに下記を追加

build.sbt
"com.typesafe.play.plugins" %% "play-plugins-mailer" % "2.3.1"

②application.confに下記を追加

application.conf
smtp.host=smtp.gmail.com
smtp.port=465
smtp.ssl=yes
smtp.tls=yes
smtp.user="xxxxx@gmail.com"
smtp.password=xxxxxxx

※smtp.userは送信元にしたいgmailアドレスを設定。
passwordはそのgmailアカウントのパスワードを設定。

③メールを送る。

Mailer.java
// メール送信
        MailerAPI mail = play.Play.application().plugin(MailerPlugin.class).email();
        mail.setSubject("件名");
        mail.setRecipient("xxxxx@gmail.com"); // 送信相手のアドレス
        mail.setFrom("xxxxx@gmail.com"); // 送信元のアドレス
        mail.send(body);

その結果、、、、

play.api.Application$$anon$1: Execution exception[[EmailException: Sending the email to the following server failed : smtp.gmail.com:465]]
    at play.api.Application$class.handleError(Application.scala:296) ~[play_2.11-2.3.6.jar:2.3.6]
    at play.api.DefaultApplication.handleError(Application.scala:402) [play_2.11-2.3.6.jar:2.3.6]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.6.jar:2.3.6]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.6.jar:2.3.6]
    at scala.Option.map(Option.scala:145) [scala-library-2.11.1.jar:na]
Caused by: org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.com:465
    at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1410) ~[commons-email-1.3.3.jar:1.3.3]
    at org.apache.commons.mail.Email.send(Email.java:1437) ~[commons-email-1.3.3.jar:1.3.3]
    at com.typesafe.plugin.CommonsMailer.send(MailerPlugin.scala:398) ~[play-plugins-mailer_2.11-2.3.1.jar:2.3.1]
    at com.typesafe.plugin.MailerBuilder$class.send(MailerPlugin.scala:309) ~[play-plugins-mailer_2.11-2.3.1.jar:2.3.1]
    at com.typesafe.plugin.CommonsMailer.send(MailerPlugin.scala:330) ~[play-plugins-mailer_2.11-2.3.1.jar:2.3.1]
Caused by: javax.mail.AuthenticationFailedException:

上記のようなエラーが出た場合、

下記URLにapplication.confに設定したアカウントでアクセスし、
https://www.google.com/settings/security/lesssecureapps

スクリーンショット 2014-11-08 午後4.28.12.png

[安全性の低いアプリのアクセス]を

「無効にする」→「有効にする」に変更。

これで解決すると思います。

正しい解決法なのかはわかりませんが、
ひとまずこれで動くようになります。

yuinchirn
《バックエンド > フロントエンド > インフラ > iOS》 Scala, Java, PlayFramework, AWS, React, Redux, Swift
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした