获取java mail api密码身份验证方法中的错误

获取java mail api密码身份验证方法中的错误,java,email,jakarta-mail,Java,Email,Jakarta Mail,我正在尝试使用java邮件api发送邮件。代码中除了Authenticator类之外,其他一切都正常。它正在发出警告,因为 Constructor PasswordAuthentication can not be applied to given types. required java.lang.String,java.lang.char[] 这是我的代码片段,我收到警告错误,但无法解决问题 Authenticator auth = new Authenticator() {

我正在尝试使用java邮件api发送邮件。代码中除了Authenticator类之外,其他一切都正常。它正在发出警告,因为

Constructor PasswordAuthentication can not be applied to given types.
required java.lang.String,java.lang.char[]
这是我的代码片段,我收到警告错误,但无法解决问题

Authenticator auth = new Authenticator() {

        @Override
        public PasswordAuthentication getPasswordAuthentication() {
 error           return new PasswordAuthentication(userName, password);
        }
    };
      error   Session session = Session.getInstance(properties, auth);
这两行//error在代码中给出了错误

请帮帮我。 提前感谢..

构造函数只接受
字符串
字符
数组作为参数。所以你应该:

返回新密码身份验证(用户名、密码.tocharray())

编辑:

问题是需要来自
javax.mail
包的
authenticificator
对象

我想你进错包了。它应该是
javax.mail.Authenticator
,而不是
java.net.Authenticator


因此,您应该使用
javax.mail
包中的对象
PasswordAuthentication
(它接受两个
String
作为参数),而不是
java.net
包中的对象
PasswordAuthentication
(它接受
String
char
数组).

当您调用构造函数时
密码身份验证(字符串a,char[]b)

例外情况是,您在参数中传递了错误的类型,例如:

您的代码:
返回新密码身份验证(用户名、密码)

用户名或密码类型错误,可能用户名不是字符串或密码不是字符[],请仔细查看

试试看

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

如果您使用的是gmail帐户,则必须禁用两步身份验证,或者您可以在gmail帐户上设置应用程序密码,然后在应用程序中使用应用程序密码


用户名和密码的类型是什么?@zouzu final String userName,final String password如果使用
getPassword
注意,它返回的是
char[]
,而不是
字符串。您可以通过
新字符串(char[])
转换它;主席先生,我在这一行也得到错误会话=会话。getInstance(属性,身份验证);是同样的例外吗?参数auth也是passwordAuthentication的对象?找不到适用于getinstance on line Session=Session.getinstance(属性,auth)的错误方法;很可能您仍然导入了错误的验证器。请注意。我尝试了这个,但仍然得到了相同的错误:-(这很奇怪,因为这看起来像是强制它为javax.mail.auth类型的完美答案