[英]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 个选择:
<img src="data:image/jpeg;base64,base64-encoded-data-here" />
内联图片<img src="data:image/jpeg;base64,base64-encoded-data-here" />
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.