Showing posts with label Without using intent. Show all posts
Showing posts with label Without using intent. Show all posts

Tuesday, 23 December 2014

Send Email from your Application without using intent in Android

Today I will show you how to send E-mail from your Application without using intent and without re-direct in any mail application.

You can download all necessary files from HEAR.




Step - 1: Import 3 jar file in your application lib folder You can get all jar from witch are given in Download Link.

Step - 2: You need to add 2 permission in your Manifest file

1.android.permission.INTERNET
2. android.permission.ACCESS_NETWORK_STATE

Step - 3: Now add 2 Java files GMailSender.java and JSSEProvider.java both file you can get from download link.

MainActivity.Java

package com.example.mailtry;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    EditText etSender, etBody, etPassword, etSubject, etTo;

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

        etSender = (EditText) findViewById(R.id.etSender);
        etBody = (EditText) findViewById(R.id.etBody);
        etPassword = (EditText) findViewById(R.id.etPassword);
        etSubject = (EditText) findViewById(R.id.etSubject);
        etTo = (EditText) findViewById(R.id.etTo);
        Button btnSend = (Button) findViewById(R.id.btnSend);
        btnSend.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (etSender.getText().toString().trim() != ""
                        && etPassword.getText().toString().trim() != ""
                        && etTo.getText().toString().trim() != ""
                        && etSubject.getText().toString().trim() != "") {

                new Thread(new Runnable() {
                    public void run() {
                        try {
                            GMailSender sender = new GMailSender(
                                    etSender.getText().toString().trim(), etPassword.getText().toString().trim());

                            //------  You can attech attechment from below code also -----//
                            // sender.addAttachment(Environment.getExternalStorageDirectory().getPath()+"/image.jpg");
                            sender.sendMail(etSubject.getText().toString().trim(),
                                    etBody.getText().toString().trim(),
                                    etSender.getText().toString().trim(),
                                    etTo.getText().toString().trim());

                          

                        } catch (Exception e) {
                        }
                    }
                }).start();
            }
                Toast.makeText(getApplicationContext(),"Your mail has been sent",Toast.LENGTH_LONG).show();
              
            }
        });

    }

}





activity_mail.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.mailtry.MainActivity"
    android:orientation="vertical">
   
    <EditText
        android:id="@+id/etSender"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:hint="Sender"
        android:layout_marginBottom="10dp"/>
   
    <EditText
        android:id="@+id/etPassword"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:hint="Password"
        android:layout_marginBottom="10dp"/>
   
    <EditText
        android:id="@+id/etTo"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:hint="To"
        android:layout_marginBottom="10dp"/>
   
    <EditText
        android:id="@+id/etSubject"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:hint="Subject"
        android:layout_marginBottom="10dp"/>
   
    <EditText
        android:id="@+id/etBody"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:hint="Body"
        android:layout_marginBottom="10dp"/>

    <Button
        android:id="@+id/btnSend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="send" />

</LinearLayout>


You can download all necessary files from HEAR.

I hope this will help you,
Happy Coding...