Kotlin (Android) Send SMTP Email with Awesome MailDroid Library [NEW] – Coding Shiksha

Kotlin (Android) Send SMTP Email with Awesome MailDroid Library [NEW] – Coding Shiksha

Welcome folks I am back with another tutorial send this tutorial we will be talking about that how we can implement SMTP mail server and kotlin and to this library will be able to integrate mail facility and Android application so that library name is maildroid Mail Droid is a handy library which comes with lot of handy functions that you can use in now to implement mailing facility in your Android application so in this blog post we will be looking at those functions with life source code examples and also source code of this examples are given below just copy paste and also the demo of this application is given below so I will see the screenshot and also try to extend this application accordingly to your needs.

 

Demo of Application

 

 

As we can see in the demo application we are sending emails through a client called as mailtrap In this tutorial I will be using a special website which is used to send and receive emails for developers who are wanting to include some mailing functionality in their application to the web servers that service name is mailtrap so you can just go to its official website from here so just signup form in for a new account and then you can use this service for development purpose and once your application is ready you can just move on to gmail.com for other SMTP server that you are using in our application

READ  Kotlin RecyclerView Animation Example - Coding Shiksha

 

Key Features

  • Sending emails using SMTP protocol
  • Compatible with all smtp providers
  • Sending HTML/CSS styled emails
  • Library is using Java Mail API that is well known as best library for sending emails

 

Include the Library

 

Maildroid is hosted on JitPack and it’s quite easy to integrate in to your project. Maildroid requires at least Android API level 19 Android KitKatHow you want to include this library in your project

Gradle
Maven

 

For Gradle

allprojects {
  repositories {
   ...
   maven { url 'https://jitpack.io' }
  }
 }

For Maven

<repositories>
   <repository>
       <id>jitpack.io</id>
       <url>https://jitpack.io</url>
    </repository>
</repositories>

 

Using this Library

 

MaildroidX.Builder()
            .smtp("")
            .smtpUsername("")
            .smtpPassword("")
            .smtpAuthentication()
            .port("")
            .type("")
            .to("")
            .from("")
            .subject("")
            .body("")
            .attachment("")
            .onCompleteCallback(object : MaildroidX.onCompleteCallback{
                override fun onSuccess() {
                    //Place for your code when email is sent successfully!
                }
                override fun onFail() {
                   //Place for your code when email is not sent!
                }
            },3000)
            .mail()

 

As you can see in the above code snippet we are using the maildroid Builder method so in this builder method you need to provide some arguments such as the from address and what is address that you are sending this mail to and if you are including any kind of attachment such as PDF file or any kind of file you need to put full path of that file and also you need to specify the message subject and there is a field called as body we need to specify the body of the email also and there are two callback functions available in this method so when the mail has been successfully sent then and on complete callback function will be fired and in that callback function you can write anything you want when you want to print out something that your mail has been sent and on the other side there is also a fail call back so when the mail is not sent successfully then you can print out something like that that the mail is not sent properly so you can handle all kind of situations in this two callback functions and lastly we can also provide some time limit and in this case we are providing 3 seconds 3 millisecond wait so this will wait 3 seconds and then it will send your mail and lastly attaching .mail() function will actually send the email to the client that is mailtrap.

READ  Kotlin PDF Viewer Library Example - Coding Shiksha

 

Full Example Source Code

 

  1. Layout File:

 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">


    <EditText
            android:layout_width="347dp"
            android:layout_height="53dp"
            android:inputType="textEmailAddress"
            android:hint="E-mail"
            android:id="@+id/mail"
            app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp" app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="70dp"
            android:layout_marginBottom="11dp" app:layout_constraintBottom_toTopOf="@+id/text"
            app:layout_constraintVertical_bias="1.0"/>

    <EditText
            android:layout_width="340dp"
            android:layout_height="112dp"
            android:inputType="textEmailAddress"
            android:hint="Text"
            android:scrollIndicators="right"
            android:scrollbars="horizontal"
            android:id="@+id/text" android:layout_marginEnd="8dp"
            app:layout_constraintEnd_toEndOf="parent" android:layout_marginRight="8dp" android:layout_marginStart="8dp"
            app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="8dp"
            app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toBottomOf="@+id/text"
            android:layout_marginBottom="415dp" app:layout_constraintHorizontal_bias="0.472"
            app:layout_constraintVertical_bias="1.0"/>
    <Button
            android:text="Send mail"
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:id="@+id/sendBTN" android:layout_marginTop="8dp"
            app:layout_constraintTop_toBottomOf="@+id/text" android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintVertical_bias="0.109"
            app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="34dp"
            android:layout_marginStart="34dp" android:layout_marginEnd="34dp" android:layout_marginRight="34dp"
            app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0"/>

</androidx.constraintlayout.widget.ConstraintLayout>

2) Kotlin Code

READ  How to make Smart Custom Alert Dialog in Kotlin - Coding Shiksha

 

package co.nedim.maildroid

import android.app.ProgressDialog
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import co.nedim.maildroidx.*
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val toMail: EditText = findViewById(R.id.mail)
        val text:EditText = findViewById(R.id.text)
        val send:Button = findViewById(R.id.sendBTN)

        send.setOnClickListener(View.OnClickListener {
            val recipient = toMail.text.toString()
            val text = text.text.toString()

            if(recipient.isNotEmpty() || text.isNotEmpty()){
                send(recipient,text)
            }else{
                Toast.makeText(this@MainActivity,"Please check your input",Toast.LENGTH_SHORT).show()
            }
        })
    }
    private fun sendDsl(to:String,text:String) {
        sendEmail {
            context(this@MainActivity)
            smtp("smtp.mailtrap.io")
            smtpUsername("")
            smtpPassword("")
            smtpAuthentication(true)
            port("465")
            type(MaildroidXType.HTML)
            to(to)
            from("someoneover@interenet.com")
            subject("Hello v1")
            body(text)
            callback {
                timeOut(3000)
                onSuccess {
                    Toast.makeText(this@MainActivity,"Mail sent!",Toast.LENGTH_SHORT).show()
                    Log.d(TAG,  "SUCCESS")
                }
                onFail {
                    Toast.makeText(this@MainActivity,"Error!",Toast.LENGTH_SHORT).show()
                    Log.d(TAG,  "ERROR")
                }
            }
        }
    }
    fun send(to:String,text:String){
        val pd:ProgressDialog = ProgressDialog(this@MainActivity)
        pd.setTitle("Send email")
        pd.setMessage("Sending...")
        pd.show()
        MaildroidX.Builder()
            .context(this@MainActivity)
            .smtp("smtp.mailtrap.io")
            .smtpUsername("")
            .smtpPassword("")
            .smtpAuthentication(true)
            .port("2525")
            .type(MaildroidXType.HTML)
            .to(to)
            .from("someoneover@interenet.com")
            .subject("Hello v1")
            .body(text)
            .attachment(this@MainActivity.filesDir.path +  "/abc.txt")
            .onCompleteCallback(object : MaildroidX.onCompleteCallback{
                override val timeout: Long = 3000

                override fun onSuccess() {
                    Toast.makeText(this@MainActivity,"Mail sent!",Toast.LENGTH_SHORT).show()
                    pd.cancel()
                }
                override fun onFail() {
                    Toast.makeText(this@MainActivity,"Error!",Toast.LENGTH_SHORT).show()
                    pd.cancel()
                }
            })
            .mail()
    }
    companion object {
        val TAG = MainActivity::class.java.name
    }
}

Leave a Reply

Close Menu

Indian Desi Tools - Ultimate Tools Website

Demo Description


This will close in 20 seconds