0

Google App Engine のメール サービスは、AppEngine SDK に含まれる JavaMail JAR を使用します。Oracle の JAR を使用しないように具体的に求めています。

しかし、SMTP を使用して電子メールを送信するために Google OAuth 2.0 を使用するには、Oracle の JavaMail JAR が必要です。

これを達成する方法について何か考えはありますか?私は立ち往生しています。当社のソフトウェアの内部機能は、AppEngine メールを使用してチーム内でメールを送信します。クライアントは、メール アドレスに代わってメールを送信するために Google OAuth SMTP を必要とします。

Oracle の JAR を含めた後、通常の AppEngine メールを使用しようとすると、次の例外が発生します。

javax.mail.SendFailedException: Send failure (com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1 (java.net.SocketException: Permission denied: Attempt to access a blocked recipient without permission.))
at javax.mail.Transport.send(Transport.java:163)
at javax.mail.Transport.send(Transport.java:48)
at app.util.EmailUtil.sendMail_AppEngine(EmailUtil.java:948)

更新:OAuthを使用してメールを送信するために使用するコードは次のとおりです

        Properties props = new Properties();
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.starttls.required", "true");
        props.put("mail.smtp.sasl.enable", "false");
        Session session = Session.getInstance(props);
        session.setDebug(false);

        final URLName unusedUrlName = null;
        SMTPTransport transport = new SMTPTransport(session, unusedUrlName);
        transport.connect("smtp.gmail.com", 587, fromAddress, null);

 byte[] response = String.format("user=%s\1auth=Bearer %s\1\1", lFrom, oauthToken).getBytes();
        response = BASE64EncoderStream.encode(response);

        transport.issueCommand("AUTH XOAUTH2 " + new String(response),
                235);

        MimeMessage msg = new MimeMessage( session );

        msg.setFrom( new InternetAddress( fromAddress , fromName ) );
        msg.setRecipients( Message.RecipientType.TO , InternetAddress.parse( toAddress , false ) );

        msg.setSubject( emailSubject );
        MimeBodyPart htmlPart = new MimeBodyPart();
        Multipart multiPart = new MimeMultipart();

        htmlPart.setContent( "<html>email content</html>" , "text/html; charset=UTF-8" );
        multiPart.addBodyPart( htmlPart );
        msg.setContent( multiPart );
        msg.saveChanges();

        transport.sendMessage(msg, msg.getAllRecipients());

誰かがここで私を助けてくれることを願っています。

4

1 に答える 1