获取java mail api密码身份验证方法中的错误
我正在尝试使用java邮件api发送邮件。代码中除了Authenticator类之外,其他一切都正常。它正在发出警告,因为获取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() {
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类型的完美答案