Thursday, 23 January 2014

How email in android programmatically.

In Today's Tutorial I am going to explain you how to you can mail the details to any email id. The email is generally used when we want to have feedback from the application user. like in contact us form or feedback form.

In this project I am creating a contact us form where i will get the name, address, mobile no, company name, email from the user and will send it to a particular email id.

So lets start.

1. create a XML layout file like this one ..

<?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:background="#659b40"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/frameLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#555555" >

        <ScrollView
            android:id="@+id/ScrollView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingLeft="30dp"
                android:paddingRight="30dp"
                android:paddingTop="15dp" >

                <EditText
                    android:id="@+id/yourname"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="20dp"
                    android:background="@drawable/buttonstyle"
                    android:ems="10"
                    android:hint="Enter Name"
                    android:paddingBottom="5dp"
                    android:paddingLeft="20dp"
                    android:paddingRight="30dp"
                    android:paddingTop="5dp"
                    android:textColorHint="#000000"
                    android:textSize="25sp" >

                    <requestFocus />
                </EditText>

                <EditText
                    android:id="@+id/yournumber"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="10dp"
                    android:layout_marginTop="10dp"
                    android:background="@drawable/buttonstyle"
                    android:ems="10"
                    android:hint="Enter Contact No"
                    android:inputType="phone"
                    android:paddingBottom="5dp"
                    android:paddingLeft="20dp"
                    android:paddingTop="5dp"
                    android:textColorHint="#000000"
                    android:textSize="25sp" />

                <EditText
                    android:id="@+id/youremail"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/buttonstyle"
                    android:ems="10"
                    android:hint="Enter Email Id"
                    android:inputType="textEmailAddress"
                    android:paddingBottom="5dp"
                    android:paddingLeft="20dp"
                    android:paddingTop="5dp"
                    android:textColorHint="#000000"
                    android:textSize="25sp" />

                <EditText
                    android:id="@+id/company_name"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:background="@drawable/buttonstyle"
                    android:ems="10"
                    android:hint="Company Name"
                    android:paddingBottom="5dp"
                    android:paddingLeft="20dp"
                    android:paddingTop="5dp"
                    android:textColorHint="#000000"
                    android:textSize="25sp" />

                <EditText
                    android:id="@+id/conatct_address"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:background="@drawable/buttonstyle"
                    android:ems="10"
                    android:hint="Address"
                    android:paddingBottom="5dp"
                    android:paddingLeft="20dp"
                    android:paddingTop="5dp"
                    android:textColorHint="#000000"
                    android:textSize="25sp" />

                <Button
                    android:id="@+id/yoursubmiot"
                    android:layout_width="fill_parent"
                    android:layout_height="50dp"
                    android:layout_marginTop="20dp"
                    android:background="@drawable/black_button"
                    android:text="Submit"
                    android:textColor="#999999"
                    android:textSize="20sp"
                    android:textStyle="bold" />
            </LinearLayout>

        </ScrollView>

    </FrameLayout>

</LinearLayout>

2. Now comes the coding part..... so lets start.

  firstly we will initialize the edit text's and submit button

        submit=(Button)findViewById(R.id.yoursubmiot);
        Name=(EditText)findViewById(R.id.yourname);
        contactno=(EditText)findViewById(R.id.yournumber);
        emailid=(EditText)findViewById(R.id.youremail);
        company_name=(EditText)findViewById(R.id.company_name);
        address=(EditText)findViewById(R.id.conatct_address);

Second now on submit button set onclick listener and put the following code in that


                        name = Name.getText().toString();
                        contact = contactno.getText().toString();
                        email = emailid.getText().toString();
                        company = company_name.getText().toString();
                        addr=address.getText().toString();

                        String to = "itdevelopersdelhi@gmail.com";
                        String message = "Name Of Customer   "
                            + name   +"\n"
                            + "      contact no       "
                            + contact
                            + ".....email id ..........."
                            + email
                            + ".........Company name........."
                            + company+"........"
                            + ".........Address........"
                            + addr
                            +".............";
                       
                        Intent email = new Intent(Intent.ACTION_SEND);
                        email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
                        email.putExtra(Intent.EXTRA_SUBJECT, "Customer Query ");
                        email.putExtra(Intent.EXTRA_TEXT, message);
                        email.setType("message/rfc822");
                        startActivity(Intent.createChooser(email, "Choose an Email client :"));
                     }
                  
In the above code we have implemented the intent and the action on intent is set to action send. and added the email id ,subject, and text. In text we have added all details which we have got from the user since it is of string type.

The Intent is set to the message/rfc822 . and at last we will start the activity.

In the project I have added added few more thinks. which you can see after downloading the project the project download link is given below.

Snapshots:-









https://adf.ly/cVYLmDownload the project

1 comment:

  1. If you’re in Lahore, go online and search for sites that would help you look for the best SEO company in Lahore. When on your search, you can’t just rely on anyone can you? So, you must search your options thoroughly, so you don’t get into any sort of fraud.
    I find awesome knowledge about Seo company in pakistan on this blog.
    Keep Sharing. Thanks!

    ReplyDelete