无法在Android中附加电子邮件附件

时间:2013-12-05 10:04:21

标签: android email

我正在尝试发送带有附件的电子邮件,我创建了一个pdf文件以及一个文本文件,用于附加文本文件并发送电子邮件我正在使用此代码

email.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                String path = "/ScriptEmails/"+ filename;
                String filepath = Environment.getExternalStorageDirectory()+File.separator+"Screenwriter"+File.separator+path;
                sendEmail ("text", "enter email here", "Script from scriptwrite android", "Your script is attached", filepath);

发送电子邮件功能如下:

public void sendEmail (String attachmentType, String emails, String subject, String text, String filePath)
    {
        Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
        emailIntent.setType(attachmentType);
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{emails}); 
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,subject); 
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, text); 
        emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///"+filePath/*mnt/sdcard/Myimage.jpeg"*/));
        startActivity(Intent.createChooser(emailIntent, "Send mail..."));
    }

现在,路径是正确的,文件名是正确的,但是在发送电子邮件时,它说,附件无法发送。

我认为我没有为我的文件类型选择正确的ATTACHMENT TYPE。

文本文件的附件类型是什么,pdf文件的附件类型是什么?

我正在使用的文本文件(txt / plain)是正确的吗?

3 个答案:

答案 0 :(得分:2)

检查一下:

Email with AttachmentExample

希望这有帮助。

答案 1 :(得分:2)

尝试使用此代码附加电子邮件客户端文件。

    File file = new File(path);
    Uri pngUri = Uri.fromFile(file);
    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.setType("text/html");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[]{});
    emailIntent.putExtra(android.content.Intent.EXTRA_BCC,new String[]{});
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
    emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, pngUri);

路径应如下所示。

final String path = Environment.getExternalStorageDirectory().toString()+ "/.....your_path";

答案 2 :(得分:2)

要附加PDF,您需要将类型设置为"application/pdf",并将文本文件"text/plain"设置为:

   File externalStorage = Environment.getExternalStorageDirectory()+File.separator+"Screenwriter"+File.separator+path;

Uri uri = Uri.fromFile(new File(externalStorage.getAbsolutePath() + "/" + "yourfilename.pdf"));

Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, emailAddress);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Text");
emailIntent.setType("application/pdf");
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);

startActivity(Intent.createChooser(emailIntent, "Send email using:"));