为了账号安全,请及时绑定邮箱和手机立即绑定

如何修复“java.net.ConnectException:连接被拒绝:连接”

如何修复“java.net.ConnectException:连接被拒绝:连接”

PIPIONE 2023-07-28 15:10:29
我正在尝试通过我的网站(JSF 网站)发送电子邮件,该网站部署在 apache 服务器上,但我不断收到“连接被拒绝:连接”消息,但是如果我直接从 NetBeans 运行 Java 应用程序中的代码,则代码可以完美运行。抱歉,如果我在帖子中犯了一些错误,你可以说这是我第一次发帖。:)    public void sendReplicationCheckResult() {    String to = "JcbSupportingSystem@jcbank.com.jo";    // Sender's email ID needs to be mentioned    String from = "Palestine.IT@jcbank.com.jo";    // Assuming you are sending email from localhost    String host = "192.168.52.95";    // Get system properties    Properties properties = System.getProperties();    // Setup mail server    properties.setProperty("mail.smtp.host", host);    properties.put("mail.smtp.auth", "false");    properties.setProperty("mail.smtp.port", "25");    // 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("Replication Check Results");        // Create the message part         BodyPart messageBodyPart = new MimeBodyPart();        messageBodyPart.setContent(message, "text/html");        // Create a multipart message        Multipart multipart = new MimeMultipart();        // Set text message part    multipart.addBodyPart(messageBodyPart);        // Part two is attachment        messageBodyPart = new MimeBodyPart();        String filename = "C:\\Temp\\ReplicationCheck.pdf";        DataSource source = new FileDataSource(filename);        messageBodyPart.setDataHandler(new DataHandler(source));        messageBodyPart.setFileName(filename);        multipart.addBodyPart(messageBodyPart);
查看完整描述

2 回答

?
墨色风雨

TA贡献1853条经验 获得超6个赞

192.168.52.95是LAN地址。虽然在您的开发环境中可能有一个邮件服务器在等待,但在您的服务器环境中没有任何东西192.168.52.95

您可能想从属性文件中读取主机,这样您就可以在不同的环境中使用不同的主机。


查看完整回答
反对 回复 2023-07-28
?
汪汪一只猫

TA贡献1898条经验 获得超8个赞

我能够使用以下代码解决该问题。我的朋友告诉我,java部署在apache上后需要有效的用户名和密码才能连接到smtp服务器,所以我添加了它们,但在我使用sslsocket连接非正常套接字后我的问题解决了。(t.connect(new Socket(host, 25));) //旧方式 //新代码使用 (t.connect(new SSLSocket(SMTP_SERVER, 25))。


private static final String SMTP_SERVER = "smtp server";

private static final String USERNAME = "username";

private static final String PASSWORD = "password";


private static final String EMAIL_FROM = "email from";

private static final String EMAIL_TO = "email to";

private static final String EMAIL_TO_CC = "cc";


private static final String EMAIL_SUBJECT = "Subject";


public void sendReplicationCheckResult() {


    // Get system properties

    Properties properties = System.getProperties();


    // Setup mail server

    properties.setProperty("mail.transport.protocol", "smtp");

    properties.setProperty("mail.smtp.host", SMTP_SERVER);

    properties.setProperty("mail.smtp.port", "25");

    properties.put("mail.smtp.starttls.enable", "true");

    properties.put("mail.smtp.auth", "false");


    // 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(EMAIL_FROM));


        // Set To: header field of the header.

        message.addRecipient(Message.RecipientType.TO, new InternetAddress(EMAIL_TO));


        // Set Subject: header field

        message.setSubject(EMAIL_SUBJECT);


        // Create the message part 

        BodyPart messageBodyPart = new MimeBodyPart();


        // Fill the message

//            messageBodyPart.setText("This is message body");

        // Create a multipart message

        Multipart multipart = new MimeMultipart();


        // Set text message part

 //multipart.addBodyPart(messageBodyPart);

        // Part two is attachment

        messageBodyPart = new MimeBodyPart();

        String filename = "File Path";

        DataSource source = new FileDataSource(filename);

        messageBodyPart.setDataHandler(new DataHandler(source));

        messageBodyPart.setFileName("File Name");

        multipart.addBodyPart(messageBodyPart);


        // Send the complete message parts

        message.setContent(multipart);


        // Send message

        SMTPTransport t = (SMTPTransport) session.getTransport("smtp");


        // connect

        t.connect(new SSLSocket(SMTP_SERVER, 25) {

            @Override

            public String[] getSupportedCipherSuites() {

                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

            }


            @Override

            public String[] getEnabledCipherSuites() {

                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

            }


            @Override

            public void setEnabledCipherSuites(String[] strings) {

                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

            }


            @Override

            public String[] getSupportedProtocols() {

                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

            }


            @Override

            public String[] getEnabledProtocols() {

                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

            }


            @Override

            public void setEnabledProtocols(String[] strings) {

                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

            }


            @Override

            public SSLSession getSession() {

                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

            }


            @Override

            public void addHandshakeCompletedListener(HandshakeCompletedListener hl) {

                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

            }


            @Override

            public void removeHandshakeCompletedListener(HandshakeCompletedListener hl) {

                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

            }


            @Override

            public void startHandshake() throws IOException {

                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

            }


            @Override

            public void setUseClientMode(boolean bln) {

                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

            }


            @Override

            public boolean getUseClientMode() {

                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

            }


            @Override

            public void setNeedClientAuth(boolean bln) {

                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

            }


            @Override

            public boolean getNeedClientAuth() {

                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

            }


            @Override

            public void setWantClientAuth(boolean bln) {

                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

            }


            @Override

            public boolean getWantClientAuth() {

                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

            }


            @Override

            public void setEnableSessionCreation(boolean bln) {

                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

            }


            @Override

            public boolean getEnableSessionCreation() {

                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

            }

        });

        // send

        t.sendMessage(message, message.getAllRecipients());


        System.out.println("Response: " + t.toString());


        t.close();

        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(null, "Email was sent Successfully"));

    } catch (IOException ex) {

        Logger.getLogger(SendEmail.class.getName()).log(Level.SEVERE, null, ex);

        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(null, ex.getLocalizedMessage()));

    } catch (MessagingException ex) {

        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(null, ex.getLocalizedMessage()));

        Logger.getLogger(SendEmail.class.getName()).log(Level.SEVERE, null, ex);

    }

}



查看完整回答
反对 回复 2023-07-28
  • 2 回答
  • 0 关注
  • 177 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信