Android簡單發送郵件(可帶附件)

來源:互聯網
上載者:User

項目中遇到了一個發送郵件的功能,在網上查了很多,資料也很多。這裡就不一一介紹了,只是寫出我使用的方案(最簡單的)


Intent email = new Intent(android.content.Intent.ACTION_SEND);//郵件發送類型:無附件,純文字email.setType("plain/text");//郵件接收者(數組,可以是多位接收者)String[] emailReciver = new String[]{"123@qq.com","456@163.com"};String  emailTitle = "標題";String emailContent = "內容";//設定郵件地址 email.putExtra(android.content.Intent.EXTRA_EMAIL, emailReciver);//設定郵件標題email.putExtra(android.content.Intent.EXTRA_SUBJECT, emailTitle);//設定發送的內容email.putExtra(android.content.Intent.EXTRA_TEXT, emailContent); //調用系統的郵件系統startActivity(Intent.createChooser(email, "請選擇郵件發送軟體"));

代碼很簡單,直接複製,然後修改就可以用(本人親自試過了)。就像我說的,這是最最簡單的方式了。它主要是通過調用系統的mail發送郵件。他的好處就是簡單,方便。如果你安裝了QQ郵箱、gmail郵箱、163郵箱的android用戶端,那麼在發送時,會提示你選擇使用哪一個。如果你沒有安裝上述郵件用戶端,那麼,就調用系統的郵件用戶端了。


下面在寫一個可以發送附件的代碼,當然也是這種最簡單的方式


Intent email = new Intent(android.content.Intent.ACTION_SEND);// 附件   File file = new File(Environment.getExternalStorageDirectory().getPath()+ File.separator + "simplenote"+ File.separator+"note.xml");//郵件發送類型:帶附件的郵件email.setType("application/octet-stream"); //郵件接收者(數組,可以是多位接收者)String[] emailReciver = new String[]{"123@qq.com","456@163.com"};String  emailTitle = "標題";String emailContent = "內容";//設定郵件地址email.putExtra(android.content.Intent.EXTRA_EMAIL, emailReciver);//設定郵件標題 email.putExtra(android.content.Intent.EXTRA_SUBJECT, emailTitle);//設定發送的內容email.putExtra(android.content.Intent.EXTRA_TEXT, emailContent);//附件email.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); //調用系統的郵件系統startActivity(Intent.createChooser(email, "請選擇郵件發送軟體"));

通過兩段代碼的比較,就明白了。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.