android - 无法在 Android 中发送带附件的邮件

标签 android jakarta-mail sendmail attachment

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

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

当数据源采用我的文件路径时,我认为我遇到了问题。不管你能在我的代码中看到更多的东西:(我已经解决了这个问题,这是我代码的最后一种情况)

首先我添加到我的附件 View :

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

最佳答案

肯定是MIME Type的问题。如果你想用电子邮件附加图片,你可以简单地使用

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


    }  

关于android - 无法在 Android 中发送带附件的邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10396214/

相关文章:

android - 如何在android中访问数据库sqlite

java - RecyclerView 在其他 View 之上没有触摸事件(覆盖)

java - 为什么我会得到这个异常(exception)?

java - 通过 JavaMail 发送点击后禁用链接

java - 如何使用 JavaMail 将多个文件附加到电子邮件?

android - 软件包安装程序已停止

javascript - React Native 中如何区分每个用户?

python - 在电子邮件 Python 中嵌入图像

email - 我们可以信任 `reply-to` 受到尊重吗

ios - 在 iPad 或 iPhone 上无需 UI 发送电子邮件的替代方法