Reputation: 651
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"));
}
}
Upvotes: 0
Views: 335
Reputation: 163
Here's a previous discussion on this topic.
ehd's answer relies on a 3rd party java library mail wrapper. If you do not trust that wrapper, consider adding standard Linux mail utility (i.e., sendMail) to your project and creating JNI layer to communicate with it.
Upvotes: 0
Reputation: 99
With the Intent class only no, but why reinvent the wheel when other apps like gmail and hotmail specialize in only sending emails? Those clients will also handle the case of attaching a file to the email, but if you are looking to handle that yourself and passing it along with the email intent look at Google's tutorial on sharing files.
Upvotes: 1
Reputation: 30088
Short answer - no. The Intent requests that the OS run an application that has registered for it. If you don't have an email app, then there will by definition be no application to handle your Intent.
Upvotes: 0