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. */@Overridepublic 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 buttonpublic 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 1;}   /*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 errorMay 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", "00000");email.setSubject("测试Email Apache");// 要发送的信息email.setMsg("测试Email Apache");// 发送email.send();} catch (EmailException e) {// TODO Auto-generated catch blockLog.i("IcetestActivity", e.getMessage());}return 1;}/* * 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 1;}}

第一种方法是调用了系统的mail app,你首先要配置系统的mail app,但是这个方法的最大问题是,你运行这个方法后

他并不会默认的发送邮件,而是弹出mail的app界面,你需要手动的点击发送,如图

android邮件发送几种方式

第二种,是调用了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单元测试和日志输出
  2. google Android 认证测试
  3. android 7.0 系统关闭彩信过CTA测试的方法
  4. android单元测试 配置注意
  5. Android 4.0.3 CTS 测试
  6. android之单元测试――上
  7. Android下的单元测试
  8. android访问服务器端上传及服务器端接收 .
  9. android CTS测试

随机推荐

  1. 移植android ndk c++各种兼容性问题
  2. 【059】分享我的 Android(安卓)应用
  3. Android(安卓)定时器真机测试
  4. 25个你应该阅读的Android网站
  5. IPC与Binder,Binder线程池,AIDL demo - And
  6. 关于android WebViewClient和WebChromeCl
  7. android 模拟器中启用相机API支持
  8. Android(安卓)Menu 用法
  9. Android自动化测试初探(二): Hierarchyview
  10. Android(安卓)10 创建文件失败