Saturday, December 25, 2010

Send email with attached file in Android

Hello Friends,

Today i have discussed about how to send email with attached file in android.

In android u have to give internet permission for android manifest file.

And this application work only from device not in android emulator... so this think is very important... And Please Refer this Android Application Website.

And source code given below.

main.xml

<?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">

<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>



And java file given also below.

Email.java

package com.fb

import java.io.file;

import android.app.Activity;

import android.net.Uri;

import android.os.Bundle;

import 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;

public class Email 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.main);

send = (Button) findViewById(R.id.emailsendbutton);

address = (EditText) findViewById(R.id.emailaddress);

subject = (EditText) findViewById(R.id.emailsubject);

emailtext = (EditText) findViewById(R.id.emailtext);

send.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

final Intent emailIntent = new Intent(

android.content.Intent.ACTION_SEND);

emailIntent.setType("image/png");

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,

emailtext.getText());

emailIntent.putExtra(Intent.EXTRA_STREAM, Uri

.parse("android.resource://"

+ getPackageName() + "/" + R.drawable.icon));

FB.this.startActivity(Intent.createChooser(emailIntent,

"Send mail..."));

}

});

}

}

So this are the simple code for send email with attcahed file in android

I think u have enjoyed this code.

20 comments:

  1. i tested it on my htc legend but had to make some changes to let it operate:

    code:

    package com.fb;

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

    public class Email 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.main);

    send = (Button) findViewById(R.id.emailsendbutton);

    address = (EditText) findViewById(R.id.emailaddress);

    subject = (EditText) findViewById(R.id.emailsubject);

    emailtext = (EditText) findViewById(R.id.emailtext);

    send.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {

    // TODO Auto-generated method stub

    final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

    emailIntent.setType("image/png");

    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, emailtext.getText());

    emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://"+ getPackageName() + "/" + R.drawable.icon));

    Email.this.startActivity(Intent.createChooser(emailIntent,"Send mail..."));

    }

    });

    }

    }

    ReplyDelete
  2. BTW it also misses the subject evertime i try to send email.
    Whenever it leaves the template it goto chosen cliƫnt (in my case i choose for Gmail).

    I also expexted there would be a file chooser instead of a default icon.

    But hey...you do good work.

    I also recommend you use a better code igniter on your website , becouse when you copy/paste it... you must manualy edit each line.

    ReplyDelete
  3. Can you pls POST the code to get Primary account EMail address?

    ReplyDelete
  4. Hi....

    but i want to add image in compose body nt as an attachment......is there any way to add image from sd card into email body.....

    ReplyDelete
  5. I am getting this err:
    "No Applications can perform this action"
    please suggest! thanx

    ReplyDelete
  6. can you please tell me how to check the email delivery report

    ReplyDelete
  7. whoo hoo! Nice. Please help! Vote!

    please vote!! I need everyone's help! http://fluapp.challenge.gov/submissions/3027-cdcs-flu-weekly-sneeze-in-your-sleeve

    or on http://8cupsaday.com click the vote!

    ReplyDelete
  8. I need to append the image in mail body, not as an attachment

    plz help me

    ReplyDelete
  9. hi, can you send post it in the zip file?

    ReplyDelete
  10. This is not working for gmail app in android. How this will work for same?

    ReplyDelete
  11. How we will send images from url in attachment.

    ReplyDelete
  12. but on opening this file image is not opening as image

    ReplyDelete
  13. Hi, this is not working it show error in the line Email.this.startActivity(Intent.createChooser(emailIntent,"Send mail..."));
    please suggest me any idea

    ReplyDelete
  14. no work curecttly because i need another informaion about permission

    ReplyDelete
  15. hi ,
    How can we send image as well as cvs or text file with email attachement.
    Please help me i want to use in my application

    ReplyDelete
  16. STEP BY STEP ANDROID TUTORIALS

    VISIT:-www.androidituts.blogspot.com

    ReplyDelete
  17. add this line also.nw subject also adding
    emailIntent.setType("text/plain");

    ReplyDelete
  18. Hi ,

    I' m new to Android app development. I am trying to create a mobile application that has three entry fields. i.e: Name , Contact Number , Email Address( Receiver's). I want to email the first and second entry of the input data captured to the email address (the third entry ) on click of Send button.

    Thanks
    Mrigang

    ReplyDelete
  19. android application development affiliation is vested with a massive duty to make the best applications for its customers that can offer them some help with amplifying their bit of the pie and access clients. Thusly, we at Webnet, ensure that we make for you the applications that are unimaginably intense in the business division and additionally rather besides offer you some help with rising among your foes

    ReplyDelete
  20. We are the best SEO services company, we find and fix the errors on your website .As a SEO services company, we tend to facilitate brands reach new customers
    through search optimization and social media organic visibility.

    ReplyDelete

How to Free Up Storage Space on Your Android Phone (Without Deleting Photos)

Running out of space on your Android phone is one of the most common (and most annoying) problems out there - especially once you start gett...