gpt4 book ai didi

java - 如何解决 javax.mail.AuthenticationFailedException 问题

转载 作者:行者123 更新时间:2023-12-01 13:26:20 25 4
gpt4 key购买 nike

该程序尝试发送电子邮件,但引发运行时异常:AuthenticationFailedException 我已经提到了 stackoverflow 问题并回答了我已经实现的相同问题,但我仍然遇到这样的异常,任何人都可以解决此问题。< br/>异常

 javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:267)
at javax.mail.Service.connect(Service.java:137)
at javax.mail.Service.connect(Service.java:86)
at javax.mail.Transport.send0(Transport.java:150)
at javax.mail.Transport.send(Transport.java:80)
at com.treamis.transport.vehicle.javaMail.send(javaMail.java:81)
at com.treamis.transport.vehicle.MysqlBackup.backupDataWithDatabase(Mysq
lBackup.java:97)
at com.treamis.transport.vehicle.MysqlBackup.run(MysqlBackup.java:118)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
Sms sent xl sheet is generated is generated

java邮件代码

import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class javaMail {

private String SMTP_PORT = "465";
private String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
private String SMTP_HOST_NAME = "smtp.gmail.com";
private Properties smtpProperties;

public javaMail() {
initProperties();
}

private void initProperties() {
smtpProperties = new Properties();
smtpProperties.put("mail.smtp.host", SMTP_HOST_NAME);
smtpProperties.put("mail.smtp.auth", "true");
smtpProperties.put("mail.debug", "true");
smtpProperties.put("mail.smtp.port", SMTP_PORT);
smtpProperties.put("mail.smtp.socketFactory.port", SMTP_PORT);
smtpProperties.put("mail.smtp.socketFactory.class", SSL_FACTORY);
smtpProperties.put("mail.smtp.socketFactory.fallback", "false");
}

public String send(String[] to, final String from, final String pwd, String subject, String body) {
javaMail tjm = new javaMail();
try {
Properties props = tjm.getSmtpProperties();
// -- Attaching to default Session, or we could start a new one --
// Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
// protected PasswordAuthentication getPasswordAuthentication() {
// return new PasswordAuthentication(from, pwd);
// }
// });
Session session = Session.getInstance(props, new GMailAuthenticator(from, pwd));
// Session session = Session.getInstance(props, new javax.mail.Authenticator() {
// protected PasswordAuthentication getPasswordAuthentication() {
// return new PasswordAuthentication(userName, password);
// }
//});
Message msg = new MimeMessage(session);
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Test mail one");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(body);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(body);
multipart.addBodyPart(messageBodyPart);
msg.setContent(multipart);
msg.setFrom(new InternetAddress(from));
InternetAddress[] addressTo = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++) {
addressTo[i] = new InternetAddress(to[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
// msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
msg.setSubject(subject);
msg.setSentDate(new Date());
Transport.send(msg);
System.out.println("Message sent OK.");
return "success";
} catch (Exception ex) {
ex.printStackTrace();
ex.getMessage();
}
return null;
}

public Properties getSmtpProperties() {
return smtpProperties;
}

public void setSmtpProperties(Properties smtpProperties) {
this.smtpProperties = smtpProperties;
}
}

GMailAuthenticator 代码 */

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;


class GMailAuthenticator extends Authenticator {
String user;
String pw;
public GMailAuthenticator (String username, String password)
{
super();
this.user = username;
this.pw = password;
}
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(user, pw);
}
}

最佳答案

AuthenticationFailedException 表示服务器认为您提供了错误的用户名或密码。你会说“当然我没有给它错误的用户名或密码,我没那么蠢!”好吧,服务器不同意你的观点。

尝试打开JavaMail session debugging ,协议(protocol)跟踪可能会提供有关出现问题的其他线索。您可能还需要将“mail.debug.auth”属性设置为“true”以获取有关登录过程的其他详细信息。

另外,请参阅 JavaMail 常见问题解答 common mistakes在您的代码中。

并确保没有防病毒软件或防火墙拦截您连接服务器的尝试。

关于java - 如何解决 javax.mail.AuthenticationFailedException 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21796070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com