0

Google OAuth 2.0 を使用して、Gmail を使用しているユーザーに代わってメールを送信しています。SMTP プロトコルを使用します。これが私のコードです:

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

当社のソフトウェアは、Google App Engine プラットフォームでホストされています。上記の方法を使用して送信された電子メールには、値が「base64」の Content-Transfer-Encoding ヘッダーが常に含まれています。

gmail.com を使用して送信される通常のメールは、このようにエンコードされません。回避策はありますか?それとも、これは常に期待されていることですか? すべてのメール サービス プロバイダー (プライベート サーバーを含む) がサポートしていますか? 私はこれについてほとんど知りません。ありがとう。

4

1 に答える 1

1

まず、JavaMail 1.5.2に組み込まれている OAuth2 サポートを使用して、OAuth2 の使用を簡素化できます。

認証方式は、JavaMail が選択する Content-Transfer-Encoding には影響しません。メッセージの実際の内容に完全に基づいています。コンテンツに多くの非 ASCII 文字が含まれている場合、base64 が使用されます。

于 2014-12-08T20:41:17.207 に答える