this is the code to send a mail with attachment
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class sendemail extends Activity {
Button send;
EditText address, subject, emailtext;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main11);
send = (Button) findViewById(R.id.emailsendbutton);
address = (EditText) findViewById(R.id.emailaddress);
subject = (EditText) findViewById(R.id.emailsubject);
emailtext = (EditText) findViewById(R.id.emailtext);
final TextView tv = (TextView)findViewById(R.id.fileContent);
send.setOnClickListener(new OnClickListener() {
// TextView tv = (TextView)findViewById(R.id.fileContent);
public void onClick(View v) {
// TODO Auto-generated method stub
if(!address.getText().toString().trim().equalsIgnoreCase("")){
// Toast.makeText(getApplicationContext(), "Please enter an
email address..", Toast.LENGTH_LONG).show();
File dir = Environment.getExternalStorageDirectory();
File file = new
File(dir,"download/phonedata.txt");
if (file.exists())
try {
Log.i(getClass().getSimpleName(), "send task - start");
final Intent emailIntent = new Intent(
android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
// emailIntent.setType("application/octet-stream");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new
String[] { address.getText().toString() });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
subject.getText());
emailIntent.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file://" + file));
Toast.makeText(sendemail.this, "File successfully attached
in your Mail!!!",
Toast.LENGTH_LONG).show();
tv.setText("File Successfully attached in your mail!!!");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
emailtext.getText());
sendemail.this.startActivity(Intent
.createChooser(emailIntent, "Send mail..."));
} catch (Throwable t) {
Toast.makeText(sendemail.this, "Request failed: " +
t.toString(),
Toast.LENGTH_LONG).show();
}
else
{
tv.setText("Sorry file doesn't exist!!!");
Toast.makeText(sendemail.this, "File not found in
sd-card....Please upload it from Server!!!",
Toast.LENGTH_LONG).show();
//Toast.makeText(getApplicationContext(), "Please enter
an email address..", Toast.LENGTH_LONG).show();
}
}
else
{
Toast.makeText(getApplicationContext(), "Please enter an
email address..", Toast.LENGTH_LONG).show();}
}
});
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------
xml file
<?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://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#6699FF">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text=""
android:id="@+id/fileContent" />
<LinearLayout android:id="@+id/LinearLayout02"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content" android:width="170dip"
android:id="@+id/emailaddress"></EditText>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/emailaddress"
android:text="Email address"></TextView>
</LinearLayout>
<LinearLayout android:id="@+id/LinearLayout03"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content" android:width="170dip"
android:id="@+id/emailsubject"></EditText>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/emailsubject"
android:text="Subject"></TextView>
</LinearLayout>
<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content" android:lines="5"
android:width="300dip"
android:id="@+id/emailtext"></EditText>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/emailsendbutton"
android:text="Send!" android:width="150dip"></Button>
</LinearLayout>
------------------------------------------------------------------------------------------------------------------------------------------------
dont forget to add internet permission in your android manifest file
PS for sending a mail test this code on real device not on emulator
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en