oAuth2 メカニズムを使用して Google SMTP サーバーに接続してメールを送信しようとしています。
public static Transport connectToSmtp(String smtpServer, int port, String email, String authToken, boolean isDebug, MimeMessage mimeMsg)
throws Exception
{
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.required", "true");
props.put("mail.smtp.sasl.enable", "true");
props.put("mail.smtp.sasl.mechanisms", "XOAUTH2");
props.put("mail.imaps.sasl.mechanisms.oauth2.oauthToken", authToken);
Session session = Session.getInstance(props);
session.setDebug(isDebug);
final String emptyPassword = "";
Transport transport = session.getTransport("smtp");
transport.connect(smtpServer, port, email, emptyPassword);
try
{
transport.sendMessage(mimeMsg, mimeMsg.getAllRecipients());
}
catch (Exception e)
{
logger.log(Level.WARNING, "Exception Occured : ", e);
}
return transport;
}
アプリのクライアント ID とシークレット:
ID - 345101*-i6ki*4tk1sms.apps.googleusercontent.com
シークレット - e6NHB*- eZ-r k
上記のコードは、次のエラーをスローしました。
javax.mail.AuthenticationFailedException: 535-5.7.8 ユーザー名とパスワードが受け入れられません。535 5.7.8 で詳細を ご覧ください https://support.google.com/mail/answer/14257 o135sm276925ith.4 - gsmtp
SMTP サーバー: aspmx.l.google.com、ポート: 25
編集:
接続しようとしている Google アプリ アカウントの以下を変更し、上記の例外をクリアしました。
2段階認証なし
安全性の低いアプリを許可する [セキュリティ] -> [基本設定] -> [安全性の低いアプリの設定に移動] -> [ユーザーが安全性の低いアプリへのアクセスを管理できるようにする]
しかし、上記の例外をクリアした後。別の例外があります。
com.sun.mail.smtp.SMTPSendFailedException: 550-5.7.1 リレーの資格が無効です [121. . .2]。Google Apps SMTP リレー サービスに登録した IP アドレスが、このメールの送信元アカウントのドメインと一致しません。Google Apps アカウントに登録されていないドメインまたはエンベロープ送信元が空のドメインからのメールをリレーしようとしている場合は、SMTP AUTH を使用して送信ドメインを識別するか、ドメインの 1 つを提示するようにメール サーバーを構成する必要があります。 HELO または EHLO コマンドの名前。詳細については、 https: //support.google.com/a/answer/6140680#invalidcred f65sm341248ith.1 - gsmtp をご覧ください。
そこで、次の設定を変更しました。
アプリ > Google Apps > Gmail > Gmail の設定 > 詳細設定
SMTP リレー サービス
- 許可された送信者: 任意のアドレス (非推奨)
- 指定した IP アドレスからのメールのみを受け入れる: いいえ
- SMTP 認証が必要: はい
- TLS 暗号化が必要: いいえ
上記の変更を試みた後でも、同じ例外がスローされます。助けてください。