email sending image

email send from android application example

Email send from android application example

We already know how send e-mail from Gmail or other mail provider, now we are going to learn how send mail from android application using intent calling. We have taken here an Activity ‘EmailSendingActivity.java’ and the email sending code write under the button click. We have taken here two TextView, one for send mail id and another for mail body. Before sending mail we have to check the email id is valid or not.

Learn How to check email validation in android.

 

Shortcode for email sending is written below.

ShortCode:-

Intent email = new Intent(Intent.ACTION_SEND);

email.putExtra(Intent.EXTRA_EMAIL, new String[]{mail_id});

email.putExtra(Intent.EXTRA_SUBJECT, "Your Subject");

email.putExtra(Intent.EXTRA_TEXT, “mail body”);

email.setType("message/rfc822");

startActivity(Intent.createChooser(email, "Select Email Provider ::"));


EmailSendingActivity.java


package com.androidfromhome.emailsending;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

public class EmailSendingActivity extends Activity implements OnClickListener {

    private EditText et_mail;
    private Button btn_send;
    private EditText et_body;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_email_sending);
        
        et_mail=(EditText)findViewById(R.id.et_email);
        et_body=(EditText)findViewById(R.id.et_body);
        btn_send=(Button)findViewById(R.id.btn_send);
        btn_send.setOnClickListener(this);
    
        
        }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        
        switch (v.getId()) {
        case R.id.btn_send:
            String mail_id=et_mail.getText().toString();
            String mail_body=et_body.getText().toString();
            
            if (!mail_body.equalsIgnoreCase("")&& !mail_id.equalsIgnoreCase("")) {
                
                if (isEmailValid(mail_id)) {
                
                    Intent email = new Intent(Intent.ACTION_SEND);
                    email.putExtra(Intent.EXTRA_EMAIL, new String[]{mail_id});          
                    email.putExtra(Intent.EXTRA_SUBJECT, "Your Subject");
                    email.putExtra(Intent.EXTRA_TEXT, mail_body);
                    email.setType("message/rfc822");
                    startActivity(Intent.createChooser(email, "Select Email Provider ::"));
    
                    
                }
                
            }
            
            break;

        default:
            break;
        }
        
    }

    
    
    public static boolean isEmailValid(String email) {
        boolean isValid = false;
        String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
        CharSequence inputStr = email;
        Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(inputStr);
        if (matcher.matches()) {
            isValid = true;
        }
        return isValid;
    }
}

 


activity_email_sending.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/LinearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#A758AB"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="15dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="2dp"
            android:text="Android From Home"
            android:textColor="#ffffff"
            android:textSize="22dp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/et_email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="#dfdfdf"
            android:ems="10"
            android:hint="Send To: mail"
            android:inputType="textEmailAddress"
            android:padding="10dp" />

        <EditText
            android:id="@+id/et_body"
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:layout_margin="10dp"
            android:background="#dfdfdf"
            android:ems="10"
            android:hint="Mail Body"
            android:padding="1dp" >

            <requestFocus />
        </EditText>

        <Button
            android:id="@+id/btn_send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#6FB35C"
            android:padding="10dp"
            android:text="Send"
            android:textColor="#ffffff"
            android:textStyle="bold" />

    </LinearLayout>

</LinearLayout>


 

Download Full Source Code

Related Post:-

Android Camera example

Use Activity as dialog in android

How Retrieve contact list in android

Switching One activity  to another activity in Android by intent

Leave a Reply

Your email address will not be published. Required fields are marked *

Get Widget