简体   繁体   English

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

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

I'd like to add an inline bitmap (generated within the code) to an email to be sent via JavaMail in Android.我想将内联 bitmap(在代码中生成)添加到 email 以通过 Android 中的 JavaMail 发送。 Below is my code currently:以下是我目前的代码:

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

I was thinking of converting the bitmap to a File object and then adding it to the body of the message, but I was thinking that there could be a more straightfirward and efficient way.我正在考虑将 bitmap 转换为文件 object,然后将其添加到消息正文中,但我认为可能有更直接和有效的方法。

The Jakarta Mail FAQ is your best resource.雅加达邮件常见问题解答是您最好的资源。 See How do I send HTML mail that includes images?请参阅如何发送包含图像的 HTML 邮件? . . That describes 3 choices:这描述了 3 个选择:

  1. Link image to web site, which I doubt works for you.将图像链接到 web 站点,我怀疑它是否适合您。
  2. Inline the image <img src="data:image/jpeg;base64,base64-encoded-data-here" />内联图片<img src="data:image/jpeg;base64,base64-encoded-data-here" />
  3. Construct a multipart/related message as you are doing.在你做的时候构造一个多部分/相关的消息。

The issue, as seen from the code, was that I was adding the text to the multipart, then the image (effectively overriding the text), and then I was assigning the multipart to the message.从代码中可以看出,问题是我将文本添加到多部分,然后是图像(有效地覆盖了文本),然后我将多部分分配给了消息。 The solution was to add the text, using addBodyPart(text) , and then use addBodyPart(image) .解决方案是使用addBodyPart(text)添加文本,然后使用addBodyPart(image) After that, I could use setContent(multipart) to properly assign the text and image to the email.之后,我可以使用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.

相关问题 使用javamail api发送的html电子邮件正文在Outlook中显示为乱码 - html email body sent using javamail api appears garbled in outlook 使用JavaMail API通过Servlet发送的电子邮件无法通过 - Email sent via servlet using JavaMail API not getting through 通过JavaMail发送的纯文本电子邮件作为附件到达 - Plain text email sent via JavaMail arrives as an attachment 使用JavaMail API通过Servlet通过附件发送的电子邮件未通过 - Email sent with attachment via servlet using JavaMail API not getting through 使用 JavaMail 在电子邮件中嵌入图像 - Inline images in email using JavaMail 无法在ExtentReport中查看使用JavaMail API发送到其他电子邮件ID的嵌入式图像? - Unable to view inline images in ExtentReport that was sent to a different email id using JavaMail API? GMail不显示使用JavaMail API发送的电子邮件的HTML- / Inline-部分 - GMail doesnt show HTML-/Inline-part of eMail sent with JavaMail API 未在使用JavaMail API发送的电子邮件中设置发件人 - Sender is not set in Email sent using JavaMail api 使用JavaMail在电子邮件中附加excel内联 - attach excel inline in the email message using JavaMail 带有附件的Javamail电子邮件:未发送文本 - Javamail email with attatchment: Text not being sent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM