Android开发中怎样调用系统Email发送邮件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android开发中怎样调用系统Email发送邮件相关的知识,希望对你有一定的参考价值。
在android中,调用Email有三种类型的Intent:
Intent.ACTION_SENDTO 无附件的发送
Intent.ACTION_SEND 带附件的发送
Intent.ACTION_SEND_MULTIPLE 带有多附件的发送
【方法1】使用SENTTO发送
Intent data=new Intent(Intent.ACTION_SENDTO);data.setData(Uri.parse("mailto:way.ping.li@gmail.com"));
data.putExtra(Intent.EXTRA_SUBJECT, "这是标题");
data.putExtra(Intent.EXTRA_TEXT, "这是内容");
startActivity(data);
Intent data=new Intent(Intent.ACTION_SENDTO);
data.setData(Uri.parse("mailto:way.ping.li@gmail.com"));
data.putExtra(Intent.EXTRA_SUBJECT, "这是标题");
data.putExtra(Intent.EXTRA_TEXT, "这是内容");
startActivity(data);
【方法2】使用SEND发送
Intent intent = new Intent(Intent.ACTION_SEND);String[] tos = "way.ping.li@gmail.com" ;
String[] ccs = "way.ping.li@gmail.com" ;
String[] bccs = "way.ping.li@gmail.com";
intent.putExtra(Intent.EXTRA_EMAIL, tos);
intent.putExtra(Intent.EXTRA_CC, ccs);
intent.putExtra(Intent.EXTRA_BCC, bccs);
intent.putExtra(Intent.EXTRA_TEXT, "body");
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///mnt/sdcard/a.jpg"));
intent.setType("image/*");
intent.setType("message/rfc882");
Intent.createChooser(intent, "Choose Email Client");
startActivity(intent);
Intent intent = new Intent(Intent.ACTION_SEND);
String[] tos = "way.ping.li@gmail.com" ;
String[] ccs = "way.ping.li@gmail.com" ;
String[] bccs = "way.ping.li@gmail.com";
intent.putExtra(Intent.EXTRA_EMAIL, tos);
intent.putExtra(Intent.EXTRA_CC, ccs);
intent.putExtra(Intent.EXTRA_BCC, bccs);
intent.putExtra(Intent.EXTRA_TEXT, "body");
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///mnt/sdcard/a.jpg"));
intent.setType("image/*");
intent.setType("message/rfc882");
Intent.createChooser(intent, "Choose Email Client");
startActivity(intent);
很简单,发送邮件中,有收件者,抄送者,密送者。 也就是分别通过
Intent.EXTRA_EMAIL,
Intent.EXTRA_CC,
Intent.EXTRA_BCC
来进行putExtra来设定的,而单个附件的发送,则使用Intent.EXTRA_STREAM来设置附件的地址Uri。
【方法3】使用SEND_MULTIPLE来进行多附件的发送
String[] tos = "way.ping.li@gmail.com" ;
String[] ccs = "way.ping.li@gmail.com" ;
intent.putExtra(Intent.EXTRA_EMAIL, tos);
intent.putExtra(Intent.EXTRA_CC, ccs);
intent.putExtra(Intent.EXTRA_TEXT, "body");
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
ArrayList<uri> imageUris = new ArrayList<uri>();
imageUris.add(Uri.parse("file:///mnt/sdcard/a.jpg"));
imageUris.add(Uri.parse("file:///mnt/sdcard/b.jpg"));
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
intent.setType("image/*");
intent.setType("message/rfc882");
Intent.createChooser(intent, "Choose Email Client");
startActivity(intent);
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
String[] tos = "way.ping.li@gmail.com" ;
String[] ccs = "way.ping.li@gmail.com" ;
intent.putExtra(Intent.EXTRA_EMAIL, tos);
intent.putExtra(Intent.EXTRA_CC, ccs);
intent.putExtra(Intent.EXTRA_TEXT, "body");
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
ArrayList<uri> imageUris = new ArrayList<uri>();
imageUris.add(Uri.parse("file:///mnt/sdcard/a.jpg"));
imageUris.add(Uri.parse("file:///mnt/sdcard/b.jpg"));
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
intent.setType("image/*");
intent.setType("message/rfc882");
Intent.createChooser(intent, "Choose Email Client");
startActivity(intent); 参考技术A 系统邮件系统的动作为android.content.Intent.ACTION_SEND,通过意图Intent可以激活系统发送邮件组件。Android开发中调用系统Email发送邮件可以用以下方法:
Intent email = new Intent(android.content.Intent.ACTION_SEND);
email.setType("plain/text");
emailReciver = new String[]"zhouyongyang122@gmail.com", "421134693@qq.com";
emailSubject = "你有一条短信";
emailBody = sb.toString();
//设置邮件默认地址
email.putExtra(android.content.Intent.EXTRA_EMAIL, emailReciver);
//设置邮件默认标题
email.putExtra(android.content.Intent.EXTRA_SUBJECT, emailSubject);
//设置要默认发送的内容
email.putExtra(android.content.Intent.EXTRA_TEXT, emailBody);
//调用系统的邮件系统
startActivity(Intent.createChooser(email, "请选择邮件发送软件"));
//如果需要带附件用以下方法:
Intent returnIt = new Intent(Intent.ACTION_SEND);
String[] tos = "wangmeng@gmail.com" ; //send to someone
String[] ccs = "tongyue@gmail.com" ; //Carbon Copy to someone
returnIt.putExtra(Intent.EXTRA_EMAIL, tos);
returnIt.putExtra(Intent.EXTRA_CC, ccs);
returnIt.putExtra(Intent.EXTRA_TEXT, "body");
returnIt.putExtra(Intent.EXTRA_SUBJECT, "subject");
Uri uri = Uri.parse("file:///sdcard/mysong.mp3");
returnIt.putExtra(Intent.EXTRA_STREAM, uri);
returnIt.setType("audio/mp3"); //use this format no matter what your attachment type is
returnIt.setType("message/rfc882");
Intent.createChooser(returnIt, "Choose Email Client");
startActivity(returnIt);
以上是关于Android开发中怎样调用系统Email发送邮件的主要内容,如果未能解决你的问题,请参考以下文章