简体   繁体   English

Java:发送带有附件的电子邮件:无法发送附件

[英]Java: Sending e-mail with attachment: Impossible to send the attachment

I'm working on creating an app but I'm having a problem. 我正在创建一个应用程序,但遇到了问题。 After I've inserted some data in a file named "message.txt", I'm not able to send this file by e-mail because my smartphone says "impossible to send the attachment". 在名为“ message.txt”的文件中插入一些数据后,由于智能手机显示“无法发送附件”,因此无法通过电子邮件发送此文件。 How can I resolve this problem? 我该如何解决这个问题?

This is the code: 这是代码:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String address = ...
    byte[] messagetosend = ...
    String filename = "message.txt";
    File file = new File(getExternalFilesDir(null), filename);
    FileOutputStream outputstream;
    try {
        outputstream = openFileOutput(filename,MODE_WORLD_READABLE);
        outputstream.write(messagetosend);
        outputstream.flush();
        outputstream.getFD().sync();
        outputstream.close();
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{address});
    sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
    sendIntent.setType("*/*");
    startActivity(Intent.createChooser(sendIntent,"Send with..."));
    }

Third party apps have no rights to access that file. 第三方应用无权访问该文件。 Use FileProvider to serve it, or copy the file to external storage instead of internal storage . 使用FileProvider为其提供服务,或将文件复制到外部存储而不是内部存储

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. 您需要将其写到外部存储,或将其公开。 visit this link and visit this you will understand about access levels. 访问此链接访问这个你就明白了有关访问级别。

OR try to send email with JavaMail here is solved , here is too 或者尝试使用JavaMail发送电子邮件, 这里解决了这里也是

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM