Android中使用apache commons-net发送后台邮件的方法
来源:互联网 发布:pc蛋蛋幸运28算法公式 编辑:程序博客网 时间:2024/06/03 23:01
上一篇文章中我介绍了使用Javamail-android在android中发送后台邮件的方法,但是,这个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=%s\1auth=Bearer %s\1\1", 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();
0 0
- Android中使用apache commons-net发送后台邮件的方法
- 使用Apache Commons-email组件发送邮件
- 使用org.apache.commons.mail 发送邮件
- 使用Apache Commons-email组件发送邮件
- 使用org.apache.commons.mail 发送邮件
- 使用org.apache.commons.mail发送邮件
- 使用apache commons email 中HtmlEmail类的setHtmlMsg(body)与 addPart(body, "text/html;charset=utf-8")方法发送邮件区别
- 在Asp.Net中使用SmtpMail发送邮件的方法
- 在Asp.Net中使用SmtpMail发送邮件的方法
- 在Asp.Net中使用SmtpMail发送邮件的方法
- 在Asp.Net中使用SmtpMail发送邮件的方法
- 在ASP.NET中使用SMTPMail发送邮件的方法
- Apache-commons-email发送邮件
- Apache Commons Email邮件发送
- java项目中添加邮件发送功能(使用apache commons mail开源项目)
- 使用apache.commons.mail包发送邮件,例子
- 使用org.apache.commons.mail 发送邮件(转)
- 使用org.apache.commons.mail 发送邮件(转)
- 织梦DEDECMS时间标签调用格式大全
- AJAX - onreadystatechange 事件
- Best Time to Buy and Sell Stock III
- mysql root密码找回
- adb配置
- Android中使用apache commons-net发送后台邮件的方法
- Android下的布局
- Java中文件的上传与下载
- angularjs directive semantic-ui pagination
- angularjs route 中的用户身份认证
- angularjs 利用 service + $broadcast 在多个controllers 之间共享数据
- 游戏服务器端编程书籍
- Java中ASC码与字符互相转换
- DedeCMS添加字段提示Invalid utf8 character string错误的解决办法