Monday, 24 August 2015

Android Programming Tutorials 4: Sending Email Through Android

How to sent E-mail through android programming: 

In Tutorial we are going to see how to sent the email by using android programming

Create Android Application:

  • File >> New >> Android Application
  • Enter Project name: SendingEmailApp
  • Package: com.ambilpursunil.newapp
  • Keep other default selections, click Next until you reach Finish

2.Copy and past the following code in the  MainAcivity.java

package com.ambilpursunil.newapp;

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

public class Email extends Activity implements View.OnClickListener {

            EditText personsEmail, intro, personsName, stupidThings, hatefulAction,
                                    outro;
            String emailAdd, beginning, name, stupidAction, hatefulAct, out;
            Button sendEmail;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                        // TODO Auto-generated method stub
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.email);
                        initializeVars();
                        sendEmail.setOnClickListener(this);
            }
            private void initializeVars() {
                        // TODO Auto-generated method stub
                        personsEmail = (EditText) findViewById(R.id.editText1);
                        intro = (EditText) findViewById(R.id.editText2);
                        personsName = (EditText) findViewById(R.id.editText3);
                        stupidThings = (EditText) findViewById(R.id.editText4);
                        hatefulAction = (EditText) findViewById(R.id.editText5);
                        outro = (EditText) findViewById(R.id.editText6);
                        sendEmail = (Button) findViewById(R.id.button1);
            }

            public void onClick(View v) {
                        // TODO Auto-generated method stub
                        convertEditTextVarsIntoStringsAndYesThisIsAMethodWeCreated();
                        String emailaddress[] = { emailAdd };
                        String message = "Well hello "
                        + name
                        +" I just wanted to say "
                        + beginning
                        + ".  Not only that but I hate when you "
                        + stupidAction
             + ", that just really makes me crazy.  I just want to make you "
                        + hatefulAct
                        + ".  Welp, thats all I wanted to chit-chatter about, oh and"
                        + out
            + ".  Oh also if you get bored you should check out www.mybringback.com"
            + '\n' + "PS. I think I love you...   :(";
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, emailaddress);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "i hate you");
                        emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
                        startActivity(emailIntent);
                         }
            private void convertEditTextVarsIntoStringsAndYesThisIsAMethodWeCreated()                          {
                        // TODO Auto-generated method stub
                        emailAdd = personsEmail.getText().toString();
                        beginning = intro.getText().toString();
                        name = personsName.getText().toString();
                        stupidAction = stupidThings.getText().toString();
                        hatefulAct = hatefulAction.getText().toString();
                        out = outro.getText().toString();
                      }
            @Override
            protected void onPause() {
                        // TODO Auto-generated method stub
                        super.onPause();
                        finish();
            }
}

2.Make the change the of  activity_main.xml  file as the below

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

    <!-- This xml is to send Email to other prot -->

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="14" >

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

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hateful Intro:" />

            <EditText
                android:id="@+id/editText1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10" >

                <requestFocus />
            </EditText>

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Email address(es):" />

            <EditText
                android:id="@+id/editText2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10" />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Person&apos;s name" />

            <EditText
                android:id="@+id/editText3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10" />

            <TextView
                android:id="@+id/textView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" Stupid Things that this Person does" />

            <EditText
                android:id="@+id/editText4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10" />

            <TextView
                android:id="@+id/textView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="What you want to do to this person:" />

            <EditText
                android:id="@+id/editText5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10" />

            <TextView
                android:id="@+id/textView6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hateful Outro" />

            <EditText
                android:id="@+id/editText6"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:ems="10" />
        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="46"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="Send Email" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="40"
        android:orientation="vertical" >

        <AnalogClock
            android:id="@+id/analogClock1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>
</LinearLayout>
</LinearLayout>


3.Copy and past the manifest.xml   file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ambilpursunil.newapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true" 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

    <activity 
       android:name="com.ambilpursunil.newapp.Email"
       android:configChanges="keyboard|keyboardHidden|orientation>

   <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>

       </activity>
  </application>

</manifest>


4.Right click on the project and Run As-->Android Application 




OutPut Explanation:

In this application there are 6 pair of text view and edit text with analog watch and a button Just enter all the field and click on the sent button, It will redirect to In-build gmail icon of your mobile and see the body of the mail if u does not like the body of the mail you can change and click on sent button of Gmail.


Please Send Your Comments To ambilpura.sunil@gmail.com


Stay Tune For Next Tutorial...Sound Effects in Android:

No comments:

Post a Comment