mirror of
https://github.com/OpenBankProject/OBP-API.git
synced 2026-02-06 20:06:56 +00:00
refactor/used Apache Commons Email instead of LiftMail- step2
This commit is contained in:
parent
da1bf7e615
commit
ffe53c9622
@ -1,11 +1,10 @@
|
||||
package code.api.util
|
||||
|
||||
import org.apache.commons.mail.{Email, SimpleEmail, HtmlEmail, MultiPartEmail, EmailAttachment, DefaultAuthenticator}
|
||||
import java.io.File
|
||||
import java.net.URL
|
||||
import code.util.Helper.MdcLoggable
|
||||
import net.liftweb.common.{Box, Empty, Full}
|
||||
import net.liftweb.util.Helpers.now
|
||||
import org.apache.commons.mail._
|
||||
|
||||
import java.net.URL
|
||||
|
||||
/**
|
||||
* Apache Commons Email Wrapper for OBP-API
|
||||
@ -41,6 +40,42 @@ object CommonsEmailWrapper extends MdcLoggable {
|
||||
attachments: List[EmailAttachment] = List.empty
|
||||
)
|
||||
|
||||
/**
|
||||
* Get default email configuration from OBP-API properties
|
||||
*/
|
||||
def getDefaultEmailConfig(): EmailConfig = {
|
||||
EmailConfig(
|
||||
smtpHost = APIUtil.getPropsValue("mail.smtp.host", "localhost"),
|
||||
smtpPort = APIUtil.getPropsValue("mail.smtp.port", "1025").toInt,
|
||||
username = APIUtil.getPropsValue("mail.smtp.user", ""),
|
||||
password = APIUtil.getPropsValue("mail.smtp.password", ""),
|
||||
useTLS = APIUtil.getPropsValue("mail.smtp.starttls.enable", "false").toBoolean,
|
||||
useSSL = APIUtil.getPropsValue("mail.smtp.ssl.enable", "false").toBoolean,
|
||||
debug = APIUtil.getPropsValue("mail.debug", "false").toBoolean
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Send simple text email with default configuration
|
||||
*/
|
||||
def sendTextEmail(content: EmailContent): Box[String] = {
|
||||
sendTextEmail(getDefaultEmailConfig(), content)
|
||||
}
|
||||
|
||||
/**
|
||||
* Send HTML email with default configuration
|
||||
*/
|
||||
def sendHtmlEmail(content: EmailContent): Box[String] = {
|
||||
sendHtmlEmail(getDefaultEmailConfig(), content)
|
||||
}
|
||||
|
||||
/**
|
||||
* Send email with attachments using default configuration
|
||||
*/
|
||||
def sendEmailWithAttachments(content: EmailContent): Box[String] = {
|
||||
sendEmailWithAttachments(getDefaultEmailConfig(), content)
|
||||
}
|
||||
|
||||
/**
|
||||
* Send simple text email
|
||||
*/
|
||||
@ -170,9 +205,43 @@ object CommonsEmailWrapper extends MdcLoggable {
|
||||
attachment.setName(name)
|
||||
attachment
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test MailHog configuration, this for testing
|
||||
* Test Gmail configuration
|
||||
*/
|
||||
def testGmailConfig(): Unit = {
|
||||
val config = EmailConfig(
|
||||
smtpHost = "smtp.gmail.com",
|
||||
smtpPort = 587,
|
||||
username = "zhw55wyl@gmail.com",
|
||||
password = "yqmadztqynmasrpm",
|
||||
useTLS = true,
|
||||
debug = true
|
||||
)
|
||||
|
||||
val content = EmailContent(
|
||||
from = "zhw55wyl@gmail.com",
|
||||
to = List("zhw110@hotmail.com"),
|
||||
subject = "Test Apache Commons Email Wrapper",
|
||||
textContent = Some("This is a test email sent using Apache Commons Email wrapper."),
|
||||
htmlContent = Some("<html><body><h1>Test Email</h1><p>This is a test email sent using Apache Commons Email wrapper.</p></body></html>")
|
||||
)
|
||||
|
||||
logger.info("Testing Gmail configuration with Apache Commons Email...")
|
||||
|
||||
sendTextEmail(config, content) match {
|
||||
case Full(messageId) => logger.info(s"Text email sent successfully: $messageId")
|
||||
case Empty => logger.error("Failed to send text email")
|
||||
}
|
||||
|
||||
sendHtmlEmail(config, content) match {
|
||||
case Full(messageId) => logger.info(s"HTML email sent successfully: $messageId")
|
||||
case Empty => logger.error("Failed to send HTML email")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test MailHog configuration
|
||||
*/
|
||||
def testMailHogConfig(): Unit = {
|
||||
val config = EmailConfig(
|
||||
|
||||
@ -12,6 +12,7 @@ import code.api.dynamic.entity.helper.DynamicEntityInfo
|
||||
import code.api.util.APIUtil.{fullBoxOrException, _}
|
||||
import code.api.util.ApiRole._
|
||||
import code.api.util.ApiTag._
|
||||
import code.api.util.CommonsEmailWrapper._
|
||||
import code.api.util.DynamicUtil.Validation
|
||||
import code.api.util.ErrorMessages.{BankNotFound, _}
|
||||
import code.api.util.ExampleValue._
|
||||
@ -84,7 +85,6 @@ import net.liftweb.json.Serialization.write
|
||||
import net.liftweb.json._
|
||||
import net.liftweb.util.Helpers.{now, tryo}
|
||||
import net.liftweb.util.{Helpers, StringHelpers}
|
||||
import code.api.util.CommonsEmailWrapper._
|
||||
import org.apache.commons.lang3.StringUtils
|
||||
|
||||
import java.net.URLEncoder
|
||||
@ -96,7 +96,6 @@ import scala.collection.immutable.{List, Nil}
|
||||
import scala.collection.mutable.ArrayBuffer
|
||||
import scala.concurrent.Future
|
||||
import scala.jdk.CollectionConverters.collectionAsScalaIterableConverter
|
||||
import scala.xml.XML
|
||||
|
||||
trait APIMethods400 extends MdcLoggable {
|
||||
self: RestHelper =>
|
||||
@ -3370,15 +3369,6 @@ trait APIMethods400 extends MdcLoggable {
|
||||
logger.debug(s"Before send user invitation by email. Purpose: ${UserInvitationPurpose.DEVELOPER}")
|
||||
|
||||
// Use Apache Commons Email wrapper instead of Lift Mailer
|
||||
val emailConfig = EmailConfig(
|
||||
smtpHost = APIUtil.getPropsValue("mail.smtp.host", "localhost"),
|
||||
smtpPort = APIUtil.getPropsValue("mail.smtp.port", "1025").toInt,
|
||||
username = APIUtil.getPropsValue("mail.smtp.user", ""),
|
||||
password = APIUtil.getPropsValue("mail.smtp.password", ""),
|
||||
useTLS = APIUtil.getPropsValue("mail.smtp.starttls.enable", "false").toBoolean,
|
||||
debug = APIUtil.getPropsValue("mail.debug", "false").toBoolean
|
||||
)
|
||||
|
||||
val emailContent = EmailContent(
|
||||
from = from,
|
||||
to = List(invitation.email),
|
||||
@ -3387,7 +3377,7 @@ trait APIMethods400 extends MdcLoggable {
|
||||
htmlContent = Some(customHtmlText)
|
||||
)
|
||||
|
||||
sendHtmlEmail(emailConfig, emailContent) match {
|
||||
sendHtmlEmail(emailContent) match {
|
||||
case Full(messageId) => logger.debug(s"Email sent successfully with Message-ID: $messageId")
|
||||
case Empty => logger.error("Failed to send user invitation email")
|
||||
}
|
||||
@ -3405,15 +3395,6 @@ trait APIMethods400 extends MdcLoggable {
|
||||
logger.debug(s"Before send user invitation by email.")
|
||||
|
||||
// Use Apache Commons Email wrapper instead of Lift Mailer
|
||||
val emailConfig = EmailConfig(
|
||||
smtpHost = APIUtil.getPropsValue("mail.smtp.host", "localhost"),
|
||||
smtpPort = APIUtil.getPropsValue("mail.smtp.port", "1025").toInt,
|
||||
username = APIUtil.getPropsValue("mail.smtp.user", ""),
|
||||
password = APIUtil.getPropsValue("mail.smtp.password", ""),
|
||||
useTLS = APIUtil.getPropsValue("mail.smtp.starttls.enable", "false").toBoolean,
|
||||
debug = APIUtil.getPropsValue("mail.debug", "false").toBoolean
|
||||
)
|
||||
|
||||
val emailContent = EmailContent(
|
||||
from = from,
|
||||
to = List(invitation.email),
|
||||
@ -3422,7 +3403,7 @@ trait APIMethods400 extends MdcLoggable {
|
||||
htmlContent = Some(customHtmlText)
|
||||
)
|
||||
|
||||
sendHtmlEmail(emailConfig, emailContent) match {
|
||||
sendHtmlEmail(emailContent) match {
|
||||
case Full(messageId) => logger.debug(s"Email sent successfully with Message-ID: $messageId")
|
||||
case Empty => logger.error("Failed to send user invitation email")
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user