Search in sources :

Example 1 with MimeBodyPart

use of jakarta.mail.internet.MimeBodyPart in project athenz by yahoo.

the class EmailNotificationService method getMimeMessage.

private MimeMessage getMimeMessage(String subject, String body, Collection<String> recipients, String from, byte[] logoImage) throws MessagingException {
    Session session = Session.getDefaultInstance(new Properties());
    // Create a new MimeMessage object.
    MimeMessage message = new MimeMessage(session);
    // Add subject, from and to lines.
    message.setSubject(subject, CHARSET_UTF_8);
    message.setFrom(new InternetAddress(from));
    message.setRecipients(jakarta.mail.Message.RecipientType.BCC, InternetAddress.parse(String.join(",", recipients)));
    // Set the HTML part.
    MimeBodyPart htmlPart = new MimeBodyPart();
    htmlPart.setContent(body, "text/html; charset=" + CHARSET_UTF_8);
    // Create a multipart/mixed parent container.
    MimeMultipart msgParent = new MimeMultipart("related");
    // Add the body to the message.
    msgParent.addBodyPart(htmlPart);
    // Add the parent container to the message.
    message.setContent(msgParent);
    if (logoImage != null) {
        MimeBodyPart logo = new MimeBodyPart();
        logo.setContent(logoImage, "image/png");
        logo.setContentID(HTML_LOGO_CID_PLACEHOLDER);
        logo.setDisposition(Part.INLINE);
        // Add the attachment to the message.
        msgParent.addBodyPart(logo);
    }
    return message;
}
Also used : InternetAddress(jakarta.mail.internet.InternetAddress) MimeMessage(jakarta.mail.internet.MimeMessage) MimeMultipart(jakarta.mail.internet.MimeMultipart) MimeBodyPart(jakarta.mail.internet.MimeBodyPart) Session(jakarta.mail.Session)

Aggregations

Session (jakarta.mail.Session)1 InternetAddress (jakarta.mail.internet.InternetAddress)1 MimeBodyPart (jakarta.mail.internet.MimeBodyPart)1 MimeMessage (jakarta.mail.internet.MimeMessage)1 MimeMultipart (jakarta.mail.internet.MimeMultipart)1