Java (Android) Send Mail with Gmail SMTP [NEW] with Gmail-Sender Library – Coding Shiksha

Java (Android) Send Mail with Gmail SMTP [NEW] with Gmail-Sender Library – Coding Shiksha

Welcome back with the another blog post send this blog post we will be integrating a library in java through which you can be sending Gmail mail in your Android application so that library name is Gmail Sender library which allows you to send SMTP mails through your Gmail account if you want to integrate this functionality in your Android application so let’s get started with this post and all the source code of this application is also listed below so just copy source code and also don’t forget to extend the source code full fully understand it and then extend the source codes accordingly to your needs.

READ  Java(Android) Circular Progress Bar in App [New] - Coding Shiksha

 

Setup

  1. Add to build.gradle in app level

 

implementation 'com.github.tntkhang:gmail-sender-library:1.2.0'

 

  1. How to use it

 

 

GMailSender.withAccount("your-email@gmail.com", "email-password")
                .withTitle(title)
                .withBody(body)
                .withSender(getString(R.string.app_name))
                .toEmailAddress(emailAddress) // one or multiple addresses separated by a comma
                .withListenner(new GmailListener() {
                    @Override
                    public void sendSuccess() {
                        Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void sendFail(String err) {
                        Toast.makeText(MainActivity.this, "Fail: " + err, Toast.LENGTH_SHORT).show();
                    }
                })
                .send();
  1. IMPORTANT ! Enable less secure for the email using to sent email: https://support.google.com/accounts/answer/6010255?hl=en
READ  Kotlin Date time picker using JRDateTimePicker

 

Full Example Source Code

 

  1. Layout File

 

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


    <android.support.design.widget.TextInputLayout
        android:id="@+id/til_email_address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/_10sdp"
        android:inputType="textEmailAddress">

        <EditText
            android:id="@+id/edt_email_address"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_email"/>

    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/til_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/_10sdp"
        android:inputType="textEmailSubject">

        <EditText
            android:id="@+id/edt_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/title"
            android:hint="@string/hint_title"/>

    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/til_body"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/_10sdp"
        android:inputType="textMultiLine">

        <EditText
            android:id="@+id/edt_body"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/body"
            android:hint="@string/hint_body"/>

    </android.support.design.widget.TextInputLayout>

    <Button
        android:id="@+id/btn_send"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_20sdp"
        android:layout_margin="@dimen/_10sdp"
        android:text="@string/send_mail"/>

</LinearLayout>

 

2) Java Code

 

package com.github.gmailsenderexample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;
import android.widget.Toast;

import com.github.tntkhang.gmailsenderlibrary.GMailSender;
import com.github.tntkhang.gmailsenderlibrary.GmailListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.edt_email_address)
    EditText edtEmailAddress;

    @BindView(R.id.edt_body)
    EditText edtBody;

    @BindView(R.id.edt_title)
    EditText edtTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);


    }

    @OnClick(R.id.btn_send)
    void sendEmail() {
        String emailAddress = edtEmailAddress.getText().toString(); // abc@gmail.com,edgh@gmail.com,xyz@gmail.com
        String title = edtTitle.getText().toString();
        String body = edtBody.getText().toString();

        GMailSender.withAccount("your-email@gmail.com", "email-password")
                .withTitle(title)
                .withBody(body)
                .withSender(getString(R.string.app_name))
                .toEmailAddress(emailAddress) // one or multiple addresses separated by a comma
                .withListenner(new GmailListener() {
                    @Override
                    public void sendSuccess() {
                        Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void sendFail(String err) {
                        Toast.makeText(MainActivity.this, "Fail: " + err, Toast.LENGTH_SHORT).show();
                    }
                })
                .send();

    }
}

 

Leave a Reply

Close Menu

Indian Desi Tools - Ultimate Tools Website

Demo Description


This will close in 20 seconds