Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javax.mail.AuthenticationFailedException in Android

I receive the below error message in my LogCat when trying to send an email from my Gmail account using JavaMail API.

11-09 11:04:14.385: W/System.err(18443): javax.mail.AuthenticationFailedException
11-09 11:04:14.385: W/System.err(18443):    at javax.mail.Service.connect(Service.java:319)
11-09 11:04:14.385: W/System.err(18443):    at javax.mail.Service.connect(Service.java:169)
11-09 11:04:14.385: W/System.err(18443):    at javax.mail.Service.connect(Service.java:118)
11-09 11:04:14.385: W/System.err(18443):    at com.srindroid.sendemail.MainActivity$RetrievedFeedTask.doInBackground(MainActivity.java:88)
11-09 11:04:14.385: W/System.err(18443):    at com.srindroid.sendemail.MainActivity$RetrievedFeedTask.doInBackground(MainActivity.java:1)
11-09 11:04:14.385: W/System.err(18443):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
11-09 11:04:14.385: W/System.err(18443):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
11-09 11:04:14.385: W/System.err(18443):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
11-09 11:04:14.385: W/System.err(18443):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-09 11:04:14.385: W/System.err(18443):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-09 11:04:14.385: W/System.err(18443):    at java.lang.Thread.run(Thread.java:841)

Source Code

package com.srindroid.sendemail;

import java.util.Properties;

import javax.mail.Authenticator;
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;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

    Session session = null;
    ProgressDialog pdialog = null;
    Context context = null;
    EditText rec, subject, message;
    String strRec, strSubject, strMessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        context = this;

        Button submit = (Button) findViewById (R.id.btnSendEmail);
        rec = (EditText) findViewById (R.id.txtToEmail);
        subject = (EditText) findViewById (R.id.txtSubject);
        message = (EditText) findViewById (R.id.txtMessage);

        submit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        strRec = rec.getText().toString();
        strSubject = subject.getText().toString();
        strMessage = subject.getText().toString();

        Properties props = new Properties();
        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", "false");
        props.put("mail.smtp.port", "465");

        session = Session.getDefaultInstance(props, new Authenticator(){
            protected PasswordAuthentication getPasswordAuthentication(){
                return new PasswordAuthentication("[email protected]",**<MyPassword>**);               
            }
        });

        pdialog = ProgressDialog.show(context, "Send Email", "Sending Email...");
        RetrievedFeedTask task = new RetrievedFeedTask();
        task.execute();

    }

    class RetrievedFeedTask extends AsyncTask<String, Void, String>{

        @Override
        protected String doInBackground(String... params) {
            try{

                Message message = new MimeMessage(session);
                message.setFrom(new InternetAddress("[email protected]"));
                message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(strRec));
                message.setSubject(strSubject);
                message.setContent(strMessage, "text/html; charset=utf-8");

                Transport transport = session.getTransport("smtp");
                transport.connect();
                transport.sendMessage(message, message.getAllRecipients());


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

        @Override
        protected void onPostExecute(String result) {
            pdialog.dismiss();
            rec.setText("");
            subject.setText("");
            message.setText("");
            Toast.makeText(getApplicationContext(), "Email Sent", Toast.LENGTH_LONG).show();
            super.onPostExecute(result);
        }



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


}

Could you please help me in understanding on what creates this AuthenticationFailedException? Please note that when I use the same credentials in www.gmail.com, I'm able to successfully login into my email account.

Thanks, Srini

like image 804
Srini Avatar asked Nov 09 '14 05:11

Srini


2 Answers

First, fix these common mistakes.

Turn on Session debugging to see if the protocol trace gives any more clues about what's wrong.

You may need to enable access for less secure apps.

like image 114
Bill Shannon Avatar answered Nov 06 '22 20:11

Bill Shannon


go to your mail and then click on my account and then select Sign-in & security option

and then on bottom show Allow less secure apps: OFF is off so ON this option and now you can send background mail.

thank you

like image 8
Adil Saiyad Avatar answered Nov 06 '22 19:11

Adil Saiyad