有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java自动发送电子邮件

我正在尝试设置一个可以自动发送电子邮件的程序。然而,第一步对我来说根本不起作用,它给出了一个错误:

com.sun.mail.util.MailConnectException: Couldn't connect to host, port: 127.0.0.1, 25; timeout -1;
  nested exception is:
    java.net.ConnectException: Connection refused: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2209)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:740)
    at javax.mail.Service.connect(Service.java:366)
    at javax.mail.Service.connect(Service.java:246)
    at javax.mail.Service.connect(Service.java:195)
    at javax.mail.Transport.send0(Transport.java:254)
    at javax.mail.Transport.send(Transport.java:124)
    at server.main(server.java:46)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.base/java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.base/java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:400)
    at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:243)
    at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:225)
    at java.base/java.net.PlainSocketImpl.connect(PlainSocketImpl.java:148)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:402)
    at java.base/java.net.Socket.connect(Socket.java:591)
    at java.base/java.net.Socket.connect(Socket.java:540)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:353)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:239)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2175)
    ... 7 more

以下是代码:

// File Name SendEmail.java

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class server {

   public static void main(String [] args) {    
      // Recipient's email ID needs to be mentioned.
      String to = "my other email properly written";

      // Sender's email ID needs to be mentioned
      String from = "my email properly written";

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

      // 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("This is the Subject Line!");

         // Now set the actual message
         message.setText("This is actual message");

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

有人知道为什么会发生这种情况以及如何解决吗?我的电子邮件实际上是打开的等,因为它是本地主机,我不知道为什么它不会工作


共 (0) 个答案