繁体   English   中英

是否可以将内联 bitmap object 添加到通过 JavaMail 发送的 email 的正文中?

[英]Is it possible to add inline bitmap object to the body of an email sent via JavaMail?

我想将内联 bitmap(在代码中生成)添加到 email 以通过 Android 中的 JavaMail 发送。 以下是我目前的代码:

try {
            // Compose the message
            // javax.mail.internet.MimeMessage class is
            // mostly used for abstraction.
            Message message = new MimeMessage(session);

            // header field of the header.
            message.setFrom(new InternetAddress("service@someone.com"));

            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
            message.setSubject("Workside Verification Service");
            message.setText(
                    "Thank you for registering. Please click on the following link to activate your account:\n\n"
                            + urlWithToken
                            + "\n\nRegards,\nThe Workside Team");

            // Add the generated QR code bitmap here
            Multipart multipart = new MimeMultipart("related");
            MimeBodyPart imgPart = new MimeBodyPart();
            // imageFile is the file containing the image

            // TODO - pass bitmap to imageFile below
            File file = new File(null);
            OutputStream os = new BufferedOutputStream(new FileOutputStream(file));

            mBitmapQR.compress(Bitmap.CompressFormat.PNG, 90, os);


            imgPart.attachFile(imageFile);
            multipart.addBodyPart(imgPart);
            message.setContent(multipart);

            Transport.send(message); // send Message

            System.out.println("Email Sent");

        } catch (MessagingException | FileNotFoundException e) {
            throw new RuntimeException(e);
        }

我正在考虑将 bitmap 转换为文件 object,然后将其添加到消息正文中,但我认为可能有更直接和有效的方法。

雅加达邮件常见问题解答是您最好的资源。 请参阅如何发送包含图像的 HTML 邮件? . 这描述了 3 个选择:

  1. 将图像链接到 web 站点,我怀疑它是否适合您。
  2. 内联图片<img src="data:image/jpeg;base64,base64-encoded-data-here" />
  3. 在你做的时候构造一个多部分/相关的消息。

从代码中可以看出,问题是我将文本添加到多部分,然后是图像(有效地覆盖了文本),然后我将多部分分配给了消息。 解决方案是使用addBodyPart(text)添加文本,然后使用addBodyPart(image) 之后,我可以使用setContent(multipart)将文本和图像正确分配给 email。

    // Add the generated QR code bitmap here
    Multipart multipart = new MimeMultipart("related");
    MimeBodyPart imgPart = new MimeBodyPart();

    // Set the cache path and generate the new file image
    String mFilePath = mContext.getCacheDir().toString();
    File file = new File(mFilePath, FILE_NAME);
    OutputStream os = new BufferedOutputStream(new FileOutputStream(file));

    // TITLE
    message.setSubject("Workside Verification Service");
    // TEXT
    MimeBodyPart txtPart = new MimeBodyPart();
    txtPart.setContent("Welcome to Workside! \n\nPlease proceed by scanning the QR code provided using the Workside application available in the Google Play store.\n\n\n"
                                + "Regards,\n\nThe Workside Team", "text/plain");
    // ADD TEXT
    multipart.addBodyPart(txtPart);
    // Generate image using the QR Bitmap, and attach it
    mBitmapQR.compress(Bitmap.CompressFormat.JPEG, 90, os);
    imgPart.attachFile(mFilePath + FILE_NAME);
    // ADD IMAGE
    multipart.addBodyPart(imgPart);
    message.setContent(multipart);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM