2

我在发送带有附件的邮件时遇到问题。我正在使用 Javamail 库(mail.jar、activitation.jar 和 additional.jar)。我可以准确地发送邮件。但我不能发送带有附件的邮件是图像到邮件。我从图库中选择了一张图片,并将其添加为我的文件名

 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);
}

当我点击发送按钮时,它已经发送到地址被写入。但是我的附件无法显示。我发送邮件时没有错误。我希望你有解决这些问题的方法......

编辑:好的,我终于解决了这个问题!..首先我定义了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);
    }
4

2 回答 2

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"));


    }  
于 2012-05-01T10:11:23.427 回答
0

我希望您传递给 addAttachment 方法的字符串是文件名,而不是 URL(即,不以“file:”开头)。

要调试您的问题,请将代码添加到使用 FileInputStream 的 addAttachment 方法中,并查看您是否可以读取文件中的数据。如果你不能,JavaMail 也不能。

此外,打开会话调试并检查协议跟踪以查看 JavaMail 实际发送的内容。这可能会提供更多线索。或者,在您实际发送消息的代码中,添加 msg.writeTo(new FileOutputStream("msg.txt")) 并查看写入文件的内容。

于 2012-05-01T18:26:10.540 回答