android中发送邮件我大概发现了3种,代码如下

packagesrc.icetest;importorg.apache.commons.mail.EmailException;importorg.apache.commons.mail.HtmlEmail;importandroid.app.Activity;importandroid.content.Intent;importandroid.os.Bundle;importandroid.util.Log;publicclassIcetestActivityextendsActivity{/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);Log.i("IcetestActivity","starticeteststep1");//sendMailIntent();//sendMailByApache();sendMailByJavaMail();}//youneedconfigthemailappinyourandroidmoblefirst,andthemailwillsendbythemailapp.andthereareonebigbug://youcan'tsendthemailSilentlyandyouneedtoclickthesendbuttonpublicintsendMailByIntent(){String[]reciver=newString[]{"181712000@qq.com"};String[]mySbuject=newString[]{"test"};StringmyCc="cc";Stringmybody="测试EmailIntent";IntentmyIntent=newIntent(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,"mailtest"));return1;}/*thismethodcan'tbeusedinandroidmobilesuccessful,butitcanrunnormallyinPC.Becauseitwillcausethejava.lang.NoClassDefFoundError:javax.activation.DataHandlererrorMaybetherearesomewaytosoloveit......therearealwaysjavaxpackagenotfoundinandroidvirtualmobile.Bytheway,themethoduseApachemailjar*/publicintsendMailByApache(){try{HtmlEmailemail=newHtmlEmail();//这里是发送服务器的名字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","00000");email.setSubject("测试EmailApache");//要发送的信息email.setMsg("测试EmailApache");//发送email.send();}catch(EmailExceptione){//TODOAuto-generatedcatchblockLog.i("IcetestActivity",e.getMessage());}return1;}/**thismethodusejavamailforandroid,itisagoodjar,*youcanseethedemoinhttp://www.jondev.net/articles/Sending_Emails_without_User_Intervention_(no_Intents)_in_Android*andyoualsoneedthreejars,whichIofferedinattachement***/publicintsendMailByJavaMail(){Mailm=newMail("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("ThisisanemailsentusingicetestfromanAndroiddevice");m.setBody("Emailbody.testbyJavaMail");try{//m.addAttachment("/sdcard/filelocation");if(m.send()){Log.i("IcetestActivity","Emailwassentsuccessfully.");}else{Log.i("IcetestActivity","Emailwassentfailed.");}}catch(Exceptione){//Toast.makeText(MailApp.this,//"Therewasaproblemsendingtheemail.",//Toast.LENGTH_LONG).show();Log.e("MailApp","Couldnotsendemail",e);}return1;}}

第一种方法是调用了系统的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服务器以后会被进行方向查询,以提高安全性。


更多相关文章

  1. android 自动化测试 monkey
  2. Android(安卓)之通知Notification应用
  3. android 打电话程序的实现
  4. android下tcp之client测试
  5. Android向Http服务器发送Http请求异常-UnknowHostException
  6. android中ActionBar +Fragment
  7. android使用Intent操作拨打号码发送短信
  8. Android(安卓)Socket 编程
  9. Android(安卓)VTS学习

随机推荐

  1. Android向通讯录添加联系人的一般方法
  2. Android(安卓)对话框【Dialog】去除白色
  3. 【Android】Android实现截取当前屏幕图片
  4. Android中日志信息的打印方式
  5. Android(安卓)语音识别示例
  6. Android(安卓)开发中最常用的 GitHub 优
  7. Android(安卓)Bundle类
  8. Android在TextView中设置图片显示问题
  9. android获取内置和外置SD卡路径
  10. Android:interpolator用法