android导出到csv并作为电子邮件附件发送

发布于 2024-10-25 09:26:50 字数 1569 浏览 2 评论 0原文

我在这个网站上看到了多个线程讨论如何在 Android 中发送带有附件的电子邮件。我尝试了此处讨论的所有方法,此处此处

我正在通过代码创建一个 csv 文件并将该文件保存到 android 内部存储中。然后我想将此文件作为电子邮件附件发送。嗯,电子邮件正在发送,我收到了,没有附件。这就是我所做的。

String columnString         =   "\"Person\",\"Gender\",\"Street1\",\"PostOfice\",\"Age\"";
String dataString           =   "\"" + currentUser.userName +"\",\"" + currentUser.gender + "\",\"" + currentUser.street1 + "\",\"" + currentUser.poNumber.toString() + "\",\"" + currentUser.age.toString() + "\"";
String combinedString       =   columnString + "\n" + dataString;
File file                   =   new File(this.getCacheDir()+ File.separator + "Data.csv");
try {
    FileOutputStream out    =   new FileOutputStream(file);
    out.write(combinedString.getBytes());
    out.close();
} catch (IOException e) {
    Log.e("BROKEN", "Could not write file " + e.getMessage());
}   
Uri u1                      =   Uri.fromFile(file);

Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Person Details");
sendIntent.putExtra(Intent.EXTRA_STREAM, u1);
sendIntent.setType("text/richtext");
startActivity(sendIntent);

我尝试将 mime 设置更改为“text/html”和“text/richtext”等。但还没有运气。谁能告诉我我做错了什么?

I have seen multiple threads in this site discussing about sending email with attachments in android. I tried every methods discussed here, here and here.

I am creating a csv file via code and saving this file to android internal storage. Then I want to send this file as attachment in an email. Well, the email is being sent, I am getting it without attachment. This is what I have done.

String columnString         =   "\"Person\",\"Gender\",\"Street1\",\"PostOfice\",\"Age\"";
String dataString           =   "\"" + currentUser.userName +"\",\"" + currentUser.gender + "\",\"" + currentUser.street1 + "\",\"" + currentUser.poNumber.toString() + "\",\"" + currentUser.age.toString() + "\"";
String combinedString       =   columnString + "\n" + dataString;
File file                   =   new File(this.getCacheDir()+ File.separator + "Data.csv");
try {
    FileOutputStream out    =   new FileOutputStream(file);
    out.write(combinedString.getBytes());
    out.close();
} catch (IOException e) {
    Log.e("BROKEN", "Could not write file " + e.getMessage());
}   
Uri u1                      =   Uri.fromFile(file);

Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Person Details");
sendIntent.putExtra(Intent.EXTRA_STREAM, u1);
sendIntent.setType("text/richtext");
startActivity(sendIntent);

I tried changing mime settings to "text/html" and "text/richtext" etc. But no luck yet. Can anyone tell me what I am doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

红墙和绿瓦 2024-11-01 09:26:50

感谢所有试图提供帮助的人。花了一整天的时间后,我从我的应用程序发送了一封带有附件的电子邮件。这是工作代码。

String columnString =   "\"PersonName\",\"Gender\",\"Street1\",\"postOffice\",\"Age\"";
String dataString   =   "\"" + currentUser.userName +"\",\"" + currentUser.gender + "\",\"" + currentUser.street1 + "\",\"" + currentUser.postOFfice.toString()+ "\",\"" + currentUser.age.toString() + "\"";
String combinedString = columnString + "\n" + dataString;

