Android中使用apache commons-net发送后台邮件的方法

发布时间:2019-06-12 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Android中使用apache commons-net发送后台邮件的方法脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

上一篇文章中我介绍了使用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();

脚本宝典总结

以上是脚本宝典为你收集整理的Android中使用apache commons-net发送后台邮件的方法全部内容,希望文章能够帮你解决Android中使用apache commons-net发送后台邮件的方法所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。