No email arrived issue when I use javax.mail

65 Views Asked by At

I am making android app with kotlin. I want to send email with my gmail account so I used javax.mail package. And now I faced an issue to send gmail. following is full content of my Notify.kt file.

package com.facecool.utils

import android.graphics.Bitmap
import java.io.ByteArrayOutputStream
import java.util.Properties
import javax.activation.DataHandler
import javax.activation.DataSource
import javax.mail.Authenticator
import javax.mail.Message
import javax.mail.Multipart
import javax.mail.Part
import javax.mail.PasswordAuthentication
import javax.mail.Session;
import javax.mail.Transport
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeBodyPart
import javax.mail.internet.MimeMessage
import javax.mail.internet.MimeMultipart
import javax.mail.util.ByteArrayDataSource


internal object Notify {
    fun sendEmail(
        subject: String?,
        message: String?,
        recipientEmail: String?,
        bmp: Bitmap?
    ) {
        if (recipientEmail == null || recipientEmail.isEmpty()) return
        try {
            val yourMail = "[email protected]"
            val yourPwd = "password"
            val properties = getProperties()
            properties["mail.debug"] = "false"

            // Create a session with authentication
            val session: Session = Session.getInstance(properties, object : Authenticator() {
                override fun getPasswordAuthentication(): PasswordAuthentication {
                    return PasswordAuthentication(
                        yourMail,
                        yourPwd
                    )
                }
            })

                val mimeMessage: MimeMessage? = createMimeMessage(
                    session, yourMail, recipientEmail, subject, message,
                    bmp!!
                )
                Transport.send(mimeMessage)
        } catch (e: java.lang.Exception) {
 
            e.printStackTrace()
        }
    }
    fun getProperties(): Properties {

        val properties = Properties()
  
            properties.put("mail.smtp.host", "smtp.gmail.com")
            properties.put("mail.smtp.port", "587")
            properties.put("mail.smtp.starttls.enable", "true")
            properties.put("mail.smtp.auth", "true")
            properties.put("mail.smtp.ssl.trust", "smtp.gmail.com")
        
        return properties
    }
    
    private fun getImageData(bmp: Bitmap): ByteArray? {
        val byteArrayOutputStream = ByteArrayOutputStream()
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream)
        return byteArrayOutputStream.toByteArray()
    }
    
    fun createMimeMessage(
        session: Session?,
        yourMail: String,
        recipientEmail: String,
        subject: String?,
        message: String?,
        bmp: Bitmap
    ): MimeMessage? {
        try {
            val mimeMessage = MimeMessage(session)
            mimeMessage.setFrom(InternetAddress(yourMail))
            if (!recipientEmail.isEmpty()) mimeMessage.setRecipient(
                Message.RecipientType.TO,
                InternetAddress(recipientEmail)
            )
            mimeMessage.setSubject(subject)

            // Add text part to the multipart
            val multipart: Multipart = MimeMultipart("mixed")
            val textPart = MimeBodyPart()
            textPart.setText(message)
            multipart.addBodyPart(textPart)
            
            //add Recognized Image
            val filePart = MimeBodyPart()
            filePart.setFileName("Recognized Face.jpg")
            filePart.setDisposition(Part.ATTACHMENT)
            val source: DataSource = ByteArrayDataSource(getImageData(bmp), "image/jpeg")
            filePart.setDataHandler(DataHandler(source))
            multipart.addBodyPart(filePart)
            
            //end
            mimeMessage.setContent(multipart)
            return mimeMessage
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }
}

After I called the sendEmail method, no email arrives. I used my gmail address and its sign in password. Do I need to use 2-step authentication for gmail? Please let me know if my code has an issue.

I checked that the ‘javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. For more information, go to 535 5.7.8 https://support.google.com/mail/?p=BadCredentials l25-20020a2e8699000000b002c9f16d5da9sm1127595lji.95 - gsmtp’ is occured. How to fix this issue?

0

There are 0 best solutions below