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; } }
第一種方法是調用了系統的mail app,你首先要配置系統的mail app,可是這個方法的最大問題是,你運行這個方法後android
他並不會默認的發送郵件,而是彈出mail的app界面,你須要手動的點擊發送,如圖apache
第二種,是調用了apache的common庫,在pc上能夠正常運行,可是在android虛擬機中會報錯java.lang.NoClassDefFoundError: javax.activation.DataHandler error編程
javax包沒法找到,我看了下這個問題仍是比較廣泛的,你們廣泛表示虛擬機是被閹割的版本,javax好像存在不全,這個實際上就沒法運行安全
第三種,實際是javamail有人作了移植,專門爲android作了開發,這下就比較好了,網上的demo代碼也比較到位,只有一個問題,就是要本身添加一個mail.java,並且對stmp要手動添加。服務器
其實理論上還應該有一種,本身實現smtp服務器,全程採用socket編程,直接與目標服務器交流,這個win下面我寫過,可是android下就算了,並且長期來說面臨着smtp服務器之後會被進行方向查詢,以提升安全性。app