in notifications/core/src/main/kotlin/org/opensearch/notifications/core/client/EmailMimeProvider.kt [30:97]
fun prepareMimeMessage(
session: Session,
fromAddress: String,
recipient: String,
messageContent: MessageContent
): MimeMessage {
// Create a new MimeMessage object
val mimeMessage = MimeMessage(session)
// Add from:
mimeMessage.setFrom(fromAddress)
// Add to:
mimeMessage.setRecipients(Message.RecipientType.TO, recipient)
// Add Subject:
mimeMessage.setSubject(messageContent.title, "UTF-8")
// Create a multipart/alternative child container
val msgBody = MimeMultipart("alternative")
// Create a wrapper for the HTML and text parts
val bodyWrapper = MimeBodyPart()
// Define the text part (if html part does not exists then use "-" string
val textPart = MimeBodyPart()
textPart.setContent(messageContent.textDescription, "text/plain; charset=UTF-8")
// Add the text part to the child container
msgBody.addBodyPart(textPart)
// Define the HTML part
if (messageContent.htmlDescription != null) {
val htmlPart = MimeBodyPart()
htmlPart.setContent(messageContent.htmlDescription, "text/html; charset=UTF-8")
// Add the HTML part to the child container
msgBody.addBodyPart(htmlPart)
}
// Add the child container to the wrapper object
bodyWrapper.setContent(msgBody)
// Create a multipart/mixed parent container
val msg = MimeMultipart("mixed")
// Add the parent container to the mimeMessage
mimeMessage.setContent(msg)
// Add the multipart/alternative part to the mimeMessage
msg.addBodyPart(bodyWrapper)
@SuppressWarnings("ComplexCondition")
if (messageContent.fileName != null &&
messageContent.fileData != null &&
messageContent.fileContentType != null &&
messageContent.fileEncoding != null
) {
// Add the attachment to the mimeMessage
var attachmentMime: MimeBodyPart? = null
when (messageContent.fileEncoding) {
"text" -> attachmentMime = createTextAttachmentPart(messageContent)
"base64" -> attachmentMime = createBinaryAttachmentPart(messageContent)
}
if (attachmentMime != null) {
msg.addBodyPart(attachmentMime)
}
}
return mimeMessage
}