在java mail api PasswordAuthentication方法中获取错误

Adi*_*Adi 2 java email jakarta-mail

我正在尝试使用java mail api发送邮件.除了Authenticator类之外,代码中的所有东西都可以.它正在发出警告...

Constructor PasswordAuthentication can not be applied to given types.
required java.lang.String,java.lang.char[]
Run Code Online (Sandbox Code Playgroud)

这是我的代码片段,我收到警告错误但无法解决问题...

Authenticator auth = new Authenticator() {

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

这两行//错误在代码中给出了错误.

请帮我.提前致谢..

Ale*_* C. 7

PasswordAuthentication构造函数只接受a Stringchar数组作为参数.所以你应该这样做:

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

编辑:

问题是Session.getInstance(java.util.Properties props, Authenticator authenticator) 需要包中的Authentificator对象javax.mail.

我认为你导入了错误的包裹.应该javax.mail.Authenticator而不是java.net.Authenticator

所以,你应该使用对象PasswordAuthenticationjavax.mail包(它接受两个Strings作为参数),而不是对象PasswordAuthentificationjava.net包(其中接受Stringchar阵列).