File file   = null;
File root   = Environment.getExternalStorageDirectory();
if (root.canWrite()){
    File dir    =   new File (root.getAbsolutePath() + "/PersonData");
     dir.mkdirs();
     file   =   new File(dir, "Data.csv");
     FileOutputStream out   =   null;
    try {
        out = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            out.write(combinedString.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Uri u1  =   null;
u1  =   Uri.fromFile(file);

Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Person Details");
sendIntent.putExtra(Intent.EXTRA_STREAM, u1);
sendIntent.setType("text/html");
startActivity(sendIntent);

另外,如果您已将手机 SD 卡安装在机器中,则此代码将无法工作。一次只有一个人可以访问 SDCard。因此,在这种情况下,从计算机上卸载 SDCard 并尝试..感谢回答 此处..还要确保您已购买在清单文件中写入外部存储的权限...

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

希望它可以帮助某人...感谢所有试图提供帮助的人。 。

Thanks for everyone who tried to help..After taking a full day I have send an email from my app with attachment..This is the working code..

String columnString =   "\"PersonName\",\"Gender\",\"Street1\",\"postOffice\",\"Age\"";
String dataString   =   "\"" + currentUser.userName +"\",\"" + currentUser.gender + "\",\"" + currentUser.street1 + "\",\"" + currentUser.postOFfice.toString()+ "\",\"" + currentUser.age.toString() + "\"";
String combinedString = columnString + "\n" + dataString;

File file   = null;
File root   = Environment.getExternalStorageDirectory();
if (root.canWrite()){
    File dir    =   new File (root.getAbsolutePath() + "/PersonData");
     dir.mkdirs();
     file   =   new File(dir, "Data.csv");
     FileOutputStream out   =   null;
    try {
        out = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            out.write(combinedString.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Uri u1  =   null;
u1  =   Uri.fromFile(file);

Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Person Details");
sendIntent.putExtra(Intent.EXTRA_STREAM, u1);
sendIntent.setType("text/html");
startActivity(sendIntent);

Also If you have mounted your phone SDCard in the machine , this code wont work. Only one can access SDCard at one time. So in that case unmount your SDCard from computer and try..Thanks to the guy who answered here..Also make sure you have bought permission to write to external Storage in your manifest file...

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

Hope it helps someone...Thanks for everyone who tried to help..

源来凯始玺欢你 2024-11-01 09:26:50

此代码将帮助您

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    buttonSend = (Button) findViewById(R.id.buttonSend);

    textTo = (EditText) findViewById(R.id.editTextTo);
    textSubject = (EditText) findViewById(R.id.editTextSubject);
    textMessage = (EditText) findViewById(R.id.editTextMessage);

    buttonSend.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            String to = textTo.getText().toString();
            String subject = textSubject.getText().toString();
            String message = textMessage.getText().toString();

            Intent i = new Intent(Intent.ACTION_SEND);
            i.setType("plain/text");
            File data = null;
            try {
                Date dateVal = new Date();
                String filename = dateVal.toString();
                data = File.createTempFile("Report", ".csv");
                FileWriter out = (FileWriter) GenerateCsv.generateCsvFile(
                        data, "Name,Data1");
                i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(data));
                i.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
                i.putExtra(Intent.EXTRA_SUBJECT, subject);
                i.putExtra(Intent.EXTRA_TEXT, message);
                startActivity(Intent.createChooser(i, "E-mail"));

            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    });
}

public class GenerateCsv {
    public static FileWriter generateCsvFile(File sFileName,String fileContent) {
        FileWriter writer = null;

        try {
            writer = new FileWriter(sFileName);
            writer.append(fileContent);
                         writer.flush();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally
        {
            try {
                writer.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return writer;
    }
}

AndroidManifest.xml 文件中添加以下行:

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission

This Code will help you out

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    buttonSend = (Button) findViewById(R.id.buttonSend);

    textTo = (EditText) findViewById(R.id.editTextTo);
    textSubject = (EditText) findViewById(R.id.editTextSubject);
    textMessage = (EditText) findViewById(R.id.editTextMessage);

    buttonSend.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            String to = textTo.getText().toString();
            String subject = textSubject.getText().toString();
            String message = textMessage.getText().toString();

            Intent i = new Intent(Intent.ACTION_SEND);
            i.setType("plain/text");
            File data = null;
            try {
                Date dateVal = new Date();
                String filename = dateVal.toString();
                data = File.createTempFile("Report", ".csv");
                FileWriter out = (FileWriter) GenerateCsv.generateCsvFile(
                        data, "Name,Data1");
                i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(data));
                i.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
                i.putExtra(Intent.EXTRA_SUBJECT, subject);
                i.putExtra(Intent.EXTRA_TEXT, message);
                startActivity(Intent.createChooser(i, "E-mail"));

            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    });
}

public class GenerateCsv {
    public static FileWriter generateCsvFile(File sFileName,String fileContent) {
        FileWriter writer = null;

        try {
            writer = new FileWriter(sFileName);
            writer.append(fileContent);
                         writer.flush();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally
        {
            try {
                writer.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return writer;
    }
}

Add this line in AndroidManifest.xml file:

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission
っ左 2024-11-01 09:26:50

尝试

sendIntent.setType("message/rfc822");

Try

sendIntent.setType("message/rfc822");
棒棒糖 2024-11-01 09:26:50

对于内部存储文件,需要使文件可读:

shareFile.setReadable(true, false);

For internal storage files, you need to make the file readable:

shareFile.setReadable(true, false);

好倦 2024-11-01 09:26:50

以下是邮件中附件 csv 文件的代码(其工作代码):
MyCsvFile.csv”应该存在于您手机的内部/外部存储器中。

有关更多信息,请查看:https://stackoverflow.com/ a/48643905/8448886

以下是将 csv 文件附加到邮件中的代码:

String csv = (Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyCsvFile.csv"); // Here csv file name is MyCsvFile.csv


button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent emailIntent = new Intent(Intent.ACTION_SEND);
                emailIntent.setType("text/plain");
                emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
                emailIntent.putExtra(Intent.EXTRA_SUBJECT, "subject here");
                emailIntent.putExtra(Intent.EXTRA_TEXT, "body text");

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

Here is the code for attachment of csv file in mail (Its working code):
MyCsvFile.csv" should be present in your Internal/External memory of phone.

For More Look into this :https://stackoverflow.com/a/48643905/8448886

Below is the code for attachment of csv file into mail :

String csv = (Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyCsvFile.csv"); // Here csv file name is MyCsvFile.csv


button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent emailIntent = new Intent(Intent.ACTION_SEND);
                emailIntent.setType("text/plain");
                emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
                emailIntent.putExtra(Intent.EXTRA_SUBJECT, "subject here");
                emailIntent.putExtra(Intent.EXTRA_TEXT, "body text");

                File file = new File(csv);
                Uri uri = Uri.fromFile(file);
                emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
                startActivity(Intent.createChooser(emailIntent, "Pick an Email provider"));
            }
        });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文