Java 如何在android中发送电子邮件附件
这是我的电子邮件Java 如何在android中发送电子邮件附件,java,android,email,android-intent,Java,Android,Email,Android Intent,这是我的电子邮件onclick-listner代码: 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"); email
onclick
-listner代码:
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();
}
}
如何从SD卡中的文件夹发送附件?请尝试下面的代码
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();
}
}
在发送文件时查看此链接,然后需要将
Uri
对象作为EXTRA\u流
传递给。因此,您需要通过Uri=Uri.parse(新文件(您的文件路径))
访问它,并且需要使用emailIntent.putExtra(Intent.EXTRA_-STREAM,Uri)代码>请参考此链接嗨Anjali,您是否在您的终端测试过此代码?这有用吗?看看代码在做什么,问了什么问题。