【问题标题】:Sending Mail with Attachment only works in GMail发送带附件的邮件仅适用于 GMail
【发布时间】:2014-12-02 18:21:52
【问题描述】:

我正在尝试从我的 Galaxy Note 3 平板电脑发送带有附件的邮件。我发送邮件的代码如下所示:

public static void sendMail(Context context, String rc, String subject, String message, File file, String type) {
    Intent email = new Intent(Intent.ACTION_SEND);
    email.putExtra(Intent.EXTRA_EMAIL, new String[] { rc });
    email.putExtra(Intent.EXTRA_SUBJECT, subject);
    email.putExtra(Intent.EXTRA_TEXT, message);
    Uri uri = Uri.fromFile(file);
    email.putExtra("android.intent.extra.MIME_TYPES", type);
//      email.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
    email.setClassName("com.android.email", "com.android.email.activity.MessageCompose");
    email.putExtra(Intent.EXTRA_STREAM, uri);
    context.startActivity(email);
}

文件被保存到外部 SD 卡。如果我使用 Gmail 应用程序,附件会附上,我可以发送邮件。如果我使用 android 的 stock email 应用程序,附件没有附加,我不知道为什么。

有人知道为什么会这样吗?我发现了很多问题,如何将文件附加到电子邮件中,他们几乎都使用这种附加文件的方式。那么为什么它只适用于 gmail 应用呢?

【问题讨论】:

    标签: android email attachment


    【解决方案1】:

    我之前也遇到过类似的问题,但是使用此代码,我可以使用“myMail”作为客户端发送一封带有 csv 文件作为附件的电子邮件。

     Uri exportFile = null;
     exportFile = Uri.fromFile(file);
     Intent emailIntent = new Intent(Intent.ACTION_SEND);
    
     emailIntent.putExtra(Intent.EXTRA_SUBJECT, "testSubject");
     emailIntent.putExtra(Intent.EXTRA_STREAM, exportFile);
     String aEmailList[] = { "test@test.org" };
     emailIntent.putExtra(Intent.EXTRA_EMAIL, aEmailList);
     emailIntent.setType("message/rfc822");
     emailIntent.putExtra(
         Intent.EXTRA_TEXT,
         emailText
         );       
     startActivityForResult(emailIntent, REQUESTCODE_SENTEMAIL);
    

    【讨论】:

      【解决方案2】:

      我刚刚想通了...您必须使用电子邮件 Intent 的setType() 方法来设置邮件的类型。然后,股票邮件应用程序会识别附件。

      【讨论】:

      • 您最终设置的类型是什么? “图像/*”?
      • 老兄...什么类型的?
      猜你喜欢
      • 1970-01-01
      • 2017-06-22
      • 2014-03-12
      • 2015-10-25
      • 2018-11-11
      • 2016-05-29
      • 2012-05-07
      • 2016-09-22
      • 2018-08-28
      相关资源
      最近更新 更多