Home > Blockchain >  Is there away to send scheduled email in android studio (kotlin) "msg.sentDate = date ` not wor
Is there away to send scheduled email in android studio (kotlin) "msg.sentDate = date ` not wor

Time:12-11

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

CodePudding user response:

I think best way to send an email. You should use GMailSender. You can create a scheduled email. You can find detailed description in Sending Email in Android using JavaMail API without using the default/built-in app

CodePudding user response:

msg.sentDate is just setting a metadata header on the email, once you send it to your outbound mail server it will still be handled immediately. You need to either use a third party library with this functionality (as another reply has suggested), or implement the delay yourself.

If you go down the latter route and still want to handle it entirely on the device (not necessarily the best idea, but that's a separate question) you need to store [1] the email content and send date somewhere (like a SQLite DB) and then periodically check for messages that are due to be sent in a recurring background task [2].

You can read about some of those things here:

1: https://developer.android.com/training/data-storage

2: https://developer.android.com/guide/background

  • Related