can not send mail with attachment in Android


i have a problem to send mail with attachment. I'm using Javamail libraries (mail.jar, activitation.jar and additional.jar ). I can send mail accurately. But i can not send mail with an attachment is image to mail. I choose an image from gallery, and it is addded as my filename

 File f = new File("file://" + uri.getPath());

I think i have a problem when datasource took the my file's path. Whatever you can see much more thing in my code:(i've solved this problem and it is the last situation of my code)

first of all i add to view of my attachment :

Button Add = (Button) findViewById(;

    Add.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View view) {


here is my onAddAttachment2 and onActivityResult code

 private void onAddAttachment2(final String mime_type) {

            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(i, null),

    protected void onActivityResult(int requestCode, int resultCode,
        Intent imageReturnedIntent) {

    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

    mAttachments = (LinearLayout) findViewById(;

    switch (requestCode) {

        Uri _uri = imageReturnedIntent.getData();


        Cursor cursor = getContentResolver()
                        new String[] { android.provider.MediaStore.Images.ImageColumns.DATA },
                        null, null, null);
        String imageFilePath = cursor.getString(0);


        Log.v("imageFilePath", imageFilePath);

As u see there is i have an AddAttachment method. Here is the code:

private void addAttachment(Uri uri) {
        addAttachment(uri, null);

    private void addAttachment(Uri uri, String contentType) {
        long size = -1;
        String name = null;

        ContentResolver contentResolver = getContentResolver();

        Cursor metadataCursor = contentResolver.query(uri, new String[] {
                OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE }, null,
                null, null);

        if (metadataCursor != null) {
            try {
                if (metadataCursor.moveToFirst()) {
                    name = metadataCursor.getString(0);
                    size = metadataCursor.getInt(1);
            } finally {

        if (name == null) {
            name = uri.getLastPathSegment();

        String usableContentType = contentType;
        if ((usableContentType == null)
                || (usableContentType.indexOf('*') != -1)) {
            usableContentType = contentResolver.getType(uri);
        if (usableContentType == null) {
            usableContentType = getMimeTypeByExtension(name);

        if (size <= 0) {
            String uriString = uri.toString();
            if (uriString.startsWith("file://")) {
                Log.v(LOG_TAG, uriString.substring("file://".length()));
                File f = new File(uriString.substring("file://".length()));
                size = f.length();
            } else {
                Log.v(LOG_TAG, "Not a file: " + uriString);
        } else {
            Log.v(LOG_TAG, "old attachment.size: " + size);
        Log.v(LOG_TAG, "new attachment.size: " + size);

        Attachment attachment = new Attachment();
        attachment.uri = uri;
        attachment.contentType = usableContentType; = name;
        attachment.size = size;

        View view = getLayoutInflater().inflate(
                R.layout.message_compose_attachment, mAttachments, false);
        TextView nameView = (TextView) view.findViewById(;
        ImageButton delete = (ImageButton) view

        delete.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View view) {

                mAttachments.removeView((View) view.getTag());


and Attachment class that has properties

static class Attachment implements Serializable {
        private static final long serialVersionUID = 3642382876618963734L;
        public String name;
        public String contentType;
        public long size;
        public Uri uri;

finally in my class i have AddAttachment method:

public void addAttachment(String file) throws Exception {
    BodyPart messageBodyPart = new MimeBodyPart();

        FileDataSource source =  new FileDataSource(file);
        messageBodyPart.setDataHandler(new DataHandler(source));


When i clicked to send button, it have been sending to adress is written. But my attachment can not be shown. I have no error when i sent mail. I hope you had a solution for these problem...

Edit: OK finally i've solved the problem!.. first i've defined ArrayList<String> uris = new ArrayList<String>();

Then i've used it in my onActivityResult method like that uris.add(imageFilePath);

lastly, before m.send code block i've add the images:

for (int i = 0; i<uris.size(); i++)

in my class, the changes shown like that :

public void addAttachment(String file) throws Exception {
        BodyPart messageBodyPart = new MimeBodyPart();

            FileDataSource source =  new FileDataSource(file);
            messageBodyPart.setDataHandler(new DataHandler(source));


There definitely the problem of MIME Type. If you want to image attached with email you can send this with simply using

private void sendEmail(String[] to,String[] cc,String subject, String message)

        ArrayList<Uri> uris = new ArrayList<Uri>();

        Uri u = Uri.fromFile(new File(front_image));
        Uri u1 = Uri.fromFile(new File(side_image));

        Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
        emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
        emailIntent.putExtra(Intent.EXTRA_CC, cc);
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
        emailIntent.putExtra(Intent.EXTRA_TEXT, message);
        emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
        /*emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + show_right_latest_path));
        emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + show_right_prev_path));
        emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + show_front_latest_path));
        emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + show_front_prev_path));*/
        startActivity(Intent.createChooser(emailIntent, "Email"));