简体   繁体   English

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

[英]Liferay: How to specify CID for attachments (so they can be embeded (i.e images) in HTML content e-mails) in MailServiceUtil

Attachments need to be added like this 附件需要像这样添加

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

, but innerly it seems that fileName is only used for MimeBodyPart.setFileName() ,但从内部看,fileName仅用于MimeBodyPart.setFileName()

I dont find anyway to use the 无论如何,我找不到使用

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

feature, so I can use images embeded in mail with 功能,因此我可以使用嵌入邮件的图像

<img src='CID:MyID'>

It seems MailEngine is in the portal jar so only for internal use, and I was not able to find a solution for MailServiceUtil. 看来MailEngine在门户jar中,所以仅供内部使用,而我找不到MailServiceUtil的解决方案。 Does it mean I need to decode all Liferay high-level API stuff from scratch and use Java Mail API? 这是否意味着我需要从头开始解码所有Liferay高级API内容并使用Java Mail API?

I don't think there's a way to do that with Liferay (at least not at version 6.2). 我认为Liferay没有办法做到这一点(至少在6.2版中没有)。 But I made it work with standard Java approach. 但是我使它与标准Java方法一起使用。 The following is pretty similar to Liferay interface. 以下内容与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();
    }
}

The message object 消息对象

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM