1

Possible Duplicate:
Sending Email in Android using JavaMail API without using the default android app(Builtin Email application)

I am new to Android Coding. My Requirement is I want to send Email using Android Code.

Please guide me regarding this.

1
  • This question is already asked hundred times on stackoverflow. Commented Jan 17, 2012 at 11:15

3 Answers 3

26

Here is solution ::

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"[email protected]"});
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();
}
Sign up to request clarification or add additional context in comments.

Comments

3

You could write a server side script using .Net, Java or PHP. Make a web request (asynchronously) to domain.com/sendemail.php with the arguments of:

  • Email Address to send to
  • Subject (could be optional but personally I would fill this out)
  • Email Body

The server side script can then handle this all for you and gives you more flexibility on the layout of the emails without having to perform app updates if something should need to change. this also means that the email can come from the app rather than the individual user keeping them anonymous (which may or may not be useful as you have not said).

To do all this look at the Painless Threading article by Android then look at how to make web requests.

Option B:

Use an Intent like so:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

String[] recipients = new String[]{"[email protected]", "",};

emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);

emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Test");

emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "This is email's message");

emailIntent.setType("text/plain");

startActivity(Intent.createChooser(emailIntent, "Send mail..."));

Reference: http://thedevelopersinfo.wordpress.com/2009/10/22/email-sending-in-android/

1 Comment

I had added this code with my activity. But Still its not working. It didn't show any error message. But the mail didn't send.
2

You can send email directly from code:

    String to = "[email protected]";

    String from = "[email protected]";

    Properties properties = System.getProperties();

    properties.setProperty("mail.smtp.host", SMPT_HOSTNAME);

    Session session = Session.getInstance(properties, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(USERNAME, PASSWORD);
        }
    });

    try {
        MimeMessage message = new MimeMessage(session);

        message.setFrom(new InternetAddress(from));

        message.addRecipient(Message.RecipientType.TO, new InternetAddress(
                to));

        message.setSubject("This is the Subject Line!");

        message.setText("This is actual message");

        Transport.send(message);
        System.out.println("Sent message successfully....");
    } catch (MessagingException mex) {
        mex.printStackTrace();
    }

1 Comment

javax.mail.* are not available on Android.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.