gpt4 book ai didi

java 邮件 javax.mail.AuthenticationFailedException

转载 作者:行者123 更新时间:2023-12-01 05:28:15 25 4
gpt4 key购买 nike

嗨,我已经编写了一个使用 java 邮件从我的服务器发送邮件的代码

这是我的代码

Properties pros = new Properties();
pros.put("mail.smtp.host", "my Ip Adress");
pros.put("mail.smtp.auth", "true");
pros.put("mail.smtp.port", "25");
Session mailSession = Session.getDefaultInstance(pros,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("emailId","password");
}
});
Message message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress("sendingAddress"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("recieverAddress"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler," +
"\n\n No spam to my email, please!");
Transport.send(message);
return "SUCCESS";

我在代码中提供的所有详细信息都是正确的,我的意思是属性设置,我已经检查过。但不幸的是它显示了一些错误消息,例如身份验证失败

错误信息是这样的

javax.mail.AuthenticationFailedException

有人对此有任何想法吗?

我的项目使用 struts2 框架。

最佳答案

使用此代码..!您正在使用传统方法从邮件服务器获取身份验证

import java.util.*;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.Message;
public void Mail(String to[], String subject, String content_message)
throws MessagingException {

boolean debug = false;

try {
String host = "smtp.gmail.com";
String from = "yourmail@gmail.com";
String pass = "yourpwd";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++) {
toAddress[i] = new InternetAddress(to[i]);
}
System.out.println(Message.RecipientType.TO);
for (int i = 0; i < toAddress.length; i++) {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject(subject);

message.setContent(content_message, "text/html; charset=\"UTF-8\"");
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}

catch (Exception e) {
System.out.println("Unable to connect");
}
}

每当需要发送邮件时调用构造函数..

关于java 邮件 javax.mail.AuthenticationFailedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9427623/

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