Android發送郵件到指定郵箱(可帶附件)

第一種方法,調用系統的郵件軟件來發送
衆所周知在Android中調用其他程序進行相關處理,都是使用的Intent。當然,Email也不例外。
在 在Android中,調用Email有三種類型的Intent:
Intent.ACTION_SENDTO 無附件的發送

Intent.ACTION_SEND 帶附件的發送

Intent.ACTION_SEND_MULTIPLE 帶有多附件的發

 1.使用SENDTO發送
Intent data=new Intent(Intent.ACTION_SENDTO);  
 data.setData(Uri.parse("mailto:[email protected]"));  
 data.putExtra(Intent.EXTRA_SUBJECT, "這是標題");  
 data.putExtra(Intent.EXTRA_TEXT, "這是內容");  
 startActivity(data); 

Android 客戶端發送文件到指定郵箱

第一、只發送純文本數據到指定郵箱

Intent email = new Intent(android.content.Intent.ACTION_SEND);    
//郵件發送類型:無附件,純文本    
email.setType("plain/text");    
//郵件接收者(數組,可以是多位接收者)    
String[] emailReciver = new String[]{"[email protected]","[email protected]"};    
    
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[]{"[email protected]","[email protected]"};    
    
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, "請選擇郵件發送軟件"));


--------------------- 
原文:https://blog.csdn.net/zhizuyiwang/article/details/51547403?utm_source=copy 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章