gpt4 book ai didi

java - Liferay:如何在 MailServiceUtil 中指定附件的 CID(以便它们可以嵌入(即图像)在 HTML 内容电子邮件中)

转载 作者:行者123 更新时间:2023-12-01 05:09:27 24 4
gpt4 key购买 nike

附件需要这样添加

MailMessage.addAttachment(File file, [String fileName])

,但内部似乎 fileName 仅用于 MimeBodyPart.setFileName()

无论如何我都找不到使用

MimeBodyPart.setContentID("myID") or MimeBodyPart.setHeader("Content-ID", "myID"); 

功能,因此我可以使用嵌入在邮件中的图像

<img src='CID:MyID'>

看来 MailEngine 位于门户 jar 中,因此仅供内部使用,并且我无法找到 MailServiceUtil 的解决方案。这是否意味着我需要从头开始解码所有 Liferay 高级 API 内容并使用 Java Mail API?

最佳答案

我认为 Liferay 没有办法做到这一点(至少在 6.2 版本中没有)。但我让它使用标准 Java 方法工作。以下与 Liferay 界面非常相似。

public void send(TemplateEmailerMailMessage mailMessage) throws UnsupportedEncodingException {
Properties properties = System.getProperties();
Session session = Session.getDefaultInstance(properties);

try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(mailMessage.getFromEmail(), mailMessage.getFromName()));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(mailMessage.getTo()));
message.setSubject(mailMessage.getSubject());

if (mailMessage.isHtmlFormat()) {
message.setText(mailMessage.getBody(), "text/html");
} else {
message.setText(mailMessage.getBody());
}

// create container for attachments and body parts
Multipart multipart = new MimeMultipart();

// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(mailMessage.getBody(), "text/html; charset=UTF-8");

multipart.addBodyPart(messageBodyPart);

// add attachments one by one
for (File file : mailMessage.getFileAttachments()) {
BodyPart messageAttachmentPart = new MimeBodyPart();
DataSource source = new FileDataSource(file);
messageAttachmentPart.setDataHandler(new DataHandler(source));
messageAttachmentPart.setFileName(file.getName());
// set Content-ID so it is recognized by <img src="cid: ... ">
messageAttachmentPart.setHeader("Content-ID", "<" + file.getName() + ">");
multipart.addBodyPart(messageAttachmentPart);
}

// Send the complete message parts
message.setContent(multipart);

// Send message
Transport.send(message);
} catch (MessagingException mex) {
mex.printStackTrace();
}
}

消息对象

public class TemplateEmailerMailMessage {

private String fromEmail;

private String fromName;

private String to;

private String body;

private String subject;

private boolean htmlFormat;

private List<File> fileAttachments = new ArrayList<>();

public void addAttachment(File file) {
fileAttachments.add(file);
}

public String getBody() {
return body;
}

public void setBody(String body) {
this.body = body;
}

public String getSubject() {
return subject;
}

public void setSubject(String subject) {
this.subject = subject;
}

public boolean isHtmlFormat() {
return htmlFormat;
}

public void setHtmlFormat(boolean htmlFormat) {
this.htmlFormat = htmlFormat;
}

public List<File> getFileAttachments() {
return fileAttachments;
}

public void setFileAttachments(List<File> fileAttachments) {
this.fileAttachments = fileAttachments;
}

public String getFromEmail() {
return fromEmail;
}

public void setFromEmail(String fromEmail) {
this.fromEmail = fromEmail;
}

public String getFromName() {
return fromName;
}

public void setFromName(String fromName) {
this.fromName = fromName;
}

public String getTo() {
return to;
}

public void setTo(String to) {
this.to = to;
}

public void setFrom(String fromEmail, String fromName) {
this.fromEmail = fromEmail;
this.fromName = fromName;
}

}

关于java - Liferay:如何在 MailServiceUtil 中指定附件的 CID(以便它们可以嵌入(即图像)在 HTML 内容电子邮件中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12232889/

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