Created
June 13, 2022 06:06
-
-
Save jidolstar/72221598832135c10642d15e8e46965b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ein.jvm.net | |
import ein.core.resource.eLoader | |
import ein.core.value.eJsonObject | |
import java.io.UnsupportedEncodingException | |
import java.util.* | |
import java.util.concurrent.Executors | |
import javax.mail.* | |
import javax.mail.internet.InternetAddress | |
import javax.mail.internet.MimeMessage | |
import javax.mail.internet.MimeUtility | |
class eGmail private constructor(private val user:String, private val pass:String){ | |
companion object:eLoader{ | |
fun init(){ eLoader += this } | |
private val map = mutableMapOf<String, eGmail>() | |
override fun load(res: eJsonObject) { | |
(res["gmail"] as? eJsonObject)?.forEach { k, eValue -> | |
(eValue as? eJsonObject)?.run { | |
add(k, s("user"), s("pass")) | |
} | |
} | |
} | |
operator fun invoke(key:String)= map[key]?:throw Throwable("invalid key") | |
fun add(key:String, user:String, pass:String) = eGmail(user, pass).also { | |
map.put(key, it) | |
} | |
} | |
private val props = Properties().also { | |
it["mail.smtp.auth"] = "true" | |
it["mail.smtp.starttls.enable"] = "true" | |
it["mail.smtp.host"] = "smtp.gmail.com" | |
it["mail.smtp.port"] = "587" | |
it["mail.smtp.ssl.trust"] = "smtp.gmail.com" | |
} | |
private val execService = Executors.newCachedThreadPool() | |
fun send(to:String, title:String, contents:String, vararg cc:String){ | |
execService.execute { | |
try { | |
Transport.send(MimeMessage(Session.getDefaultInstance(props, object : Authenticator() { | |
override fun getPasswordAuthentication(): PasswordAuthentication { | |
return PasswordAuthentication(user, pass) | |
} | |
})).apply { | |
setFrom(InternetAddress(user)) | |
addRecipient(Message.RecipientType.TO, InternetAddress(to)) | |
cc.forEach { | |
addRecipient(Message.RecipientType.CC, InternetAddress(it)) | |
} | |
subject = MimeUtility.encodeText(title, "UTF-8", "B") | |
setContent(contents, "text/html; charset=UTF-8") | |
}) | |
} catch (e: MessagingException) { | |
println("메일전송실패1 ${e.localizedMessage}") | |
throw RuntimeException(e) | |
} catch (e: UnsupportedEncodingException) { | |
println("메일전송실패2 ${e.localizedMessage}") | |
throw RuntimeException(e) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment