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 

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