JavaMail在Android 4中不起作用,但它适用于Android 5

时间:2015-10-17 04:55:06

标签: android javamail

我有以下简单的代码来缩小问题范围:

public void sendEmail() throws AddressException, MessagingException {
    String host = "smtp.gmail.com";
    String address = "from@gmail.com";

    final String from = "from@gmail.com";
    final String pass = "pass";
    String to = "to@gmail.com";

    Multipart multiPart;
    String finalString = "end";

    Properties props = System.getProperties();
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.user", address);
    props.put("mail.smtp.password", pass);
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.auth", "true");
    props.put("mail.debug", "true");

    Log.i("Check", "done pops");

    Session session = Session.getInstance(props, new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(from, pass);
        }
    });

    DataHandler handler = new DataHandler(new ByteArrayDataSource(
            finalString.getBytes(), "text/plain"));
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));
    message.setDataHandler(handler);
    Log.i("Check", "done sessions");

    multiPart = new MimeMultipart();

    InternetAddress toAddress;
    toAddress = new InternetAddress(to);
    message.addRecipient(Message.RecipientType.TO, toAddress);
    Log.i("Check", "added recipient");
    message.setSubject("Send Auto-Mail");
    message.setContent(multiPart);
    message.setText("Test");

    Log.i("check", "transport");
    Transport transport = session.getTransport("smtp");
    Log.i("check", "connecting");
    transport.connect(host, address, pass);
    Log.i("check", "wana send");
    transport.sendMessage(message, message.getAllRecipients());
    transport.close();

    Log.i("check", "sent");

}

使用以下 AndroidManifest 权限从Android应用发送电子邮件:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

对于Android 5,它的效果非常好,如下所示的调试日志:

220 smtp.gmail.com ESMTP n94sm9230173qgd.33 - gsmtp
DEBUG SMTP: connected to host "smtp.gmail.com", port: 587
EHLO localhost
250-smtp.gmail.com at your service, [190.227.217.252]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
STARTTLS
220 2.0.0 Ready to start TLS
EHLO localhost
250-smtp.gmail.com at your service, [190.227.217.252]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 VXNlcm5hbWU6
dG5zLnVrLnBwcEBnbWFpbC5jb20=
334 UGFzc3dvcmQ6
dG5zcHBwMjAxNA==
235 2.7.0 Accepted
wana send
DEBUG SMTP: use8bit false
MAIL FROM:<from@gmail.com>
250 2.1.0 OK n94sm9230173qgd.33 - gsmtp
RCPT TO:<to@gmail.com>
250 2.1.5 OK n94sm9230173qgd.33 - gsmtp
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   from@gmail.com
DATA
354  Go ahead n94sm9230173qgd.33 - gsmtp
From: from@gmail.com
To: from@gmail.com
Message-ID: <221217786.1.1445054341639.JavaMail.root@localhost>
Subject: Send Auto-Mail
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Test
.
250 2.0.0 OK 1445054498 n94sm9230173qgd.33 - gsmtp
QUIT
221 2.0.0 closing connection n94sm9230173qgd.33 - gsmtp
sent

但如果我在Android 4中运行相同的代码,则会出现以下错误。

javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbsjU...

注意:我确实在我的Gmail帐户中启用了&#34;允许安全性较低的应用:ON&#34;

注意:我还有最新的JavaMail jar文件。版本:1.5.5-20150902.211410-1从here下载

我已经挖掘了一段时间,但我无法找到任何解决方案。不确定是否有人遇到这个问题并成功解决了。

由于

0 个答案:

没有答案