如何在Android中发送带附件的电子邮件
时间:2020-02-23 14:34:40 来源:igfitidea点击:
在本教程中,我们将学习如何在Android中以编程方式发送电子邮件,以及如何添加文件、图像或者视频。
电子邮件通常用于专业和私人谈话。许多应用程序需要电子邮件选项来向其他人发送数据。你可以很容易地用你的Android应用程序发送电子邮件。附件呢?不幸的是,Android不允许我们添加许多不同的文件格式,但是你可以添加很少的文件格式,比如照片、音频、视频、pdf和word文档。
电子邮件附件示例
让我们首先创建一个允许用户通过电子邮件发送数据的应用程序。Intent.ACTION发送在现有的电子邮件是通过手机发送的。同样Intent.ACTION_GET_内容用于附件。
Intent.ACTION_SEND
动作发送用于将数据发送到应用程序中的其他活动或者其他应用程序。两者都很容易理解和使用。如果要将数据发送到应用程序中的其他活动,则只需指定数据及其类型。Android操作系统将完成剩下的工作……它将找到兼容的活动并显示数据。同样,我们可以将数据发送到其他应用程序。将内容类型设置为简单纯文本。下面是它应该如何编码
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] { email });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,subject);
Intent.ACTION_GET_CONTENT
ACTION_GET_CONTENT用于选择多种类型的数据并将其返回。通过将MIME类型设置为此类数据,可以直接选择所需的内容类型。或者我们可以选择任何类型的数据。为此,用选择器包装意图,它将启动一个新的活动,并让用户选择他们想要的内容。下面是如何做到的
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_GALLERY);
添加列表文件权限
向列表文件添加以下权限。
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" <uses-permission android:name="android.permission.INTERNET" <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"
打开你的Android工作室并创建一个新项目。创建主活动并将以下代码粘贴到活动中_主.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
tools:context=".MainActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:padding="5dp" >
<EditText
android:id="@+id/et_to"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:hint="Receiver's Email Address!"
android:inputType="textEmailAddress"
android:singleLine="true"
<EditText
android:id="@+id/et_subject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/et_to"
android:layout_margin="5dp"
android:hint="Enter Subject"
android:singleLine="true"
<EditText
android:id="@+id/et_message"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="@id/et_subject"
android:layout_margin="5dp"
android:gravity="top|left"
android:hint="Compose Email"
android:inputType="textMultiLine"
<Button
android:id="@+id/bt_send"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_below="@id/et_message"
android:layout_margin="5dp"
android:text="Send"
<Button
android:id="@+id/bt_attachment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="attachment"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
</RelativeLayout>
</ScrollView>
现在来了主活动.java
package com.example.admin.emailattachmentexample;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText et_email;
EditText et_subject;
EditText et_message;
Button Send;
Button Attachment;
String email;
String subject;
String message;
String attachmentFile;
Uri URI = null;
private static final int PICK_FROM_GALLERY = 101;
int columnIndex;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_email = (EditText) findViewById(R.id.et_to);
et_subject = (EditText) findViewById(R.id.et_subject);
et_message = (EditText) findViewById(R.id.et_message);
Attachment = (Button) findViewById(R.id.bt_attachment);
Send = (Button) findViewById(R.id.bt_send);
//send button listener
Send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendEmail();
}
});
//attachment button listener
Attachment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openFolder();
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == PICK_FROM_GALLERY && resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
columnIndex = cursor.getColumnIndex(filePathColumn[0]);
attachmentFile = cursor.getString(columnIndex);
Log.e("Attachment Path:", attachmentFile);
URI = Uri.parse("file://" + attachmentFile);
cursor.close();
}
}
public void sendEmail()
{
try
{
email = et_email.getText().toString();
subject = et_subject.getText().toString();
message = et_message.getText().toString();
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] { email });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,subject);
if (URI != null) {
emailIntent.putExtra(Intent.EXTRA_STREAM, URI);
}
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
this.startActivity(Intent.createChooser(emailIntent,"Sending email..."));
}
catch (Throwable t)
{
Toast.makeText(this, "Request failed try again: " + t.toString(),Toast.LENGTH_LONG).show();
}
}
public void openFolder()
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_GALLERY);
}
}
运行你的应用程序,这里是输出
带附件的电子邮件示例
单击“添加附件”
现在点击发送按钮
发送电子邮件
我们可以通过单击此链接下载此项目。

