Error sending email with android

1

Good I want to send emails but I get an error and I do not know why, the password is set correctly

The code is as follows:

public void enviarCorreos(){

    final String correoEnvio = "[email protected]";

    final String contraseña= ""; //en la aplicación esta puesta
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    Properties properties = new Properties();
    properties.put("mail.smtp.host", "smtp.googlemail.com");
    properties.put("mail.smtp.socketFactory.port", "465");
    properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.port", "465");
    properties.put("mail.imap.ssl.enable", "true"); // required for Gmail


    try {
        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(correoEnvio, contraseña);
            }
        });

        if (session!= null){
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(correoEnvio));
            message.setSubject("holaaa");
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]"));
            message.setContent("holaaa","text/html; charset=utf-8");

            Transport.send(message);

        }
    } catch (Exception e){
        e.printStackTrace();
    }

}

And he gives me the following error

05-28 16:42:06.875 1992-1992/com.example.sergio.playbetwincliente W/System.err: javax.mail.AuthenticationFailedException
05-28 16:42:06.895 1992-1992/com.example.sergio.playbetwincliente W/System.err:     at javax.mail.Service.connect(Service.java:319)
05-28 16:42:06.895 1992-1992/com.example.sergio.playbetwincliente W/System.err:     at javax.mail.Service.connect(Service.java:169)
05-28 16:42:06.895 1992-1992/com.example.sergio.playbetwincliente W/System.err:     at javax.mail.Service.connect(Service.java:118)
05-28 16:42:06.895 1992-1992/com.example.sergio.playbetwincliente W/System.err:     at javax.mail.Transport.send0(Transport.java:188)
05-28 16:42:06.895 1992-1992/com.example.sergio.playbetwincliente W/System.err:     at javax.mail.Transport.send(Transport.java:118)
05-28 16:42:06.895 1992-1992/com.example.sergio.playbetwincliente W/System.err:     at com.example.sergio.playbetwincliente.Principal.enviarCorreos(Principal.java:422)
05-28 16:42:06.895 1992-1992/com.example.sergio.playbetwincliente W/System.err:     at com.example.sergio.playbetwincliente.Principal.consePremios(Principal.java:388)
05-28 16:42:06.895 1992-1992/com.example.sergio.playbetwincliente W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
05-28 16:42:06.895 1992-1992/com.example.sergio.playbetwincliente W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
05-28 16:42:06.895 1992-1992/com.example.sergio.playbetwincliente W/System.err:     at android.view.View$1.onClick(View.java:3818)
05-28 16:42:06.895 1992-1992/com.example.sergio.playbetwincliente W/System.err:     at android.view.View.performClick(View.java:4438)
05-28 16:42:06.895 1992-1992/com.example.sergio.playbetwincliente W/System.err:     at android.view.View$PerformClick.run(View.java:18422)
05-28 16:42:06.895 1992-1992/com.example.sergio.playbetwincliente W/System.err:     at android.os.Handler.handleCallback(Handler.java:733)
05-28 16:42:06.895 1992-1992/com.example.sergio.playbetwincliente W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
05-28 16:42:06.895 1992-1992/com.example.sergio.playbetwincliente W/System.err:     at android.os.Looper.loop(Looper.java:136)
05-28 16:42:06.895 1992-1992/com.example.sergio.playbetwincliente W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5045)
05-28 16:42:06.895 1992-1992/com.example.sergio.playbetwincliente W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
05-28 16:42:06.895 1992-1992/com.example.sergio.playbetwincliente W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
05-28 16:42:06.895 1992-1992/com.example.sergio.playbetwincliente W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-28 16:42:06.895 1992-1992/com.example.sergio.playbetwincliente W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-28 16:42:06.895 1992-1992/com.example.sergio.playbetwincliente W/System.err:     at dalvik.system.NativeStart.main(Native Method)

I use the libraries javax.mail, additionnal and activation

And with the Internet permission activated since I use it for another part of the project

    
asked by Sergio Alcántara Reyes 28.05.2017 в 17:51
source

1 answer

0

Already many thanks to link , look at the link and it was that I did not have

compile 'com.sun.mail:android-activation:1.5.5'

With that simple line my problem was solved, but as I saw you created a class for the sending, it convinced me more that idea, so I tried it and it also works very well. Thank you very much

I leave the link

How to send mail directly from android

    
answered by 04.06.2017 / 22:04
source