电子邮件附件在android中不成功

时间:2011-05-19 11:56:02

标签: android

考虑以下代码:

String outFileName = "/data/data/com.packagename/attachment.ics";

emailintent.putExtra(Intent.EXTRA_STREAM, Uri.parse(outFileName));
        emailintent.setType("plain/text");
        startActivity(Intent.createChooser(emailintent, "Send mail..."));

上面的代码启动了电子邮件客户端,并在启动时显示附件。但是当我发送电子邮件时,没有收到附件。身体正在接受。 这里出了什么问题?

提前谢谢。

编辑: 我需要为ics文件添加特定的mime类型吗?我甚至尝试发送一个txt文件,但也没有发送。当我尝试发送电子邮件时,附件会显示,但是当我收到电子邮件时它不会出现

5 个答案:

答案 0 :(得分:3)

我发现了正在发生的问题。我将要附加到电子邮件的文件放入我的应用程序内的私人文件夹中。电子邮件客户端无法访问..

所有我必须做的就是将它放在SD卡的公共目录中,然后电子邮件客户端获得访问权限,我开始收到我从我的应用程序发送的邮件。

PS:即使对于ics文件,MIME类型也是纯文本。

感谢您的帮助。

答案 1 :(得分:1)

有很多与此主题相关的主题。

您是否尝试添加此

i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + attachmentFilePath));

How to send an attachment with the Email in android?

Android: How do I attach a temporary, generated image to an email?

problem sending an email with an attachment programmatically

答案 2 :(得分:1)

我在使用附加SQLite数据库发送电子邮件时遇到同样的问题。我正在寻找这个问题的解决方案,但我一无所获。

无法通过电子邮件从内部存储发送附件。

要通过电子邮件发送文件,请先将该文件保存到外部存储,然后附加并发送。

我正在使用此代码将文件保存到SD卡

public void copyFileToSdCard()
    {
        File f = new File(Environment.getExternalStorageDirectory() + "/File name");
        if(f.exists()) 
        {

        }
        else
        {
            try 
            {
                File sd = Environment.getExternalStorageDirectory();
                File data = Environment.getDataDirectory();

                if (sd.canWrite()) 
                {
                    String currentPath = "file path";
                    String backupFilePath = "file name ";
                    File currentFile = new File(data, currentPath);
                    File backupFile = new File(sd, backupFilePath);

                    if (currentFile.exists()) {
                        FileChannel src = new FileInputStream(currentFile).getChannel();
                        FileChannel dst = new FileOutputStream(backupFile).getChannel();
                        dst.transferFrom(src, 0, src.size());
                        src.close();
                        dst.close();
                    }
                }
            } 
            catch (Exception e) {
                Log.w("Backup", e);
            }
        }
    }

这用于附加和发送

Intent i = new Intent(Intent.ACTION_SEND);
            i.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] {emailAddress}); 
            i.putExtra(Intent.EXTRA_SUBJECT, "Subject text");
            i.putExtra(Intent.EXTRA_TEXT, "Body text");
            Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "file name"));
            i.putExtra(Intent.EXTRA_STREAM, uri);
            i.setType("text/plain");
            startActivity(Intent.createChooser(i, "Send mail"));

答案 3 :(得分:0)

试试这个

emailintent.setType("text/calendar");

答案 4 :(得分:0)

public class SendingMail {

public static void SendingMail(Context context) {

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

    emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"abc@wxy.com"});  

    emailIntent.setType("text/html");


    // Image file saved in sdcard 

        emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+File.separator+"sdcard"
                + File.separator + "MyImage.png"));


    emailIntent.putExtra(Intent.EXTRA_TEXT, "My image");

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

}

这会有用......