How to send mail directly from android

I am developing an app for incident management of a company.

In one of the activities, an issue is recorded and there is a Send button, which must send the mail. This I want to make it alien to the user.

What method should I use? From Android I know how to send an email by opening the gmail application or other mail manager, but is there a possibility to send it directly without going through there?.

If you know another way I'll be happy to read it.

 5
Author: Stefan Nolde, 2016-06-01

3 answers

I know that's not what you want, but if you want to send an email through the gmail app it's as simple as making an intent like this:

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto","[email protected]", null));
                emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Android APP - ");
                startActivity(Intent.createChooser(emailIntent,  getActivity().getString(R.string.enviar_mail)));

In your case, that you are looking for minimal user interaction and that the management is done in a quasi-automatic way, you could use the Library JavaMail

You have a basic example (both read and send) in my repository.

A greeting.

 8
Author: Sergi Barjola, 2016-06-02 07:44:47

If you want to send an email directly on Android, you can use JavaMail for Android

In your project build.gradle you need to add maven repository:

repositories { 
     jcenter()
     maven {
         url "https://maven.java.net/content/groups/public/"
     }
 }

And in your build dependencies you have to add to the dependencies:

dependencies {
     compile 'com.sun.mail:android-mail:1.5.5'
     compile 'com.sun.mail:android-activation:1.5.5'
 }

I leave you the Class MailJob as an example.

/**
 * Created by snolde on 06-04-2017.
 */

public class MailJob extends AsyncTask<MailJob.Mail,Void,Void>{
    private final String user;
    private final String pass;

    public MailJob(String user, String pass) {
        super();
        this.user=user;
        this.pass=pass;
    }

    @Override
    protected Void doInBackground(Mail... mails) {
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(user, pass);
                    }
                });
        for (Mail mail:mails) {

            try {

                Message message = new MimeMessage(session);
                message.setFrom(new InternetAddress(mail.from));
                message.setRecipients(Message.RecipientType.TO,
                        InternetAddress.parse(mail.to));
                message.setSubject(mail.subject);
                message.setText(mail.content);

                Transport.send(message);

            } catch (MessagingException e) {
                Log.d("MailJob", e.getMessage());
            }
        }
        return null;
    }

    public static class Mail{
        private final String subject;
        private final String content;
        private final String from;
        private final String to;

        public Mail(String from, String to, String subject, String content){
            this.subject=subject;
            this.content=content;
            this.from=from;
            this.to=to;
        }
    }
}

To send emails you call the following code (with the data you want to send from your Result):

new MailJob(user, passwd).execute(
              new MailJob.Mail("[email protected]", "[email protected]", "subjeto", "contenido")
            );
 5
Author: Stefan Nolde, 2017-05-30 03:53:41

This would be an option using an intent "chooser", what it does is automatically open a dialog screen with all the applications installed on your device that can send an email:

  String[] TO = {""}; //Direcciones email  a enviar.
  String[] CC = {""}; //Direcciones email con copia.

  Intent emailIntent = new Intent(Intent.ACTION_SEND);

  emailIntent.setData(Uri.parse("mailto:"));
  emailIntent.setType("text/plain");
  emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
  emailIntent.putExtra(Intent.EXTRA_CC, CC);
  emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Tu Asunto...");
  emailIntent.putExtra(Intent.EXTRA_TEXT, "[email protected]"); // * configurar email aquí!

  try {
     startActivity(Intent.createChooser(emailIntent, "Enviar email."));        
     Log.i("EMAIL", "Enviando email...");
  }
  catch (android.content.ActivityNotFoundException e) {
     Toast.makeText(this, "NO existe ningún cliente de email instalado!.", Toast.LENGTH_SHORT).show();
  }

You can select the client to send the email!.
enter the description of the image here

 3
Author: Jorgesys, 2016-06-01 17:25:01