Android send email using intent | Email with attach text

How to send email in android studio using intent

Send email from you own application. Sending mail help you also in sending user 
feedback to developer. Set your default feedback email to send instant email just type 
user comment or problem in your application. Android send email is easy followed by 
"message/rfc822" this will fetch all the email sending browser. Learn more about sending
email from Android official page.

package write your package name here;

import android.os.Bundle;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    
    EditText mailTo,mailSubject,mailMessage;
    Button sendEmail;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mailTo = (EditText)findViewById(R.id.editText1);
        mailSubject = (EditText)findViewById(R.id.editText2);
        mailMessage = (EditText)findViewById(R.id.editText3);

        sendEmail = (Button)findViewById(R.id.sendMail);
        sendEmail.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                
                //Get and Set editText value in String.
                String to = mailTo.getText().toString();
                String subject = mailSubject.getText().toString();
                String message = mailMessage.getText().toString();
                
                Intent email = new Intent(Intent.ACTION_SEND);
                email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
                email.putExtra(Intent.EXTRA_SUBJECT, subject);
                email.putExtra(Intent.EXTRA_TEXT, message);

                //This will show prompts of email intent
                email.setType("message/rfc822");
                startActivity(Intent.createChooser(email, "Choose an Email sender :"));
            }

        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // For menu in Title bar
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }
}

Visit NextGenEarn Blog for all new code snippets no deprecated code.

Add a Comment

Your email address will not be published. Required fields are marked *