Hello I'm new to android and I have an Android application that allows the user to enter massage, his/her email, and the specific date they want to receive the email, and when they click send, the email will be sent to that address automatically in the background. is there a way to do it in android studio using kotlin?
I tried this but it's not working my code
package com.example.sendingautomaticemail
import android.util.Log
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.bouncycastle.jce.provider.BouncyCastleProvider
import java.security.Security
import java.text.SimpleDateFormat
import java.util.*
import javax.mail.*
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeBodyPart
import javax.mail.internet.MimeMessage
import javax.mail.internet.MimeMultipart
class EmailService(private var server: String, private var port: Int) {
data class Email(
val auth: Authenticator,
val toList: List<InternetAddress>,
val from: Address,
val subject: String,
val body: String
)
class UserPassAuthenticator(private val username: String, private val password: String) : Authenticator() {
override fun getPasswordAuthentication(): PasswordAuthentication {
return PasswordAuthentication(username, password)
}}
fun send(email: Email) {
val props = Properties()
props["mail.smtp.auth"] = "true"
props["mail.user"] = email.from
props["mail.smtp.host"] = server
props["mail.smtp.port"] = port
props["mail.smtp.starttls.enable"] = "true"
props["mail.smtp.ssl.trust"] = server
props["mail.mime.charset"] = "UTF-8"
val msg: Message = MimeMessage(Session.getDefaultInstance(props, email.auth))
msg.setFrom(email.from)
val sdf = SimpleDateFormat("dd/MM/yyyy hh:mm:ss")
val date = sdf.parse("06/12/2021 01:00:00")
msg.sentDate = date
msg.setRecipients(Message.RecipientType.TO, email.toList.toTypedArray())
msg.replyTo = arrayOf(email.from)
msg.addHeader("X-Mailer", CLIENT_NAME)
msg.addHeader("Precedence", "bulk")
msg.subject = email.subject
msg.setContent(MimeMultipart().apply {
addBodyPart(MimeBodyPart().apply {
setText(email.body, "iso-8859-1")
//setContent(email.htmlBody, "text/html; charset=UTF-8")
})
})
Transport.send(msg)}
companion object {
const val CLIENT_NAME = "Android StackOverflow programmatic email"}}
but msg.sentDate = date
not working the email was sanded right away not in the next day