Pages

Monday, July 23, 2012

How to send email from and ANDROID Application programatically?

Let’s go to the code fast……
This is the code in the mail java file….
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<span class="IL_AD" id="IL_AD10">package</span> com.coderzheaven;
 
<span class="IL_AD" id="IL_AD9">import</span> 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;
import android.widget.Toast;
 
public class sendMailDemo extends Activity {
    Button send;
    EditText address, subject, emailbody;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        address = (EditText) findViewById(R.id.address);
        subject = (EditText) findViewById(R.id.subject);
        emailbody = (EditText) findViewById(R.id.body);
        send = (Button) findViewById(R.id.send);
 
        send.setOnClickListener(new OnClickListener() {
            @Override
            public void <span class="IL_AD" id="IL_AD8">onClick</span>(View v) {
                sendEmail();
            }
        });
    }
 
    public void sendEmail(){
 
        if(!address.getText().toString().trim().equalsIgnoreCase("")){
          final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
          emailIntent.setType("plain/text");
          emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ address.getText().toString()});
          emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject.getText());
          emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailbody.getText());
          sendMailDemo.this.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
        }
        else{
            Toast.makeText(getApplicationContext(), "Please enter <span class="IL_AD" id="IL_AD2">an email address</span>..", Toast.LENGTH_LONG).show();
        }
      }
    }
Now the layout file (main.xml)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://<span class="IL_AD" id="IL_AD7">schemas</span>.android.com/apk/res/android"
    android:<span class="IL_AD" id="IL_AD11">orientation</span>="<span class="IL_AD" id="IL_AD12">vertical</span>"
    android:background="@drawable/android"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/<span class="IL_AD" id="IL_AD1">emailaddress</span>"
        android:text="Email Address"
        android:textStyle="bold">
    </TextView>
 
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="250dip"
        android:hint="email address"
        android:id="@+id/address">
    </EditText>
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Subject"
        android:textStyle="bold">
    </TextView>
 
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="250dip"
        android:hint="Subject"
        android:id="@+id/subject">
    </EditText>
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Your Message"
        android:textStyle="bold">
    </TextView>
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:lines="5"
        android:hint="Your message here!!"
        android:id="@+id/body">
    </EditText>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/send"
        android:text="Send Email"
        android:width="150dip">
    </Button>
</LinearLayout>
The AndroidManifest.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.coderzheaven"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="Send Mail Demo">
        <activity android:name=".sendMailDemo"
                  android:label="Send Mail Demo">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
Note: However if you test this in your emulator, it will not work. Install it in your device to test it.

No comments:

Post a Comment

Popular Posts