Anju
Anju

Reputation: 9479

retrieve gmail account details configured in android

In my application, I have to send a mail when a button is clicked. I have all details for sending the mail except the "from address". This "from address" should be the gmail account configured in the android phone. How can I fetch those details? Can anyone please help?

Upvotes: 2

Views: 2219

Answers (2)

Anju
Anju

Reputation: 9479

It got worked by using AccountManager class.

AccountManager manager = AccountManager.get(this);
Account[] accounts = manager.getAccountsByType("com.google");
Account account = accounts[0];

We can fetch account name using

account.name

and the encrypted password using

manager.getPassword(account)

Upvotes: 3

maid450
maid450

Reputation: 7486

You can use the built-in ACTION_SEND intent to use default android's mail sending app, here you have an example.

This is the part you would need:

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
//set the content-type
emailIntent.setType("plain/text");
//set the receivers address
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { address.getText().toString() });
//set the subject
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject.getText());
//set the text
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailtext.getText());
Email.this.startActivity(Intent.createChooser(emailIntent, "Send mail..."));

Note: I haven't tested it myself

Upvotes: -1

Related Questions