如何将.txt文件附加到电子邮件中?

时间:2012-08-17 13:44:20

标签: android email

我对Android很新,我遇到了电子邮件的问题。我试图将一个文本文件附加到电子邮件并发送它,但是当我这样做时,我得到一个“文件太大”的错误。这是我第一次在应用程序中设置电子邮件,有人可以帮忙吗?

代码:

    File myFile = new File(Environment.getExternalStorageDirectory() + "/test.txt");
    Uri uri = Uri.fromFile(myFile);

    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);  

    String aEmailList[] = { "person@gmail.com" };    
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList);     
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Test");  
    emailIntent.setType("plain/text");  
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "This is a test.");
    emailIntent.putExtra(Intent.EXTRA_STREAM, uri);

    startActivity(emailIntent);   

1 个答案:

答案 0 :(得分:0)

  

您好,试试这个。

public void sendMail(String[] mailTo,String[] cc,String subject, String body, String attachmentFilePath)
{
    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    emailIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);

    emailIntent.setType("plain/text");

    if(mailTo!=null)
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,mailTo);
    if(cc!=null)
    emailIntent.putExtra(android.content.Intent.EXTRA_CC, cc);
    if(subject!=null)
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
    if(body!=null)
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(body));
    if(mailTo!=null)
    emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+attachmentFilePath));

    context.startActivity(emailIntent); 
}