Is it possible to send email in Android without using a email client like gmail, hotmail, etc. Can I send a email directly with the Intent class only?
Right now I'm using the emulators build in email app.
Also how can I let the user attach a file to the email in an easy way? I want to use a button that opens a folder with images on the emulator and lets the user to pick a file to attach.
Here is my code:
public class MainActivity extends ActionBarActivity {
private EditText textEmail;
private EditText textSubject;
private EditText textMessage;
private Button btnSend, btnAttach;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textEmail = (EditText)findViewById(R.id.editText);
textSubject = (EditText)findViewById(R.id.editText2);
textMessage = (EditText)findViewById(R.id.editText3);
btnSend = (Button)findViewById(R.id.button);
btnAttach =(Button)findViewById(R.id.button2);//not implemented yet
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SendEmail();
textEmail.setText("");
textSubject.setText("");
textMessage.setText("");
}
});
}
protected void SendEmail(){
String toEmail = textEmail.getText().toString();
String theSubject = textSubject.getText().toString();
String theMessage = textMessage.getText().toString();
Intent email = new Intent(Intent.ACTION_SEND);
email.setData(Uri.parse("mailto:"));
email.putExtra(Intent.EXTRA_EMAIL,toEmail);
email.putExtra(Intent.EXTRA_SUBJECT,theSubject);
email.putExtra(Intent.EXTRA_TEXT,theMessage);
email.setType("message/rfc822");
// the user can choose the email client
startActivity(Intent.createChooser(email, "Send email"));
}
}