Skip to content

Instantly share code, notes, and snippets.

@ch8n
Last active May 22, 2019 10:33
Show Gist options
  • Select an option

  • Save ch8n/ae9378a802e9b1247d9b8574b4737749 to your computer and use it in GitHub Desktop.

Select an option

Save ch8n/ae9378a802e9b1247d9b8574b4737749 to your computer and use it in GitHub Desktop.
object Mailer {
@SuppressLint("CheckResult")
fun sendMail(email: String, subject: String, message: String): Completable {
return Completable.create { emitter ->
//configure SMTP server
val props: Properties = Properties().also {
it.put("mail.smtp.host", "smtp.gmail.com")
it.put("mail.smtp.socketFactory.port", "465")
it.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory")
it.put("mail.smtp.auth", "true")
it.put("mail.smtp.port", "465")
}
//Creating a session
val session = Session.getDefaultInstance(props, object : Authenticator() {
override fun getPasswordAuthentication(): PasswordAuthentication {
return PasswordAuthentication(Config.EMAIL, Config.PASSWORD)
}
})
try {
MimeMessage(session).let { mime ->
mime.setFrom(InternetAddress(Config.EMAIL))
//Adding receiver
mime.addRecipient(Message.RecipientType.TO, InternetAddress(email))
//Adding subject
mime.setSubject(subject)
//Adding message
mime.setText(message)
//send mail
Transport.send(mime)
}
} catch (e: MessagingException) {
emitter.onError(e)
}
//ending subscription
emitter.onComplete()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment