上一篇文章中我介绍了使用Javamail-androidandroid中发送后台邮件的方法,但是,这个Javamail-android并不是一个靠谱的开源组织发布的,而且是修改过的Javaee,总觉得用起来别扭,所以我就一直搜寻一种更靠谱的解决方案。总算功夫不负有心人,看到了这两篇文章:Sending email without user interaction in Android,Sending a mail in Java (and Android) with Apache Commons Net SMTP : STARTTLS, SSL,在这两篇文章中介绍了使用 apache commons-net 来发送邮件的方法。这个就很对路了,apache出品,正规大厂,质量信得过产品,呵呵。

核心代码片段:

// 建立发送邮件任务
new AsyncTask<String, Integer, Boolean>() {

        @Override
        protected Boolean doInBackground(String... params) {

            //使用commons-net中的AuthenticatingSMTPClient
            AuthenticatingSMTPClient client = new AuthenticatingSMTPClient("TLS", true);
            try {
                client.connect("smtp.gmail.com", 465);
                client.ehlo("localhost"); //需要ehlo一下
                client.sendCommand("AUTH XOAUTH2 " + Base64.encodeBase64URLSafeString(
                        String.format("user=%s1auth=Bearer %s11", me.name, oauthToken).getBytes())
                        ); //发送Google的XOAUTH2命令
                client.setSender(me.name);

                //处理多个收件人
                String[] recipientsArray = null;
                if(recipients.toString().contains(",")) {
                    recipientsArray = recipients.toString().trim().split(",");
                    for( String recipient : recipientsArray ) {
                        client.addRecipient(recipient);
                    }
                }else {
                    recipientsArray = new String[]{recipients.toString()};
                    client.addRecipient(recipients.toString());
                }

                //处理邮件主题与正文
                Writer writer = client.sendMessageData();
                if(null != writer) {

                    //使用apache mime4j中的EncoderUtil来处理中文乱码的问题
                    String encodedSubject = EncoderUtil.encodeIfNecessary(subject, Usage.TEXT_TOKEN, 0); //使用apache mime4j中的EncoderUtil来编码邮件主题
                    String encodedBody = EncoderUtil.encodeB(body.getBytes()); //使用apache mime4j中的EncoderUtil来编码邮件正文

                    Log.i(SMSBroadcastReceiver.class.getName(), "encodedSubject: " + encodedSubject);
                    Log.i(SMSBroadcastReceiver.class.getName(), "encodedBody: " + encodedBody);

                    SimpleSMTPHeader header = new SimpleSMTPHeader(me.name, recipientsArray[0], encodedSubject);
                    header.addHeaderField("Content-Type", "text/plain; charset=UTF-8");
                    header.addHeaderField("Content-Transfer-Encoding", "base64");
                    for (int i = 0; i < recipientsArray.length; i++) {
                        if(0 == i) continue;
                        header.addCC(recipientsArray[i]);
                    }
                    writer.write(header.toString());
                    writer.write(encodedBody);
                    writer.close();
                }
                return true;
            } catch (Exception e) {
                return false;
            } finally {
                try {
                    client.disconnect();
                } catch (IOException e) {
                }
            }
        }

    }.execute();

本文固定链接: http://www.js-code.com/android/android_56326.html