日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android邮件发送几种方式

發布時間:2023/12/20 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android邮件发送几种方式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?android中發送郵件我大概發現了3種,代碼如下

Java代碼??

  • 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?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?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",?"00000");??

  • ????????????email.setSubject("測試Email?Apache");??

  • ????????????//?要發送的信息??

  • ????????????email.setMsg("測試Email?Apache");??

  • ????????????//?發送??

  • ????????????email.send();??

  • ????????}?catch?(EmailException?e)?{??

  • ????????????//?TODO?Auto-generated?catch?block??

  • ????????????Log.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;??

  • ????}??

  • } ?


  • 轉載于:https://blog.51cto.com/1137907860/1680397

    總結

    以上是生活随笔為你收集整理的android邮件发送几种方式的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。