Adi Adi - 1 month ago 8
Java Question

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..

Answer

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).