From 3a94ef57e35618eda43597fe3cde07c46e4515d2 Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Sat, 17 Feb 2024 04:16:16 -0500 Subject: [PATCH] [+] Email service --- .../java/icu/samnyan/aqua/net/utils/Email.kt | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/main/java/icu/samnyan/aqua/net/utils/Email.kt b/src/main/java/icu/samnyan/aqua/net/utils/Email.kt index f415c579..e74fe17c 100644 --- a/src/main/java/icu/samnyan/aqua/net/utils/Email.kt +++ b/src/main/java/icu/samnyan/aqua/net/utils/Email.kt @@ -3,9 +3,12 @@ package icu.samnyan.aqua.net.utils import ext.Bool import ext.Str import icu.samnyan.aqua.net.db.AquaNetUser +import jakarta.annotation.PostConstruct import org.simplejavamail.api.mailer.Mailer import org.simplejavamail.email.EmailBuilder import org.simplejavamail.springsupport.SimpleJavaMailSpringSupport +import org.slf4j.Logger +import org.slf4j.LoggerFactory import org.springframework.boot.context.properties.ConfigurationProperties import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Import @@ -20,3 +23,53 @@ class EmailProperties { lateinit var senderAddr: Str } + +/** + * Email service. All email related operations should be placed here. + * + * Library Documentation: https://www.simplejavamail.org/ + */ +@Service +@Import(SimpleJavaMailSpringSupport::class) +class EmailService( + val mailer: Mailer, + val props: EmailProperties, +) { + val log: Logger = LoggerFactory.getLogger(EmailService::class.java) + + /** + * Test the connection of the email service on startup + */ + @PostConstruct + fun testConnection() { + if (!props.enable) return + + try { + mailer.testConnection() + log.info("Email Service Connected") + } catch (e: Exception) { + log.error("Email Service Connection Failed", e) + } + } + + /** + * Send a confirmation email to the user + */ + fun confirmationEmail(user: AquaNetUser) { + + } + + fun testEmail(addr: Str, name: Str) { + if (!props.enable) return + + log.info("Sending test email to $addr") + mailer.sendMail(EmailBuilder.startingBlank() + .from(props.senderName, props.senderAddr) + .to(name, addr) + .withSubject("Test Email") + .withPlainText("This is a test email to check if AquaNet Email Works").buildEmail()).thenRun { + log.info("Test email sent to $addr") + + } + } +} \ No newline at end of file