Android-将TXT文件作为附件发送到电子邮件失败(“无法发送附件") [英] Android - Sending TXT File as Attachment to Email Fails ("Couldn't send attachment")

查看:209
本文介绍了Android-将TXT文件作为附件发送到电子邮件失败(“无法发送附件")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让我的Android应用程序发送带有附件的电子邮件,并且我开始使用.txt文件,因为这些都很简单.

I am trying to get my Android app to send an e-mail with a file attached, and I'm starting out with a .txt file, since those are simple.

到目前为止,我有这个(发生在片段内):

So far I have this (taking place inside a Fragment):

//Send the email
Intent mailIntent = new Intent(Intent.ACTION_SEND);
mailIntent.setType("text/Message");
mailIntent.putExtra(Intent.EXTRA_EMAIL  , new String[]{address});
mailIntent.putExtra(Intent.EXTRA_SUBJECT, "Test Email");
mailIntent.putExtra(Intent.EXTRA_TEXT   , "Hi!  This is a test!");

//Deal with the attached report
String FileName = "report.txt";
Calculator.generateReport(getActivity().getApplicationContext(), FileName);
//It will be called "report.txt"
File attachment = getActivity().getApplicationContext().getFileStreamPath(FileName);
if (!attachment.exists() || !attachment.canRead()) {
    Toast.makeText(getActivity().getApplicationContext(), 
                   "Attachment Error", 
                   Toast.LENGTH_SHORT).show();
    System.out.println("ATTACHMENT ERROR");
}
else
{
    Uri uri = Uri.fromFile(attachment);
    mailIntent.putExtra(Intent.EXTRA_STREAM, uri);
}

//Send, if valid!
try {
   startActivity(Intent.createChooser(mailIntent, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(getActivity().getApplicationContext(), 
               "There are no email clients installed.", 
               Toast.LENGTH_SHORT).show();
}

不幸的是,这似乎不起作用.现在我知道该文件存在;如果在generateReport()之后插入适当的代码,则可以找到并访问该文件并读取其内容.在那里,我有正确的名字.

Unfortunately, that doesn't seem to work. Now I know the file exists; if I insert the appropriate code after generateReport(), I can find and access the file and read its contents. It is there, and I have the right name.

当我可以选择电子邮件客户端时,我选择了Gmail,然后看到确实有一个report.txt文件附加到电子邮件中.但是,当我发送电子邮件时,我收到一条通知,指出无法发送附件",并且电子邮件到达时没有任何附件.

When I am given the option of choosing an email client, I pick Gmail and see that there is indeed a report.txt file attached to the email. When I send the email, though, I get a notification stating "Couldn't send attachment", and the email arrives without anything attached.

我应该注意,我也尝试了其他意图类型,例如text/plainmessage/rfc822,但无济于事.

I should note that I have tried other intent types as well, such as text/plain and message/rfc822, to no avail.

关于我可能做错了什么的任何想法?

Any ideas on what I might be doing wrong?

推荐答案

如果您已将文件另存为该应用程序的私有文件,则该应用程序可以查看是否正常,但外部电子邮件客户端将无法看到它.

If you have saved the file as being private to the app, the app can see if fine but the external email client will not be able to see it.

您需要将其写到外部存储中,或将其公开.

You'll need to write it out to external storage, or make it public.

使用 http://developer.android.com/reference/android/content/Context.html#MODE_WORLD_READABLE http://developer.android.com/guide/topics/data/data-storage.html

这篇关于Android-将TXT文件作为附件发送到电子邮件失败(“无法发送附件")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