发送电子邮件时包括附件

时间:2013-01-25 13:19:53

标签: android android-intent email-attachments

我的应用程序正在发送附带附件的电子邮件,但问题是它是否发送了电子邮件但没有附件。该文件确实存在于SD卡中,我可以将其分离并查看该文件。

以下是代码 Email.java

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL, new String[] { "" });
i.putExtra(Intent.EXTRA_SUBJECT, "");
i.putExtra(Intent.EXTRA_STREAM,Uri.parse("file://mnt/sdcard/fun/itisfun.csv"));    
i.putExtra(Intent.EXTRA_TEXT, "");
i.setType("message/rfc822");
startActivity(Intent.createChooser(i,"gmail :"));

以下是我在 Menifest

中尝试过的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<permission  android:name="android.permission.INTERNET"></permission>

我将非常感谢您的帮助。

4 个答案:

答案 0 :(得分:6)

试试如下:

       File pngDir = new File(Environment.getExternalStorageDirectory(),"fun/"); 
            if (!pngDir.exists())
            {
                pngDir.mkdirs();
            }              
            File pngfile=new File(pngDir,"itisfun.csv");
            Uri pngUri =Uri.fromFile(pngfile);
            Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
            emailIntent.setType("text/plain");
            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[]{""});
            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "");
            emailIntent.putExtra(android.content.Intent.EXTRA_STREAM,pngUri);
            emailIntent.setType("message/rfc822");
            startActivity(Intent.createChooser(emailIntent, "Send mail..."));

答案 1 :(得分:0)

以下是我用来将文件附加到电子邮件(Gmail应用)的代码:

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
        emailIntent.setType("message/rfc822");
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, 
            new String[]{emailTo});
        //has to be an ArrayList
        ArrayList<Uri> uris = new ArrayList<Uri>();
        //convert from paths to Android friendly Parcelable Uri's
        for (String file : filePaths)
        {
            File fileIn = new File(file);
            Uri u = Uri.fromFile(fileIn);
            uris.add(u);
        }
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailText);
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,subject);
        emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
        context.startActivity(Intent.createChooser(emailIntent, "Send mail via"));

答案 2 :(得分:0)

试试这段代码:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
// emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailSignature);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, toSenders);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subjectText);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, messageText+"\n\n"+emailSignature); 

emailIntent.setType("image/jpeg");
File bitmapFile = new File(Environment.getExternalStorageDirectory()+
        "/"+FOLDER_NAME+"/picture.jpg");
myUri = Uri.fromFile(bitmapFile);
emailIntent.putExtra(Intent.EXTRA_STREAM, myUri);
startActivity(Intent.createChooser(emailIntent, "Send your email in:"));

希望它会对你有所帮助。

答案 3 :(得分:0)

尝试以下代码

                Intent i = new Intent(Intent.ACTION_SEND);
                i.setType("text/plain");
                i.putExtra(Intent.EXTRA_EMAIL, new String[] { "" });
                i.putExtra(Intent.EXTRA_SUBJECT, "");
                i.putExtra(Intent.EXTRA_STREAM,Uri.parse("file://sdcard/fun/itisfun.csv"));    
                i.putExtra(Intent.EXTRA_TEXT, "");
                i.setType("message/rfc822");
                startActivity(Intent.createChooser(i,"gmail :"));