有人会举例说明在android中发送带附件的邮件

时间:2012-07-13 07:13:47

标签: android email email-attachments

我需要开发一个小型电子邮件应用程序,我必须生成CSV并使用android生成CSV文件。我必须发送此附件而不保存到存储中。我发现使用文件编写器和我的代码附加CSV文件很困难,如下所示:

@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 email = new Intent(Intent.ACTION_SEND);
          email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
          //email.putExtra(Intent.EXTRA_CC, new String[]{ to});
          //email.putExtra(Intent.EXTRA_BCC, new String[]{to});
          email.setType("text/csv");
          email.putExtra(Intent.EXTRA_SUBJECT, subject);
          email.putExtra(Intent.EXTRA_TEXT, message);
        email.putExtra(Intent.EXTRA_STREAM,generateCsvFile("testdata.csv"));

          //need this to prompts email client only

          startActivity(Intent.createChooser(email, "Choose an Email client :"));

        }
    });
}
public static FileWriter generateCsvFile(String sFileName) throws IOException
   {
       FileWriter writer;
       writer = new FileWriter(sFileName);
       writer.append("DisplayName");
       writer.append(',');
       writer.append("Age");
       writer.append('\n');

       writer.append("RajeshV");
       writer.append(',');
       writer.append("26");
       writer.append('\n');

       writer.append("Mrithula");
       writer.append(',');
       writer.append("29");
       writer.append('\n');

      return writer;
    }

}

我在这一行遇到了问题:

email.putExtra(Intent.EXTRA_STREAM,generateCsvFile("testdata.csv"));

5 个答案:

答案 0 :(得分:2)

  

发送附件的步骤

Intent email = new Intent(Intent.ACTION_SEND);

email .setType("image/jpeg");

email .putExtra(Intent.EXTRA_SUBJECT, "My Picture");

email .putExtra(Intent.EXTRA_STREAM, Uri.parse("file://sdcard/captureimage.png"));

startActivity(Intent.createChooser(email , "Email:"));
  

有关详细信息,请参阅以下链接

multiple attachments

email with attached file

答案 1 :(得分:2)

我得到了工作代码

@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-p

答案 2 :(得分:1)

@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 email = new Intent(Intent.ACTION_SEND);
              email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
              //email.putExtra(Intent.EXTRA_CC, new String[]{ to});
              //email.putExtra(Intent.EXTRA_BCC, new String[]{to});
              email.putExtra(Intent.EXTRA_SUBJECT, subject);
              email.putExtra(Intent.EXTRA_TEXT, message);

              //need this to prompts email client only
              email.setType("message/rfc822");

              startActivity(Intent.createChooser(email, "Choose an Email client :"));

            }
        });
    }
}

答案 3 :(得分:1)

我已完成从SD卡发送任何带有邮件附件的文件..

Intent sendEmail= new Intent(Intent.ACTION_SEND);
   sendEmail.setType("rar/image");
   sendEmail.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new        
             File("/mnt/sdcard/download/abc.rar")));
             startActivity(Intent.createChooser(sendEmail, "Email:"));

答案 4 :(得分:0)

Intent jj=new Intent(android.content.Intent.ACTION_SEND);

Intent jj=new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);

String fileName = "file://" + Environment.getExternalStorageDirectory()+"/"+filename;         

jj.putExtra(Intent.EXTRA_SUBJECT, "Chat Info");

jj.putExtra(Intent.EXTRA_STREAM, Uri.parse(fileName));

jj.setType("message/rsc822");

Intent chooser = Intent.createChooser(jj , "Select Sender");

context.startActivity(chooser);