如何在android中发送电子邮件附件

时间:2015-01-20 09:39:52

标签: java android email android-intent

这是我的电子邮件onclick - 列表代码:

private void sendEmail(){

Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse("mailto:" + "recipient@example.com")); 
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "My email's subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "My email's body");

try {
    startActivity(Intent.createChooser(emailIntent, "Send email using..."));
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(Activity.this, "No email clients installed.",   Toast.LENGTH_SHORT).show();
}

}

如何从SdCard中的文件夹发送附件?

1 个答案:

答案 0 :(得分:-1)

尝试以下代码

public void clickbutton(View v) {
try {
strFile = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/temp";

File file = new File(strFile);
if (!file.exists())
file.mkdirs();
strFile = strFile + "/report.html";
createFile();
//
Log.i(getClass().getSimpleName(), "send  task - start");
//
final Intent emailIntent = new Intent(
android.content.Intent.ACTION_SEND);
//
address = "hamdy.ghanem@gmail.com";
subject = "Recite";
emailtext = "Please check the attached recite";
//
emailIntent.setType("plain/text");

emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] { address });

emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);

emailIntent.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file://" + strFile));

emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailtext);

this.startActivity(Intent
.createChooser(emailIntent, "Send mail..."));

} catch (Throwable t) {
Toast.makeText(this, "Request failed: " + t.toString(),
Toast.LENGTH_LONG).show();
}
}public void clickbutton(View v) {
try {
strFile = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/temp";

File file = new File(strFile);
if (!file.exists())
file.mkdirs();
strFile = strFile + "/report.html";
createFile();
//
Log.i(getClass().getSimpleName(), "send  task - start");
//
final Intent emailIntent = new Intent(
android.content.Intent.ACTION_SEND);
//
address = "hamdy.ghanem@gmail.com";
subject = "Recite";
emailtext = "Please check the attached recite";
//
emailIntent.setType("plain/text");

emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] { address });

emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);

emailIntent.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file://" + strFile));

emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailtext);

this.startActivity(Intent
.createChooser(emailIntent, "Send mail..."));

} catch (Throwable t) {
Toast.makeText(this, "Request failed: " + t.toString(),
Toast.LENGTH_LONG).show();
}
}

并查看此链接http://www.codeproject.com/Tips/268122/Send-email-with-attachment-by-android