Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Below is my sendmail class I’ve to to send a mail with javamail API:  

package com.example.****.Utils;


 

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 SendMail extends AsyncTask<Void, Void, Void> {

  private Context context;

  private Session session;


 

  private String email;

  private String subject;

  private String message;

  public SendMail(Context context, String email, String subject, String message)

  {

      this.context = context;

      this.email = email;

      this.subject = subject;

      this.message = message;

  }

  @Override

    protected void onPreExecute() {

      super.onPreExecute();

      Toast.makeText(context, "Sending mail...", Toast.LENGTH_SHORT).show();

  }

  @Override

    protected void onPostExecute(Void aVoid)

  {

      super.onPostExecute(aVoid);

      Toast.makeText(context, "Message Sent", Toast.LENGTH_SHORT).show();

  }

  @Override

    protected Void doInBackground(Void... params) {

      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", "true");

      props.put("mail.smtp.port", "465");

      session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {

          protected  PasswordAuthentication getPasswordAuthentication() {

              return new PasswordAuthentication(Config.EMAIL, Config.PASSWORD);

          }

      });

      try {

          MimeMessage mm = new MimeMessage(session);

          mm.setFrom(new InternetAddress(Config.EMAIL));

          mm.addRecipient(Message.RecipientType.TO, new InternetAddress(email));

          mm.setSubject(subject);

          mm.setText(message);

          Transport.send(mm);

      }

      catch (MessagingException e) {

          e.printStackTrace();

      }

      return null;

  }

}

Below is the code I use to provide the Email and password:

String email = "******";

        String subject = "Activation";

        String message = "This User requires activation: " + "<a href='http://localhost:80/approve/" +mAuth.getCurrentUser().getEmail() +"> Click here </a>";

        SendMail sm = new SendMail(this, email, subject,message);

        sm.execute();

Even after giving the right password, I get the below error:

W/System.err: javax.mail.AuthenticationFailedException

        at javax.mail.Service.connect(Service.java:319)

        at javax.mail.Service.connect(Service.java:169)

        at javax.mail.Service.connect(Service.java:118)

        at javax.mail.Transport.send0(Transport.java:188)

W/System.err:     at javax.mail.Transport.send(Transport.java:118)

        at com.example.**.Utils.SendMail.doInBackground(SendMail.java:71)

        at com.example.**.Utils.SendMail.doInBackground(SendMail.java:19)

        at android.os.AsyncTask$3.call(AsyncTask.java:378)

        at java.util.concurrent.FutureTask.run(FutureTask.java:266)

        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:289)

        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)

        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)

        at java.lang.Thread.run(Thread.java:919)


 

Can anyone tell me what I’m doing wrong here?

1 Answer

0 votes
by (19.7k points)

You should use the below property in your doInBackground() method to avoid the error.

props.put("mail.smtp.ssl.trust", "smtp.gmail.com"); 

Interested in Java? Check out this Java Certification by Intellipaat.   

Browse Categories

...