不能与Android的附件发送邮件(can not send mail with attachmen

2019-09-16 22:32发布

我必须用附件发送邮件的问题。 我使用JavaMail库(的mail.jar,activitation.jar和additional.jar)。 我可以准确地发送邮件。 但带有附件的图像邮寄我无法发送邮件。 我从图库中选择图片,并且它addded作为我的文件名

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

我想我已经当数据源接过我的文件的路径问题。 无论你可以看到我的代码更件事:(我已经解决了这个问题,这是我的代码的最后一种情况)

首先,我添加到我的查看附件:

Button Add = (Button) findViewById(R.id.btnAdd);

    Add.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View view) {
            onAddAttachment2("image/*");

        }
    });

这里是我的onAddAttachment2和onActivityResult代码

 private void onAddAttachment2(final String mime_type) {



            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType(mime_type);
            startActivityForResult(Intent.createChooser(i, null),
                    ACTIVITY_REQUEST_PICK_ATTACHMENT);
        }

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

    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

    mAttachments = (LinearLayout) findViewById(R.id.attachments);

    switch (requestCode) {
    case ACTIVITY_REQUEST_PICK_ATTACHMENT:

        Uri _uri = imageReturnedIntent.getData();

        addAttachment(_uri);

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

            uris.add(imageFilePath);



        Log.v("imageFilePath", imageFilePath);
        break;
    }
}

正如你看到有我有一个AddAttachment方法。 下面是代码:

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 {
                metadataCursor.close();
            }
        }

        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;
        attachment.name = name;
        attachment.size = size;

        View view = getLayoutInflater().inflate(
                R.layout.message_compose_attachment, mAttachments, false);
        TextView nameView = (TextView) view.findViewById(R.id.attachment_name);
        ImageButton delete = (ImageButton) view
                .findViewById(R.id.attachment_delete);
        nameView.setText(attachment.name);
        delete.setTag(view);
        view.setTag(attachment);
        mAttachments.addView(view);


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

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


            }
        });
    }

具有性能及附件类

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

终于在我的Mail.java类我有AddAttachment方法:

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

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

    _multipart.addBodyPart(messageBodyPart);
}

当我点击发送按钮 ,它已经发送给ADRESS被写入。 但我的附件不能正常显示。 我没有错误,当我发送的邮件。 我希望你有针对这些问题的解决方案...

编辑:OK最后我已经解决了这个问题。首先我定义! ArrayList<String> uris = new ArrayList<String>();

然后我在我这样的onActivityResult方法,用它uris.add(imageFilePath);

最后,前m.send代码块,我添加图像:

for (int i = 0; i<uris.size(); i++)
                    {
                    m.addAttachment(uris.get(i).toString());
                    }

在我Mail.java类,显示这样的变化:

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

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

        _multipart.addBodyPart(messageBodyPart);
    }

Answer 1:

肯定有MIME类型的问题。 如果你想附有电子邮件图像您可以简单地使用该发送

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));
        uris.add(u);
        uris.add(u1);



        Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
        emailIntent.setData(Uri.parse("mailto:"));
        emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        emailIntent.setType("image/jpg");
        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"));


    }  


Answer 2:

我希望你传递给addAttachment方法的字符串是一个文件名,而不是一个URL(即不以启动“文件:”)。

要调试问题,添加代码到使用一个FileInputStream的addAttachment方法,看看你是否可以在文件中读取数据。 如果不能,JavaMail的将无法要么。

此外,打开会话调试和检查协议跟踪,看看有什么JavaMail是实际发送。 这可能会提供更多的线索。 或者,在你的代码实际发送消息,添加msg.writeTo(新FileOutputStream中(“msg.txt”)),看看什么东西写入文件。



文章来源: can not send mail with attachment in Android