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.

Community
  • 1
  • 1
Thiru
  • 2,374
  • 7
  • 21
  • 29

3 Answers3

26

Here is solution ::

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
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();
}
Nikunj Patel
  • 21,853
  • 23
  • 89
  • 133
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[]{"my@email.com", "",};

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/

Graham Smith
  • 25,627
  • 10
  • 46
  • 69
  • 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. – Thiru Jan 18 '12 at 03:35
2

You can send email directly from code:

    String to = "abcd@gmail.com";

    String from = "web@gmail.com";

    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();
    }
Roman Black
  • 3,501
  • 1
  • 22
  • 31