我的程序最初连接到数据库服务器并执行查询,然后尝试通过smtp服务器发送电子邮件。
查询正在成功执行,但程序无法连接到smtp服务器。
如果我正在评论数据库连接代码,则邮件发送代码正在正常执行。
还有一个案例如果我是先发送邮件发送代码然后在程序运行正常后数据库执行代码。
问题是如何在同一程序中执行数据库查询后发送邮件。
这是我的代码。
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();}
}
}
堆栈跟踪
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
答案 0 :(得分:0)
可能不是最好的,但是下面的代码可以完成工作。
我在获取数据库连接之前创建了会话。
我在数据库连接代码之前移动了创建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();}
}