javax.mail.MessaginException:无法连接到SMTP主机:localhost,端口:25;

javax.mail.MessaginException:无法连接到SMTP主机:localhost,端口:25;,java,email,jakarta-mail,postfix-mta,Java,Email,Jakarta Mail,Postfix Mta,这是我的密码 public class Email { static Properties props; public static void main(String[] args){ String from = "some email"; String to= "some other email"; String host = "localhost"; props = System.getProperties();

这是我的密码

public class Email {
    static Properties props;
    public static void main(String[] args){
        String from = "some email";
        String to= "some other email";
        String host = "localhost";
        props = System.getProperties();
        props.setProperty("mail.host", host);
        props.put("mail.transport.protocol", "smtp");
        Session session = Session.getDefaultInstance(props,null);
        try{
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject("This is the Subject Line!");
            message.setText("This is actual 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
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
    at javax.mail.Service.connect(Service.java:295)
    at javax.mail.Service.connect(Service.java:176)
    at javax.mail.Service.connect(Service.java:125)
    at javax.mail.Transport.send0(Transport.java:194)
    at javax.mail.Transport.send(Transport.java:124)
    at com.cisco.ci.support.email.Email.main(Email.java:51)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    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:321)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:237)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
    ... 12 more

我的mac上似乎没有smtp服务器?有什么解决方案吗?

您需要在mac电脑上安装一个smtp服务器,这并不是那么简单,或者使用现有的smtp服务器。显然,您将需要该服务器的帐户,因为没有身份验证的smtp服务器并不多


例如,创建一个googlemail帐户,然后检查:例如如何使用它

在Mac上安装SMTP,或将代码更改为指向真正的SMTP服务器(或SMTP服务器)。您将在中找到其他有用的信息。