Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting error in java mail api PasswordAuthentication method

I am trying to send mail using java mail api..Every thing is ok in the code except the Authenticator class .It is giving warning as ...

Constructor PasswordAuthentication can not be applied to given types.
required java.lang.String,java.lang.char[]

Here is my code snippet where i am getting warning error but not able to resolve the issue...

Authenticator auth = new Authenticator() {

        @Override
        public PasswordAuthentication getPasswordAuthentication() {
 error           return new PasswordAuthentication(userName, password);
        }
    };
      error   Session session = Session.getInstance(properties, auth);

These two lines with //error is giving error in the code..

Please help me. Thanks in advance..

like image 932
Adi Avatar asked Feb 27 '26 09:02

Adi


2 Answers

PasswordAuthentication constructor only accept a String and a char array as arguments. So you should do :

return new PasswordAuthentication(userName, password.toCharArray());

Edit :

The problem is that Session.getInstance(java.util.Properties props, Authenticator authenticator) requires an Authentificator object from the javax.mail package.

I think you've imported the wrong package. It should be javax.mail.Authenticator and not java.net.Authenticator

So you should use the object PasswordAuthentication from the javax.mail package (which accept two Strings as argument), instead of the object PasswordAuthentification from the java.net package (which accept a String and a char array).

like image 175
Alexis C. Avatar answered Mar 01 '26 21:03

Alexis C.


when you call the constructor PasswordAuthentication(String a, char[] b)

the Exception is telling you that you are passing a wrong type in the parameters, for example:

your code: return new PasswordAuthentication(userName, password);

userName or password are wrong type, maybe userName is not a String or password is not a char[], take a look carefully.

like image 41
ZaoTaoBao Avatar answered Mar 01 '26 23:03

ZaoTaoBao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!