Hello Friends,
Here i am back, today i am going to share a code with you people "How to send email with Gmail / E-Mail client from Android mobile" find the screen short mention below:
Example:
Click on Attachment button
Check Android Apps on Google Play
https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en
Here i am back, today i am going to share a code with you people "How to send email with Gmail / E-Mail client from Android mobile" find the screen short mention below:
Example:
Codes:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="@string/name"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:ems="10"
android:hint="@string/phone"
android:inputType="number" />
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:ems="10"
android:hint="@string/email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/msg"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:ems="10"
android:gravity="top"
android:hint="@string/message"
android:inputType="textMultiLine" />
<Button
android:id="@+id/attachment"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="@string/attachment" />
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
android:text="@string/submit" />
</LinearLayout>
</LinearLayout>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="@string/name"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:ems="10"
android:hint="@string/phone"
android:inputType="number" />
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:ems="10"
android:hint="@string/email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/msg"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:ems="10"
android:gravity="top"
android:hint="@string/message"
android:inputType="textMultiLine" />
<Button
android:id="@+id/attachment"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="@string/attachment" />
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
android:text="@string/submit" />
</LinearLayout>
</LinearLayout>
MainActivity.java
package in.androiddevelopmentanddiscussione.sendemailwithattachment;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
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 implements OnClickListener{
EditText name,phone,email,msg;
String name1,phone1,email1,msg1,recipient,subject,attachmentFile;
Button submit,attachment;
Uri URI = null;
private static final int PICK_FROM_GALLERY = 101;
int columnIndex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText)findViewById(R.id.name);
phone = (EditText)findViewById(R.id.phone);
email = (EditText)findViewById(R.id.email);
msg = (EditText)findViewById(R.id.msg);
submit = (Button)findViewById(R.id.submit);
attachment = (Button)findViewById(R.id.attachment);
submit.setOnClickListener(this);
attachment.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == attachment) {
openGallery();
}
if (v == submit) {
recipient = "Your Email ID here";
subject = "Mail Form Android App";
try {
name1 = name.getText().toString();
phone1 = phone.getText().toString();
email1 = email.getText().toString();
msg1 = msg.getText().toString();
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] { recipient });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,subject);
if (URI != null) {
emailIntent.putExtra(Intent.EXTRA_STREAM, URI);
}
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Name: "+name1+"\n\nPhone No.: "+phone1+"\n\nEmail Id: "+email1+"\n\nMessage: \n"+msg1);
this.startActivity(Intent.createChooser(emailIntent,"Sending email..."));
} catch (Throwable t) {
Toast.makeText(this,"No email client installed." + t.toString(),Toast.LENGTH_LONG).show();
}
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_FROM_GALLERY && resultCode == RESULT_OK) {
/**
* Get Path
*/
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
columnIndex = cursor.getColumnIndex(filePathColumn[0]);
attachmentFile = cursor.getString(columnIndex);
Log.e("Attachment Path:", attachmentFile);
URI = Uri.parse("file://" + attachmentFile);
cursor.close();
}
}
public void openGallery() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("return-data", true);
startActivityForResult(
Intent.createChooser(intent, "Complete action using"),
PICK_FROM_GALLERY);
}
}
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
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 implements OnClickListener{
EditText name,phone,email,msg;
String name1,phone1,email1,msg1,recipient,subject,attachmentFile;
Button submit,attachment;
Uri URI = null;
private static final int PICK_FROM_GALLERY = 101;
int columnIndex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText)findViewById(R.id.name);
phone = (EditText)findViewById(R.id.phone);
email = (EditText)findViewById(R.id.email);
msg = (EditText)findViewById(R.id.msg);
submit = (Button)findViewById(R.id.submit);
attachment = (Button)findViewById(R.id.attachment);
submit.setOnClickListener(this);
attachment.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == attachment) {
openGallery();
}
if (v == submit) {
recipient = "Your Email ID here";
subject = "Mail Form Android App";
try {
name1 = name.getText().toString();
phone1 = phone.getText().toString();
email1 = email.getText().toString();
msg1 = msg.getText().toString();
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] { recipient });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,subject);
if (URI != null) {
emailIntent.putExtra(Intent.EXTRA_STREAM, URI);
}
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Name: "+name1+"\n\nPhone No.: "+phone1+"\n\nEmail Id: "+email1+"\n\nMessage: \n"+msg1);
this.startActivity(Intent.createChooser(emailIntent,"Sending email..."));
} catch (Throwable t) {
Toast.makeText(this,"No email client installed." + t.toString(),Toast.LENGTH_LONG).show();
}
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_FROM_GALLERY && resultCode == RESULT_OK) {
/**
* Get Path
*/
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
columnIndex = cursor.getColumnIndex(filePathColumn[0]);
attachmentFile = cursor.getString(columnIndex);
Log.e("Attachment Path:", attachmentFile);
URI = Uri.parse("file://" + attachmentFile);
cursor.close();
}
}
public void openGallery() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("return-data", true);
startActivityForResult(
Intent.createChooser(intent, "Complete action using"),
PICK_FROM_GALLERY);
}
}
Manifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
Download full project from here:
Check Android Apps on Google Play
https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en