附件未以编程方式发送到邮件中

Posted

技术标签:

【中文标题】附件未以编程方式发送到邮件中【英文标题】:Attachment is not coming in mail programmatically 【发布时间】:2015-09-24 13:39:30 【问题描述】:

我正在用代码将 TEXT 文件附加到电子邮件:

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto",
                                    "abc@gmail.com", null));

    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Report");

    emailIntent.putExtra(Intent.EXTRA_TEXT, prepareBodyMail());
    File root = Environment.getExternalStorageDirectory();
    File file = new File(root, "/MyFolder/report.txt");

    Uri uri = Uri.fromFile(file);
    emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(emailIntent, "Pick an Email provider"));

此代码与 Gmail、电子邮件 和其他应用程序完美配合

但这不是通过 Google 的INBOX 应用程序附加文件

只有身体和主体来了,没有任何依恋

我已通过Inbox Problem 向 Google 网上论坛报告此问题

任何人都可以帮助我在代码中缺少什么吗?

【问题讨论】:

嘿,您解决了问题还是仍在搜索。请尝试我提供的解决方案希望这对你有用 我仍在寻找解决方案。我也已向 Google 群组报告了该问题。希望能得到解决。 对我来说同样的问题,无法在任何地方找到解决方案。 遇到同样的问题 :( @Kushal 你找到解决方案了吗? 【参考方案1】:
String fileLocation = Environment.getExternalStorageDirectory() + "/MyFolder/report.txt";    
String to[] = "abc@gmail.com";

Intent intentEmail = new Intent(Intent.ACTION_SEND);
intentEmail.setType("vnd.android.cursor.dir/email");
intentEmail.putExtra(Intent.EXTRA_EMAIL, to);
intentEmail.putExtra(Intent.EXTRA_STREAM, fileLocation);
intentEmail.putExtra(Intent.EXTRA_SUBJECT, "Subject");

startActivity(Intent.createChooser(intentEmail , "Pick an Email provider"));

【讨论】:

这也不适用于 Inbox 应用程序。我认为我们应该在意图中提供 Uri 类型参数 wirh Intent.EXTRA_STREAM kay 。这是您代码中的问题【参考方案2】:

试试这个

     Uri myUri = Uri.parse("file://" + path);
     emailIntent.putExtra(Intent.EXTRA_STREAM, myUri);

【讨论】:

【参考方案3】:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] "email@example.com");
intent.putExtra(Intent.EXTRA_SUBJECT, "subject here");
intent.putExtra(Intent.EXTRA_TEXT, "body text");
File root = Environment.getExternalStorageDirectory();
File file = new File(root, xmlFilename);
if (!file.exists() || !file.canRead()) 
    Toast.makeText(this, "Attachment Error", Toast.LENGTH_SHORT).show();
    finish();
    return;

Uri uri = Uri.fromFile("file://" + file);
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent, "Send email..."));

【讨论】:

这适用于 Gmail、电子邮件应用程序,但不适用于 Inbox 应用程序.. 我已经提到我的代码适用于 Gmail 和电子邮件,但不适用于收件箱..【参考方案4】:
  public void sendMailWithIntent(String emailTo,
                                   String subject, String emailText, List<String> filePaths) 
        try 
            //need to "send multiple" to get more than one attachment
            final Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
            emailIntent.setType("text/plain");
            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
                    Util.extractEmails(emailTo));
//            emailIntent.putExtra(android.content.Intent.EXTRA_CC,
//                    new String[]emailCC);
            emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
            emailIntent.putExtra(Intent.EXTRA_TEXT, emailText);
            ArrayList<Uri> uris = new ArrayList<Uri>();
            //has to be an ArrayList
            if (filePaths != null) 
                //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.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
            Intent chooser = Intent.createChooser(emailIntent, "Send mail...");
            activity.startActivityForResult(chooser, 1);
         catch (Exception e) 
            new ShowToast(context, e.getMessage());
        

    

调用方法

    List<String> list = new ArrayList<>();
    list.add(TO_ATTACH_ONE);
    list.add(TO_ATTACH_TWO);
    sendMailWithIntent(toAddresses, subject, body, list);

【讨论】:

【参考方案5】:

ACTION_SENDTO 好像官方不支持附件。它仅适用于 Gmail 和默认电子邮件客户端,因为它们使用“未记录”功能。我发现发送带有附件的电子邮件的唯一方法是使用 ACTION_SEND。

我的解决方案:

public class EmailSender 

