我正在尝试编写一个程序,该程序构造一封电子邮件,其正文中有 4 个组件,正文顶部的图像,下面的文本,包含文本的表格(尚未执行此操作,不在问题范围内),以及最后是文本和表格下方的第二张图片。目前,我的代码仅添加第一个 MimeBodyPart 作为内联图像,所有其他 MimeBodyPart(包括文本)都作为附件附加到电子邮件,而不是包含在正文中的文本中。电子邮件应该先保存在本地,然后再检查以手动发送。
仅供参考,我不熟悉 html,所以如果有问题,请提供答案/正确的方法来实现我想要实现的目标。此外,如果有人可以包括如何附加 2x2 表格并给出如何调整列/行大小并填充它们的指示,那就太棒了。
import java.io.File;
import java.io.FileOutputStream;
import java.util.UUID;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class MailGenerator {
public MailGenerator() {
}
public void writeMail() {
try{
Message message = new MimeMessage(Session.getInstance(System.getProperties()));
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]"));
message.setSubject("Testing");
//make it a draft!!
message.setHeader("X-Unsent", "1");
MimeBodyPart bodyText = new MimeBodyPart();
bodyText.setText("This is a test message");
Multipart multipart = new MimeMultipart();
// only the first bodypart here appears intext, commenting out the first brings the next into the body
uploadImage(multipart, "header.png");
multipart.addBodyPart(bodyText);
uploadImage(multipart, "footer.png");
// integration
message.setContent(multipart);
// store file
message.writeTo(new FileOutputStream(new File("mail.eml")));
} catch (Exception e) {
System.out.println(e); //Lazy catching
}
}
public void uploadImage(Multipart multipart, String fileName) throws Exception {
String contentID = getContentID(fileName);
MimeBodyPart htmlImage = new MimeBodyPart();
htmlImage.setText(""
+ "<html>"
+ " <body>"
+ " <img src=\"cid:" + contentID + "\" />"
+ " </body>"
+ "</html>"
,"US-ASCII", "html");
multipart.addBodyPart(htmlImage);
MimeBodyPart imagePart = new MimeBodyPart();
imagePart.attachFile(fileName);
imagePart.setContentID(contentID);
imagePart.setDisposition(MimeBodyPart.INLINE);
multipart.addBodyPart(imagePart);
}
public String getContentID(String prefix) {
return String.format("%s-%s", prefix, UUID.randomUUID());
}
}
我找到了一个快速的解决方法,对 html 有了一个灵光一现,我确实觉得有一种更好、更动态的方法可以向 html 添加更多元素,但我在这里工作的目标是:
public void uploadImage(Multipart multipart, String fileName0, String fileName1) throws Exception {
String contentID0 = getContentID(fileName0);
String contentID1 = getContentID(fileName1);
MimeBodyPart htmlImage = new MimeBodyPart();
htmlImage.setText(""
+ "<html>"
+ " <body>"
+ " <img src=\"cid:" + contentID0 + "\" />"
+ " <p>Here is my image:</p>"
+ " <img src=\"cid:" + contentID1 + "\" />"
+ " </body>"
+ "</html>"
,"US-ASCII", "html");
multipart.addBodyPart(htmlImage);
MimeBodyPart imagePart0 = new MimeBodyPart();
imagePart0.attachFile(fileName0);
imagePart0.setContentID(contentID0);
imagePart0.setDisposition(MimeBodyPart.INLINE);
MimeBodyPart imagePart1 = new MimeBodyPart();
imagePart1.attachFile(fileName1);
imagePart1.setContentID(contentID1);
imagePart1.setDisposition(MimeBodyPart.INLINE);
multipart.addBodyPart(imagePart0);
multipart.addBodyPart(imagePart1);
}
仍然对更好的选择和答案持开放态度。因为这应该比实际情况更加动态。