我正在尝试通过我的网站(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);
// Send the complete message parts
message.setContent(multipart);
// Send message
SMTPTransport t = (SMTPTransport) session.getTransport("smtp");
// connect
t.connect(new Socket(host, 25));
// send
t.sendMessage(message, message.getAllRecipients());
System.out.println("Response: " + t.toString());
t.close();
Files.deleteIfExists(Paths.get("C:\\Temp\\ReplicationCheck.pdf"));
} catch (MessagingException mex) {
mex.printStackTrace();
} catch (IOException ex) {
Logger.getLogger(SendEmail.class.getName()).log(Level.SEVERE, null, ex);
}
}
答案 0 :(得分:0)
192.168.52.95
是一个LAN地址。在开发环境中,可能有一个邮件服务器在等待东西,而在服务器环境中,192.168.52.95
上什么也没有。
您可能想从属性文件中读取主机,这样您就可以在不同的环境中使用不同的主机。
答案 1 :(得分:0)
我可以使用以下代码解决问题。 我的朋友告诉我,java在apache上部署后,java需要有效的用户名和密码才能连接到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);
}
}