gpt4 book ai didi

java - 是否可以将内联位图对象添加到通过 JavaMail 发送的电子邮件正文中?

转载 作者:行者123 更新时间:2023-12-01 16:36:47 24 4
gpt4 key购买 nike

我想将内联位图(在代码中生成)添加到要通过 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);
}

我正在考虑将位图转换为 File 对象,然后将其添加到消息正文中,但我认为可能有一种更直接、更有效的方法。

最佳答案

Jakarta 邮件常见问题解答是您最好的资源。请参阅How do I send HTML mail that includes images? 。这描述了 3 个选择:

  1. 将图像链接到网站,我怀疑这是否适合您。
  2. 内联图像 <img src="data:image/jpeg;base64,base64-encoded-data-here" />
  3. 像您正在做的那样构建多部分/相关消息。

关于java - 是否可以将内联位图对象添加到通过 JavaMail 发送的电子邮件正文中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61933826/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com