Android 2.3 send direct mail to GMail, Outlook, Yahoo

2

I am developing an application where there are registered users in a MySQL table where there is a "mail" field.

In the App, there is a button "I forgot my password", when the user clicks, he receives a "message" with his password in his mail with which he is registered.

The shipment must be "automatic", that is, you should not ask to choose a postal client.

  

I found many examples but as always, they DID NOT WORK for being poorly explained, but at last I found one that DOES WORK and I want to share it with you.

Original article: link

    
asked by Jorny 21.03.2017 в 09:09
source

1 answer

2
  • Download the necessary JAR libraries (mail.jar, additionnal.jar, activation.jar).
    link
  • Create an empty project on Android.
  • In the project browser, change from "Android to Project"
  • Copy the 3 JARs and paste them into the "libs" folder by right clicking and then on the "Paste" option.
  • Click on the "File" Menu and then on "Project Structure"
  • A window will open, click on the "app" option, then on the "Dependencies" tab.
  • Click on the "+" symbol and then on the "JAR dependency" option to add a new library.
  • Another window will open, choose the libraries that we pasted before (one by one), click on OK.
  • When we have added the 3 JAR libraries, click on OK in the "Project Structure" window.
  • Change from "Project to Android" (see step 3).
  • Create class clsConfiguracion.java

    public class clsConfiguracion {
        public static final String DE_CORREO ="[email protected]";
        public static final String DE_PASSWORD ="contraseña_remitente";
    }
    
  • Create class clsSendMail

    import android.app.ProgressDialog;
    import android.content.Context;
    import android.os.AsyncTask;
    import android.widget.Toast;
    
    import java.util.Properties;
    
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    
    public class clsEnviaCorreo extends AsyncTask<Void,Void,Void> {
        private Context contexto;
        private Session De_Sesion;
    
        private String A_Correo;
        private String A_Asunto;
        private String A_Mensaje;
    
        private ProgressDialog progreso;
    
        public clsEnviaCorreo(Context cContexto, String cCorreo, String cAsunto, String cMensaje) {
            this.contexto = cContexto;
            this.A_Correo = cCorreo;
            this.A_Asunto = cAsunto;
            this.A_Mensaje = cMensaje;
        }
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progreso = ProgressDialog.show(contexto, "Enviando mensaje", "Espere...", false, false);
        }
    
        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            progreso.dismiss();
            Toast.makeText(contexto, "Mensaje enviado", Toast.LENGTH_LONG).show();
        }
    
        @Override
        protected Void doInBackground(Void... params) {
            Properties props = new Properties();
            /*Configuraciones según el proveedor de Correo electrónico que enviará el Mensaje*/
            /*=========================================================================================
                                             PARA GMAIL
             Requisito: se debe activar "Permitir que aplicaciones menos seguras accedan a tu cuenta"
         https://www.google.com/settings/security/lesssecureapps
                ===========================================================================================*/
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", "465");
            /*=========================================================================================
                                             PARA OUTLOOK (ANTES HOTMAIL)
        ===========================================================================================*/
            /*props.put("mail.smtp.host", "smtp-mail.outlook.com");
            props.put("mail.smtp.port", "587");
            props.put("mail.smtp.starttls.enable","true");
            props.put("mail.smtp.auth", "true");*/
           /*=========================================================================================
                                             PARA YAHOO
             Requisito: se debe activar "Permitir aplicaciones que utilicen un inicio de sesión menos seguro"
             https://login.yahoo.com/account/security#other-apps?lang=es-ES
        ===========================================================================================*/
            /*props.put("mail.smtp.host", "smtp.mail.yahoo.com");
            props.put("mail.smtp.port", "587");
            props.put("mail.smtp.debug", "true");
            props.put("mail.smtp.ssl.enable", "true");
            props.put("mail.smtp.starttls.enable","true");
            props.put("mail.smtp.auth", "true");*/
    
            De_Sesion = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(clsConfiguracion.DE_CORREO, clsConfiguracion.DE_PASSWORD);
                        }
                    });
    
            try {
                MimeMessage mm = new MimeMessage(De_Sesion);
                mm.setFrom(new InternetAddress(clsConfiguracion.DE_CORREO));
                mm.addRecipient(Message.RecipientType.TO, new InternetAddress(A_Correo));
                mm.setSubject(A_Asunto);
                mm.setText(A_Mensaje);
                Transport.send(mm);
            } catch (MessagingException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
    
  • activity_main.xml

        

    <EditText
        android:id="@+id/txtCorreoDestino"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="[email protected]"
        android:layout_below="@+id/lblDestinatario"/>
    
    <TextView
        android:id="@+id/lblAsunto"
        android:text="Asunto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtCorreoDestino"/>
    
    <EditText
        android:id="@+id/txtAsunto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Prueba de mensaje"
        android:layout_below="@+id/lblAsunto"/>
    
    <TextView
        android:id="@+id/lblMensaje"
        android:text="Mensaje"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtAsunto"/>
    
    <EditText
        android:id="@+id/txtMensaje"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:lines="4"
        android:text="Este es un mensaje de prueba desde JavaMail"
        android:layout_below="@+id/lblMensaje"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnEnviar"
        android:text="Enviar correo"
        android:layout_below="@+id/txtMensaje"/>
    

  • MainActivity.java

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
        private EditText xCorreo;
        private EditText xAsunto;
        private EditText xMensaje;
        private Button xEnviar;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            xCorreo = (EditText) findViewById(R.id.txtCorreoDestino);
            xAsunto = (EditText) findViewById(R.id.txtAsunto);
            xMensaje = (EditText) findViewById(R.id.txtMensaje);
            xEnviar = (Button) findViewById(R.id.btnEnviar);
            xEnviar.setOnClickListener(this);
        }
        private void EnviarCorreo() {
            String sCorreo = xCorreo.getText().toString().trim();
            String sAsunto = xAsunto.getText().toString().trim();
            String sMensaje = xMensaje.getText().toString().trim();
            clsEnviaCorreo objCorreo = new clsEnviaCorreo(this, sCorreo, sAsunto, sMensaje);
            objCorreo.execute();
        }
    
        @Override
        public void onClick(View v) {
            EnviarCorreo();
        }
    }
    
  • Finally the necessary permissions in AndroidManifest.xml

    <uses-permission android:name="android.permission.INTERNET"/>
    
  • Now yes, to try and thank;), good luck to all.

  •   

    In the case of GMAIL and YAHOO, you must activate the corresponding permissions so that it can work, (read the comments within the code of clsEnviaCorreo .

        
    answered by 21.03.2017 / 09:09
    source