3

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

3
  • What's the type of userName and password ? Commented Nov 14, 2013 at 9:06
  • @ZouZou final String userName, final String password Commented Nov 14, 2013 at 9:08
  • If you are using getPassword note it returns a char[], not a String. You can convert it via new String(char[]). Commented Nov 14, 2013 at 9:08

4 Answers 4

9

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

Sign up to request clarification or add additional context in comments.

1 Comment

i am also getting error on this line Session session = Session.getInstance(properties, auth);
1

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.

4 Comments

sir i am getting error on this line also Session session = Session.getInstance(properties, auth);
is same exception? parameter auth is an object of passwordAuthentication too?
error is no suitable method found for getinstance on line Session session = Session.getInstance(properties, auth);
Most likely you're still importing the wrong Authenticator. Note that you don't need to use an Authenticator at all.
1

Try

Session session = Session.getInstance(properties,
          new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
          });

1 Comment

I tried this, but I still get the same error :-( which is weird since this looks like the perfect answer to force it to be of javax.mail.auth type
0

If you are using gmail account, you must disable the two step authentication or you can either set on your gmail account app password and use the app password instead in your application.

https://support.google.com/accounts/answer/185833?hl=en

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.