android邮件发送几种方式
android中发送邮件我大概发现了3种,代码如下
package src.icetest; import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log; public class IcetestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("IcetestActivity", "start ice test step 1");
// sendMailIntent();
//sendMailByApache();
sendMailByJavaMail();
} // you need config the mail app in your android moble first,and the mail will send by the mail app. and there are one big bug:
//you can't send the mail Silently and you need to click the send button
public int sendMailByIntent() {
String[] reciver = new String[] { "181712000@qq.com" };
String[] mySbuject = new String[] { "test" };
String myCc = "cc";
String mybody = "测试Email Intent";
Intent myIntent = new Intent(android.content.Intent.ACTION_SEND);
myIntent.setType("plain/text");
myIntent.putExtra(android.content.Intent.EXTRA_EMAIL, reciver);
myIntent.putExtra(android.content.Intent.EXTRA_CC, myCc);
myIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, mySbuject);
myIntent.putExtra(android.content.Intent.EXTRA_TEXT, mybody);
startActivity(Intent.createChooser(myIntent, "mail test")); return ; }
/*this method can't be used in android mobile successful,but it can run normally in PC.
Because it will cause the java.lang.NoClassDefFoundError: javax.activation.DataHandler error
May be there are some way to solove it ......there are always javax package not found in android virtual mobile.
By the way ,the method use Apache mail jar
*/
public int sendMailByApache() { try {
HtmlEmail email = new HtmlEmail();
// 这里是发送服务器的名字
email.setHostName("smtp.gmail.com");
// 编码集的设置
email.setTLS(true);
email.setSSL(true); email.setCharset("gbk");
// 收件人的邮箱
email.addTo("181712000@qq.com");
// 发送人的邮箱
email.setFrom("wcf0000@gmail.com");
// 如果需要认证信息的话,设置认证:用户名-密码。分别为发件人在邮件服务器上的注册名称和密码
email.setAuthentication("wcf1000", "");
email.setSubject("测试Email Apache");
// 要发送的信息
email.setMsg("测试Email Apache");
// 发送
email.send();
} catch (EmailException e) {
// TODO Auto-generated catch block
Log.i("IcetestActivity", e.getMessage());
} return ;
}
/*
* this method use javamail for android ,it is a good jar,
* you can see the demo in http://www.jondev.net/articles/Sending_Emails_without_User_Intervention_(no_Intents)_in_Android
* and you also need three jars ,which I offered in attachement
*
* */
public int sendMailByJavaMail() {
Mail m = new Mail("wcfXXXX@gmail.com", "XXXXX");
m.set_debuggable(true);
String[] toArr = {"18170000@qq.com"};
m.set_to(toArr);
m.set_from("18170000@qq.com");
m.set_subject("This is an email sent using icetest from an Android device");
m.setBody("Email body. test by Java Mail");
try {
//m.addAttachment("/sdcard/filelocation");
if(m.send()) {
Log.i("IcetestActivity","Email was sent successfully."); } else {
Log.i("IcetestActivity","Email was sent failed.");
}
} catch (Exception e) {
// Toast.makeText(MailApp.this,
// "There was a problem sending the email.",
// Toast.LENGTH_LONG).show();
Log.e("MailApp", "Could not send email", e);
} return ;
}
}
第一种方法是调用了系统的mail app,你首先要配置系统的mail app,但是这个方法的最大问题是,你运行这个方法后
他并不会默认的发送邮件,而是弹出mail的app界面,你需要手动的点击发送,如图
第二种,是调用了apache的common库,在pc上可以正常运行,但是在android虚拟机中会报错java.lang.NoClassDefFoundError: javax.activation.DataHandler error
javax包无法找到,我看了下这个问题还是比较普遍的,大家普遍表示虚拟机是被阉割的版本,javax好像存在不全,这个实际上就无法运行
第三种,实际是javamail有人做了移植,专门为android做了开发,这下就比较好了,网上的demo代码也比较到位,只有一个问题,就是要自己添加一个mail.java,而且对stmp要手动添加。
其实理论上还应该有一种,自己实现smtp服务器,全程采用socket编程,直接与目标服务器交流,这个win下面我写过,但是android下就算了,而且长期来讲面临着smtp服务器以后会被进行方向查询,以提高安全性。
http://wcf1987.iteye.com/blog/1292509
android邮件发送几种方式的更多相关文章
- Android 数据存储五种方式
1.概述 Android提供了5种方式来让用户保存持久化应用程序数据.根据自己的需求来做选择,比如数据是否是应用程序私有的,是否能被其他程序访问,需要多少数据存储空间等,分别是: ① 使用Shared ...
- android 定位的四种方式
[原文] 开发中对于地图及地理位置的定位是我们经常要用地,地图功能的使用使得我们应用功能更加完善,下面总结了一下网络中现有对于介绍android定位的4种方式,希望对大家有帮助: android 定 ...
- android 定位的几种方式介绍
[地理位置] android 定位的几种方式介绍 开发中对于地图及地理位置的定位是我们经常要用地,地图功能的使用使得我们应用功能更加完善,下面 www.androidkaifa.com 总结了一下网络 ...
- Android 反射-换一种方式编程
Android 反射-换一种方式编程 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/59109933 本文出自[赵彦军的博客] 上一 ...
- 【转】在Android Studio中下载Android SDK的两种方式(Android Studio3.0、windows)
在Android Studio中下载Android SDK的两种方式(Android Studio3.0.windows) 方式一.设置HTTP Proxy1. 打开Settings2. 点击HTTP ...
- Android学习—下载Android SDK的两种方式
在Android Studio中下载Android SDK的两种方式 Android studio下载地址:http://www.android-studio.org/ 方式一.设置HTTP Prox ...
- Android邮件发送详解
转载:http://flysnow.iteye.com/blog/1128354 Android中我为什么发不了邮件???我手机里明明有邮件客户端的,可我为什么不能调用它发送邮件???相信这是很多人会 ...
- Android数据存储五种方式总结
本文介绍Android平台进行数据存储的五大方式,分别如下: 1 使用SharedPreferences存储数据 2 文件存储数据 3 SQLite数据库存储数据 4 使用Cont ...
- 将Eclipse代码导入到Android Studio的两种方式
转: http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0104/2259.html 说到使用Android Studio,除了新建 ...
随机推荐
- wordpress教程之get_posts()
get_posts 介绍 June 3rd 2012 评论(16) get_posts 函数,简单的来讲是 get_post 的复数新形势,但因为是文章多篇提取,所以使用方法上却略有不同,支持众多参数 ...
- 深入理解JS原型链与继承
我 觉得阅读精彩的文章是提升自己最快的方法,而且我发现人在不同阶段看待同样的东西都会有不同的收获,有一天你看到一本好书或者好的文章,请记得收藏起来, 隔断时间再去看看,我想应该会有很大的收获.其实今天 ...
- Regex类
一.属性 CacheSize 获取或设置已编译的正则表达式的当前静态缓存中的最大项数. 默认是15个,最近使用的15个会存在缓存中,避免重新创建.当有第16个进来会替换掉第 1个.保持15个.Opt ...
- winform datagridview 添加行号。
private void dataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { private ...
- 二分求解 三角形 stl的应用 涉及范围的二分查找可以先求上界再算下界,结果即上界减下界
二分 Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Description You ...
- Notepad++背景颜色设置
经常试用notepad++看代码,白色的背景连续看的时间长了眼睛很容变花,所以找了相关的设置选项,分享给大家 具体设置步骤如下: 然后如下设置 这样前景色背景色已经发生改变了哟,下面再修改下选中行的背 ...
- eclipse中如何创建maven项目
1.在eclipse中,file-->new-->maven project,勾选Create a simple project,点击next. 2.添加项目信息,点击finish.(pa ...
- [置顶] ID3算法的python实现
这篇文章的内容接着http://blog.csdn.net/xueyunf/article/details/9214727的内容,所有还有部分函数在http://blog.csdn.net/xueyu ...
- 点击返回键退出popupwindow的方法
点击返回键退出popupwindow mPopupWindow.setFocusable(true); 这句非常重要,对背景不会有影响 mPopupWindow.setBackgroundDrawab ...
- C#主要字典集合性能对比[转]
A post I made a couple days ago about the side-effect of concurrency (the concurrent collections in ...