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

Java代码

  1. packagesrc.icetest;

  2. importorg.apache.commons.mail.EmailException;

  3. importorg.apache.commons.mail.HtmlEmail;

  4. importandroid.app.Activity;

  5. importandroid.content.Intent;

  6. importandroid.os.Bundle;

  7. importandroid.util.Log;

  8. publicclassIcetestActivityextendsActivity{

  9. /**Calledwhentheactivityisfirstcreated.*/

  10. @Override

  11. publicvoidonCreate(BundlesavedInstanceState){

  12. super.onCreate(savedInstanceState);

  13. setContentView(R.layout.main);

  14. Log.i("IcetestActivity","starticeteststep1");

  15. //sendMailIntent();

  16. //sendMailByApache();

  17. sendMailByJavaMail();

  18. }

  19. //youneedconfigthemailappinyourandroidmoblefirst,andthemailwillsendbythemailapp.andthereareonebigbug:

  20. //youcan'tsendthemailSilentlyandyouneedtoclickthesendbutton

  21. publicintsendMailByIntent(){

  22. String[]reciver=newString[]{"181712000@qq.com"};

  23. String[]mySbuject=newString[]{"test"};

  24. StringmyCc="cc";

  25. Stringmybody="测试EmailIntent";

  26. IntentmyIntent=newIntent(android.content.Intent.ACTION_SEND);

  27. myIntent.setType("plain/text");

  28. myIntent.putExtra(android.content.Intent.EXTRA_EMAIL,reciver);

  29. myIntent.putExtra(android.content.Intent.EXTRA_CC,myCc);

  30. myIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,mySbuject);

  31. myIntent.putExtra(android.content.Intent.EXTRA_TEXT,mybody);

  32. startActivity(Intent.createChooser(myIntent,"mailtest"));

  33. return1;

  34. }

  35. /*thismethodcan'tbeusedinandroidmobilesuccessful,butitcanrunnormallyinPC.

  36. Becauseitwillcausethejava.lang.NoClassDefFoundError:javax.activation.DataHandlererror

  37. Maybetherearesomewaytosoloveit......therearealwaysjavaxpackagenotfoundinandroidvirtualmobile.

  38. Bytheway,themethoduseApachemailjar

  39. */

  40. publicintsendMailByApache(){

  41. try{

  42. HtmlEmailemail=newHtmlEmail();

  43. //这里是发送服务器的名字

  44. email.setHostName("smtp.gmail.com");

  45. //编码集的设置

  46. email.setTLS(true);

  47. email.setSSL(true);

  48. email.setCharset("gbk");

  49. //收件人的邮箱

  50. email.addTo("181712000@qq.com");

  51. //发送人的邮箱

  52. email.setFrom("wcf0000@gmail.com");

  53. //如果需要认证信息的话,设置认证:用户名-密码。分别为发件人在邮件服务器上的注册名称和密码

  54. email.setAuthentication("wcf1000","00000");

  55. email.setSubject("测试EmailApache");

  56. //要发送的信息

  57. email.setMsg("测试EmailApache");

  58. //发送

  59. email.send();

  60. }catch(EmailExceptione){

  61. //TODOAuto-generatedcatchblock

  62. Log.i("IcetestActivity",e.getMessage());

  63. }

  64. return1;

  65. }

  66. /*

  67. *thismethodusejavamailforandroid,itisagoodjar,

  68. *youcanseethedemoinhttp://www.jondev.net/articles/Sending_Emails_without_User_Intervention_(no_Intents)_in_Android

  69. *andyoualsoneedthreejars,whichIofferedinattachement

  70. *

  71. **/

  72. publicintsendMailByJavaMail(){

  73. Mailm=newMail("wcfXXXX@gmail.com","XXXXX");

  74. m.set_debuggable(true);

  75. String[]toArr={"18170000@qq.com"};

  76. m.set_to(toArr);

  77. m.set_from("18170000@qq.com");

  78. m.set_subject("ThisisanemailsentusingicetestfromanAndroiddevice");

  79. m.setBody("Emailbody.testbyJavaMail");

  80. try{

  81. //m.addAttachment("/sdcard/filelocation");

  82. if(m.send()){

  83. Log.i("IcetestActivity","Emailwassentsuccessfully.");

  84. }else{

  85. Log.i("IcetestActivity","Emailwassentfailed.");

  86. }

  87. }catch(Exceptione){

  88. //Toast.makeText(MailApp.this,

  89. //"Therewasaproblemsendingtheemail.",

  90. //Toast.LENGTH_LONG).show();

  91. Log.e("MailApp","Couldnotsendemail",e);

  92. }

  93. return1;

  94. }

  95. }


更多相关文章

  1. Android(安卓)cts all pass 全攻略
  2. Android(安卓)cts all pass 全攻略
  3. Android(安卓)下面的一些命令
  4. Android中的测试小demo
  5. android的测试工具CTS
  6. Android(安卓)cts all pass 全攻略
  7. Android自学笔记-7-Android中的junit
  8. Android上的单元测试
  9. android Instrumentation

随机推荐

  1. Android(安卓)7.0新特性——依然范特西
  2. Android集成百度定位以及导航详解
  3. 深入Android(安卓)【三】 —— 组件入门
  4. 简述Android(安卓)解决65536/64K方法数限
  5. Android(安卓)studio文件编码格式怎么快
  6. android 点击字体,图片背景效果一起变换De
  7. 【安卓笔记】res/raw与assets区别
  8. Android(安卓)和 Webview 如何相互 sayHe
  9. Android之Intent深入
  10. android下的蓝牙A2DP