gpt4 book ai didi

java - java/servlet中发送邮件出错

转载 作者:太空宇宙 更新时间:2023-11-04 11:17:26 25 4
gpt4 key购买 nike

我正在尝试使用java发送电子邮件。但我遇到如下错误

javax.mail.AuthenticationFailedException: failed to connect, no password specified

当我通过正确的电子邮件 ID 和密码进行身份验证时,为什么会收到此错误?

这是我的代码

import java.io.IOException;
import java.net.PasswordAuthentication;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class TestMail
*/
@WebServlet("/TestMail")
public class TestMail extends HttpServlet {
private static final long serialVersionUID = 1L;


public TestMail() {
super();

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
todo(request,response);

}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
todo(request,response);

}

private void todo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf8");
response.setCharacterEncoding("utf8");

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.host","smtp.gmail.com");
props.put("mail.smtp.post","587");



Session session = Session.getDefaultInstance(props,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"testing@gmail.com", "testing123");
}
});

Message message=new MimeMessage(session);
try {
message.setFrom(new InternetAddress("testing@gmail.com","hello"));

message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("testing@gmail.com"));
message.setSubject("Testing Email");
message.setText("hello this is testing mail \n \n Congrets");
Transport.send(message);
System.out.println("Mail Sent Successfully");
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

my error image

最佳答案

JavaMail Authenticator 位于 javax.mail 包中,与同名的 java.net 类不同。两者不共享相同的身份 validator ,因为 JavaMail API 与 Java 1.1 一起使用,而 Java 1.1 没有 java.net 变体。

引用这个: http://www.rgagnon.com/javadetails/java-0538.html

关于java - java/servlet中发送邮件出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45301969/

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