private static final String INTENT_DATA_SCHEME_EMAIL = "mailto:";
private static final String FILE_PROVIDER_AUTHORIZER = "com.smartinhalerlive.app.fileprovider";

private String subject;
private String body;
private File attachment;

public EmailSender(String subject, String body, File attachment) 
    this.subject = subject;
    this.body = body;
    this.attachment = attachment;


public void sendEmail(Context context, EmailSenderListener listener) 
    List<ResolveInfo> emailClients = getEmailClients(context);

    if (null != emailClients) 
        ResolveInfo defaultEmailClient = getDefaultEmailClient(emailClients);

        if (null != defaultEmailClient) 
            sendEmailUsingSelectedEmailClient(context, defaultEmailClient, listener);
         else 
            showSelectEmailClientsDialog(context, emailClients, listener);
        
    


private void showSelectEmailClientsDialog(Context context,
                                          List<ResolveInfo> emailClients,
                                          EmailSenderListener listener) 
    String[] emailClientNames = getEmailClientNames(context, emailClients);

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle(R.string.email_dialog_title);
    builder.setItems(emailClientNames, (dialog, which) -> 
                sendEmailUsingSelectedEmailClient(context, emailClients.get(which), listener);
            
        );

    builder.create().show();


@NonNull
private static String[] getEmailClientNames(Context context, List<ResolveInfo> emailClients) 
    PackageManager packageManager = context.getPackageManager();
    String[] emailClientNames = new String[emailClients.size()];
    for (int i = 0; i < emailClients.size(); i++) 
        emailClientNames[i] = emailClients.get(i).activityInfo.applicationInfo.loadLabel(packageManager).toString();
    
    return emailClientNames;


@Nullable
private static ResolveInfo getDefaultEmailClient(List<ResolveInfo> emailClients) 
    ResolveInfo defaultEmailClient = null;
    if (emailClients.size() == 1) 
        defaultEmailClient = emailClients.get(0);
     else 
        for (ResolveInfo emailClient : emailClients) 
            if (emailClient.isDefault) 
                defaultEmailClient = emailClient;
                break;
            
        
    
    return defaultEmailClient;


private static List<ResolveInfo> getEmailClients(Context context) 
    PackageManager pm = context.getPackageManager();
    Intent emailDummyIntent = getEmptyEmailIntent();
    return pm.queryIntentActivities(emailDummyIntent, 0);


public static Intent getEmptyEmailIntent() 
    Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
    emailIntent.setData(Uri.parse(INTENT_DATA_SCHEME_EMAIL));
    return emailIntent;


private static void grantReadPermissionForAttachmentUri(Context context,
                                                        Intent emailIntent,
                                                        Uri attachmentUri) 
    if (emailIntent == null || attachmentUri == null) 
        return;
    
    context.grantUriPermission(emailIntent.getComponent().getPackageName(),
            attachmentUri,
            Intent.FLAG_GRANT_READ_URI_PERMISSION);


private void sendEmailUsingSelectedEmailClient(Context context,
                                               ResolveInfo emailClient,
                                               EmailSenderListener listener) 
    try 
        Intent emailIntent = new Intent(Intent.ACTION_SEND);

        emailIntent.putExtra(Intent.EXTRA_SUBJECT, null != subject ? subject : "");
        emailIntent.putExtra(Intent.EXTRA_TEXT, null != body ? body : "");
        emailIntent.setComponent(new ComponentName(emailClient.activityInfo.packageName, emailClient.activityInfo.name));
        addAttachment(context, attachment, emailIntent);

        listener.onEmailIntentReady(emailIntent);
     catch (Exception ex) 
        Timber.e("Error sending email", ex);
    


private static void addAttachment(Context context, File attachment, Intent emailIntent) 
    if (attachment != null) 
        Uri contentUri = FileProvider.getUriForFile(context, FILE_PROVIDER_AUTHORIZER, attachment);
        grantReadPermissionForAttachmentUri(context, emailIntent, contentUri);
        emailIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
    


public interface EmailSenderListener 
    void onEmailIntentReady(Intent emailIntent);

【讨论】:

以上是关于附件未以编程方式发送到邮件中的主要内容,如果未能解决你的问题,请参考以下文章

有没有一种在事先不知道文件类型的情况下使用 SMTP 在 python 中发送电子邮件附件的快速方法?

C#.net 对带有附件的电子邮件进行排队以进行发送的最佳方式

如何在python程序中发邮件

如何在iOS5中发送电子邮件到后台的收据?

未以编程方式检测 Dismiss Popover

在服务中发送电子邮件(不提示用户)