How to Send Emails from My Android Application

How to send emails from my Android application?

The best (and easiest) way is to use an Intent:

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"recipient@example.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT , "body of email");
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MyActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}

Otherwise you'll have to write your own client.

how send email from android application directly?

There are two ways to send email from android application directly without any intent

  1. using SMTP & JavaMail API follow the link for details
  2. using Webservice( for example PHP script ) so there is a server side code and you hit that URL with param like ( name,subject etc) so basically PHP code send mail at the end and it's very easy to use .

Personally, I suggest you use Webservice because it's simpler than first approach.

** Edit: You can save email in sharedpreferences or database and fetch it from there when you try to send mail rather than hardcoding it to avoid theft email risk.(directly putting mail address inside code)

Anyway you can prevent other from getting your full source code.See this answer

Send data from Android app with email

GmailBackground is small library to send an email in background without user interaction :

Usage:

    BackgroundMail.newBuilder(this)
.withUsername("username@gmail.com")
.withPassword("password12345")
.withMailto("toemail@gmail.com")
.withType(BackgroundMail.TYPE_PLAIN)
.withSubject("this is the subject")
.withBody("this is the body")
.withOnSuccessCallback(new BackgroundMail.OnSuccessCallback() {
@Override
public void onSuccess() {
//do some magic
}
})
.withOnFailCallback(new BackgroundMail.OnFailCallback() {
@Override
public void onFail() {
//do some magic
}
})
.send();

Source

(I've tested it myself)

How to send emails from my app using a Gmail account?

For anyone interested, no solution worked. Gmail just refused to allow different locations to login, because of extra-security (without option to disable this).
I moved to use other email provider. No problems anymore with the same code.



Related Topics



Leave a reply



Submit