gpt4 book ai didi

java - "java.net.connectionException"显示连接被拒绝

转载 作者:行者123 更新时间:2023-11-29 05:23:43 26 4
gpt4 key购买 nike

我正在尝试使用 java mail api 并在我的 servlet 中使用以下代码来发送邮件。我找不到解决错误的方法。

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// Recipient's email ID needs to be mentioned.
String to = "xyz@gmail.com";

// Sender's email ID needs to be mentioned
String from = "abc.com";

// Assuming you are sending email from localhost
String host = "localhost";

// Get system properties
Properties properties = System.getProperties();

// Setup mail server
properties.setProperty("mail.smtp.host", host);

// Get the default Session object.
Session session = Session.getDefaultInstance(properties);

// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();

try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: header field
String subject=request.getParameter("subject");
String content=request.getParameter("mail");


message.setSubject(subject);
// Now set the actual message
message.setText(content);
// Send message
Transport.send(message);
String title = "Send Email";
String res = "Sent message successfully....";
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " +
"transitional//en\">\n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor=\"#f0f0f0\">\n" +
"<h1 align=\"center\">" + title + "</h1>\n" +
"<p align=\"center\">" + res + "</p>\n" +
"</body></html>");
}catch (MessagingException mex) {
mex.printStackTrace();
}

}

我的控制台出现以下错误:

Caused by: java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) at java.net.AbstractPlainSocketImpl.connect(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:312) at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:236) at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2019) ... 29 more

最佳答案

您没有在 localhost 上运行 SMTP 服务器,并且您告诉 API 使用 localhost 作为您的邮件服务器。

您需要安装本地 SMTP 服务器,或者将 smtp 主机名设置为您有权访问的服务器,例如:

      String host = "smtp.yourisp.com"; // real server name required here

// Get system properties
Properties properties = System.getProperties();

// Setup mail server
properties.setProperty("mail.smtp.host", host);

关于java - "java.net.connectionException"显示连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23590320/

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