简体   繁体   English

执行查询到数据库后无法连接到SMTP服务器

[英]Unable to connect to SMTP server after executing a query to database

My program is initially connecting to databse server and executing a query and then trying to send an e-mail through smtp server. 我的程序最初连接到数据库服务器并执行查询,然后尝试通过smtp服务器发送电子邮件。

The query is executing successfully but the program is unable to connect to smtp server. 查询正在成功执行,但程序无法连接到smtp服务器。

If I'm commenting databse connection code mail sending code is executing properly. 如果我正在评论数据库连接代码邮件发送代码正在执行。

And one more case If i'm wrting mail sending code first and then databse execution code after program is working properly. 还有一个案例如果我首先发送邮件发送代码然后在程序正常工作后数据库执行代码。

The question is how I can send a mail after executing database queries in the same program. 问题是如何在同一程序中执行数据库查询后发送邮件。

Here is my code. 这是我的代码。

package com.ravi;

import java.util.Properties;
import java.sql.*;

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 TestMail {

public static void main(String[] args) throws Exception{

        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","****");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("select * from emp");
        while(rs.next()){
            System.out.println(rs.getString("eno"));
            System.out.println(rs.getString("ename"));
        }
        rs.close();
        stmt.close();
        con.close();
        //System.exit(1);


        String to = "****@***.co.in";//change accordingly  
        String from = "*****@***.co.in";//change accordingly  
        String host = "***.***.co.in";//or IP address  

       //Get the session object  
        Properties properties = System.getProperties();  
        properties.setProperty("mail.smtp.host", host); 
        properties.setProperty("java.net.preferIPv4Stack", "true");
        Session session = Session.getDefaultInstance(properties);  

       //compose the message  
        try{  
           MimeMessage message = new MimeMessage(session);  
           message.setFrom(new InternetAddress(from));  
           message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
           message.setSubject("Ping");  
           message.setText("Hello, this is example of sending email  ");  

           // Send message  
           Transport.send(message);  
           System.out.println("message sent successfully....");  

        }catch (MessagingException mex) {mex.printStackTrace();}  

}

} }

Stack trace 堆栈跟踪

javax.mail.MessagingException: Could not connect to SMTP host: ***.***.co.in, port: 25;
  nested exception is:
    java.net.SocketException: Network is unreachable: connect
    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.ravi.TestMail.main(TestMail.java:50)
Caused by: java.net.SocketException: Network is unreachable: 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:321)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:237)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
    ... 7 more

May not be the best but below code does the work. 可能不是最好的,但是下面的代码可以完成工作。

I'm creating session before i'm getting the databse connection. 在我获得数据库连接之前,我正在创建会话。

I moved creating Session code before the database connection code. 我在数据库连接代码之前移动了创建Session代码。

import java.util.Properties;
import java.sql.*;

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 TestMail {

public static void main(String[] args) throws Exception{


    String to = "****@***.co.in";//change accordingly  
    String from = "*****@***.co.in";//change accordingly  
    String host = "***.***.co.in";//or IP address  

   //Get the session object  
    Properties properties = System.getProperties();  
    properties.setProperty("mail.smtp.host", host); 
    properties.setProperty("java.net.preferIPv4Stack", "true");
    Session session = Session.getDefaultInstance(properties);  



    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","****");
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("select * from emp");
    while(rs.next()){
        System.out.println(rs.getString("eno"));
        System.out.println(rs.getString("ename"));
    }
    rs.close();
    stmt.close();
    con.close();
    //System.exit(1);



   //compose the message  
    try{  
       MimeMessage message = new MimeMessage(session);  
       message.setFrom(new InternetAddress(from));  
       message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
       message.setSubject("Ping");  
       message.setText("Hello, this is example of sending email  ");  

       // Send message  
       Transport.send(message);  
       System.out.println("message sent successfully....");  

    }catch (MessagingException mex) {mex.printStackTrace();}  

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM