gpt4 book ai didi

java - 发送邮件错误,javax.mail.MessagingException : Could not connect to SMTP host: localhost, port: 25;

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:46:28 25 4
gpt4 key购买 nike

这是我的代码

import java.util.Properties;
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;


public class MailSendClass {
public static void main (String [] args){

// Recipient's email ID needs to be mentioned.
String to = "abc82@gmail.com";

// Sender's email ID needs to be mentioned
String from = "xyz@gmail.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);

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
message.setSubject("Thanks for registering on our website!");

// Now set the actual message
message.setText("Welcome To Job Portal !!!! Again Thanks ");

// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}

}
}

我每次都会收到这个错误

javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1706)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
at javax.mail.Service.connect(Service.java:291)
at javax.mail.Service.connect(Service.java:172)
at javax.mail.Service.connect(Service.java:121)
at javax.mail.Transport.send0(Transport.java:190)
at javax.mail.Transport.send(Transport.java:120)
at MailSendClass.main(MailSendClass.java:58)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:284)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:227)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1672)
... 7 more
BUILD SUCCESSFUL (total time: 3 seconds)

我没有得到发生这种情况的错误。请帮我解决这个错误。

最佳答案

错误不言自明:javax.mail.MessagingException:无法连接到 SMTP 主机:localhost,端口:25;

你在本地主机上没有 SMTP 服务器,但你在那里配置它:

  // Assuming you are sending email from localhost
String host = "localhost";
...
// Setup mail server
properties.setProperty("mail.smtp.host", host);

所以你必须:

  • 将本地 SMTP 服务器配置为本地系统上的中继(Postfix 或 sendmail 是两个众所周知的服务器)
  • 配置一个虚拟服务器,该服务器仅跟踪邮件请求但甚至不尝试投递邮件(众所周知,Python 具有开箱即用的此类虚拟服务器)
  • 或使用允许您使用的服务器配置您的应用程序 - 在公司环境中联系您的系统管理员,或在个人环境中联系您的 ISP。无论如何,您甚至需要它来配置真正的中继。

关于java - 发送邮件错误,javax.mail.MessagingException : Could not connect to SMTP host: localhost, port: 25;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32882595/

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