【问题标题】:How to send an HTML file as an email content?如何将 HTML 文件作为电子邮件内容发送?
【发布时间】:2018-08-06 22:07:03
【问题描述】:
button2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        String emailList[]= {"rufidennis@gmail.com"};
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("message/rfc822");
        intent.putExtra(Intent.EXTRA_EMAIL,emailList);
        intent.putExtra(Intent.EXTRA_SUBJECT,"Email Subject");          
        intent.putExtra(Intent.EXTRA_TEXT,"file:///android_asset/www/sample3.html");                     
        startActivity(Intent.createChooser(intent,"Choice email APP"));
    }
});

我正在使用按钮 onclick 侦听器方法通过我的应用程序发送电子邮件。我有一个要作为电子邮件内容发送的 html 文件,文件的位置存储在资产内的 www 文件夹中。如何将该电子邮件作为电子邮件内容发送?

【问题讨论】:

标签: android html-email intentfilter android-assets


【解决方案1】:
final Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType(TEXT_HTML);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,emailTo);
emailIntent.putExtra(android.content.Intent.EXTRA_CC,emailCC);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT,"file:///android_asset/www/sample3.html");

context.startActivity(Intent.createChooser(emailIntent, context.getString(R.string.send_by_email)));

【讨论】:

  • 添加一些关于为什么 OP 应该使用您的代码的评论?
【解决方案2】:

使用以下代码发送带有文件附件的邮件

String filename="file.html"; 
File filelocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename);
Uri path = Uri.fromFile(filelocation); 
Intent emailIntent = new Intent(Intent.ACTION_SEND);
// set the type to 'email'
emailIntent .setType("vnd.android.cursor.dir/email");
String to[] = {"asd@gmail.com"};
emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
// the attachment
emailIntent .putExtra(Intent.EXTRA_STREAM, path);
// the mail subject
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
startActivity(Intent.createChooser(emailIntent , "Send email..."));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-04
    • 2018-04-29
    • 1970-01-01
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    相关资源
    最近更新 更多