GMail SMTP不授权JavaMail API中使用的凭据,即使它们是正确的
我已经编写了一个测试代码从may Java应用程序发送电子邮件。即使我使用的是有效的gmail地址和密码,但在运行测试代码时仍会出现以下错误:GMail SMTP不授权JavaMail API中使用的凭据,即使它们是正确的,java,gmail,jakarta-mail,Java,Gmail,Jakarta Mail,我已经编写了一个测试代码从may Java应用程序发送电子邮件。即使我使用的是有效的gmail地址和密码,但在运行测试代码时仍会出现以下错误: javax.mail.MessagingException: javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. 这是我的测试代码 public class GmailSmtpSSL { GmailSmtpSSL(Str
javax.mail.MessagingException: javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted.
这是我的测试代码
public class GmailSmtpSSL {
GmailSmtpSSL(String username, String password) {
usern = username;
pass = password;
setDebugMsg("Setting user name to : " + usern);
setDebugMsg("Using given password : " + pass);
props = new Properties();
setDebugMsg("Setting smtp server: smtp.gmail.com");
setDebugMsg("Using SSL at port 465 auth enabled");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.user", usern);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
//props.put("mail.smtp.debug", "true");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
SMTPAuthenticator auth = new SMTPAuthenticator();
session = Session.getDefaultInstance(props, auth);
session.setDebug(true);
setDebugMsg("Session initialization complete");
}
public void destroy() {
props.clear();
props = null;
usern = null;
pass = null;
session = null;
}
public void sendMailTo(String to, String sub, String body)
throws MessagingException {
Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss a");
String dateToday = formatter.format(currentDate.getTime()).toLowerCase();
if (to.equals("self")) {
to = usern;
}
setDebugMsg("Composing message: To " + to);
setDebugMsg("Composing message: Subject " + sub);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(usern));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject("PinguBot: " + dateToday + " " + sub);
message.setText(body);
setDebugMsg("Attempting to send...");
Transport transport = session.getTransport("smtps");
transport.connect("smtp.gmail.com", 465, usern, pass);
Transport.send(message);
transport.close();
} catch (MessagingException me) {
setDebugMsg(me.toString());
throw new MessagingException(me.toString());
}
setDebugMsg("Mail was send successfully");
}
public String getDebugMsg() {
String msg = new String(debugMsg);
debugMsg = " ";
return msg;
}
private static void setDebugMsg(String msg) {
debugMsg += msg + "\n";
System.out.println(msg);
}
private static String debugMsg = "";
private String usern;
private String pass;
private Session session;
private static Properties props;
private class SMTPAuthenticator extends Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(usern, pass);
}
}
public static void main(String... args){
GmailSmtpSSL gssl=new GmailSmtpSSL("<valid Id>@gmail.com", "<valid password>");
String to="satinder_2000@outlook.com";
String sub="Hello";
String body= "Hamara Dil Aapke Paas hai!!";
try {
gssl.sendMailTo(to, sub, body);
} catch (MessagingException ex) {
Logger.getLogger(GmailSmtpSSL.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
公共类GmailSmtpSSL{
GmailSmtpSSL(字符串用户名、字符串密码){
usern=用户名;
pass=密码;
setDebugMsg(“将用户名设置为:“+usern”);
setDebugMsg(“使用给定密码:+pass”);
props=新属性();
setDebugMsg(“设置smtp服务器:smtp.gmail.com”);
setDebugMsg(“在端口465启用SSL身份验证”);
put(“mail.smtp.host”、“smtp.gmail.com”);
props.put(“mail.smtp.port”,“465”);
props.put(“mail.smtp.user”,usern);
props.put(“mail.smtp.auth”,“true”);
props.put(“mail.smtp.starttls.enable”、“true”);
//props.put(“mail.smtp.debug”,“true”);
props.put(“mail.smtp.socketFactory.port”,“465”);
put(“mail.smtp.socketFactory.class”、“javax.net.ssl.SSLSocketFactory”);
props.put(“mail.smtp.socketFactory.fallback”、“false”);
SMTPAuthenticator auth=新的SMTPAuthenticator();
session=session.getDefaultInstance(props,auth);
session.setDebug(true);
setDebugMsg(“会话初始化完成”);
}
公共空间销毁(){
props.clear();
props=null;
usern=null;
pass=null;
会话=空;
}
public void sendMailTo(字符串收件人、字符串子项、字符串正文)
抛出消息异常{
Calendar currentDate=Calendar.getInstance();
SimpleDataFormat格式化程序=新的SimpleDataFormat(“yyyy-MMM-dd-HH:mm:ss a”);
String dateToday=formatter.format(currentDate.getTime()).toLowerCase();
如果(等于(“自我”)){
to=usern;
}
setDebugMsg(“编写消息:到”+到);
setDebugMsg(“撰写消息:主题”+sub);
试一试{
Message Message=新的mimessage(会话);
message.setFrom(新的InternetAddress(usern));
message.setRecipients(message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject(“PinguBot:+dateToday+”+sub);
message.setText(正文);
setDebugMsg(“正在尝试发送…”);
传输=session.getTransport(“smtps”);
transport.connect(“smtp.gmail.com”,465,usern,pass);
传输。发送(消息);
transport.close();
}捕获(消息例外我){
setDebugMsg(me.toString());
抛出新的MessaginException(me.toString());
}
setDebugMsg(“邮件已成功发送”);
}
公共字符串getDebugMsg(){
String msg=新字符串(debugMsg);
debugMsg=“”;
返回味精;
}
私有静态void setDebugMsg(字符串msg){
debugMsg+=msg+“\n”;
System.out.println(msg);
}
私有静态字符串debugMsg=“”;
私有字符串用户n;
私人串通;
非公开会议;
私有静态属性道具;
私有类SMTPAuthenticator扩展验证器{
公共密码身份验证getPasswordAuthentication(){
返回新密码身份验证(usern,pass);
}
}
公共静态void main(字符串…参数){
GmailSmtpSSL gssl=新的GmailSmtpSSL(“@gmail.com”,”);
字符串到satinder_2000@outlook.com";
String sub=“Hello”;
字符串体=“Hamara Dil Aapke Paas hai!!”;
试一试{
gssl.sendMailTo(收件人、子收件人、正文);
}捕获(消息例外){
Logger.getLogger(GmailSmtpSSL.class.getName()).log(Level.SEVERE,null,ex);
}
}
}
相反,我在我的gmail收件箱中收到一封电子邮件,如下所示
次临界安全警报
Body-有人刚刚使用你的密码试图从非谷歌应用程序登录到你的帐户。谷歌阻止了他们,但你应该检查发生了什么。查看您的帐户活动,确保其他人没有访问权限
有人能告诉我代码有什么问题吗
谢谢你的帮助
问候
辛格汉姆你没有做错什么。这是谷歌和他们处理发送电子邮件的应用程序的方式。请检查这篇文章,看看它是否对你有帮助。我遇到了同样的问题。登录到您的帐户,转到您的安全设置,然后打开“允许安全性较低的应用”。您可能还需要创建一个。你可能想修复这些。谢谢你的建议@厄立特里亚人