Hassaan Rabbani
Hassaan Rabbani

Reputation: 2465

Unable to attach an attachment with email in android

I am trying to send an email with an attachment, I have created a pdf file as well as a text file, for attaching a text file and sending an email i am using this code

email.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                String path = "/ScriptEmails/"+ filename;
                String filepath = Environment.getExternalStorageDirectory()+File.separator+"Screenwriter"+File.separator+path;
                sendEmail ("text", "enter email here", "Script from scriptwrite android", "Your script is attached", filepath);

And the send email function is like this:

public void sendEmail (String attachmentType, String emails, String subject, String text, String filePath)
    {
        Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
        emailIntent.setType(attachmentType);
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{emails}); 
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,subject); 
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, text); 
        emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///"+filePath/*mnt/sdcard/Myimage.jpeg"*/));
        startActivity(Intent.createChooser(emailIntent, "Send mail..."));
    }

now, the path is correct, the file name is correct, yet upon sending an email, it says, the attachment couldn't be send.

What i think is that i am not putting right ATTACHMENT TYPE for my file type.

What would be the attachment type for a text file and what would be an attachment type for a pdf file?

for text file i am using (txt/plain) is this correct?

Upvotes: 0

Views: 6135

Answers (2)

GrIsHu
GrIsHu

Reputation: 23638

For attaching the PDF you need to set the type as "application/pdf" and for text file "text/plain" below :

   File externalStorage = Environment.getExternalStorageDirectory()+File.separator+"Screenwriter"+File.separator+path;

Uri uri = Uri.fromFile(new File(externalStorage.getAbsolutePath() + "/" + "yourfilename.pdf"));

Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, emailAddress);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Text");
emailIntent.setType("application/pdf");
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);

startActivity(Intent.createChooser(emailIntent, "Send email using:"));

Upvotes: 2

Niranj Patel
Niranj Patel

Reputation: 33258

Try this code for attach file with email client.

    File file = new File(path);
    Uri pngUri = Uri.fromFile(file);
    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.setType("text/html");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[]{});
    emailIntent.putExtra(android.content.Intent.EXTRA_BCC,new String[]{});
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
    emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, pngUri);

Path should be like below.

final String path = Environment.getExternalStorageDirectory().toString()+ "/.....your_path";

Upvotes: 2

Related Questions