2012-06-14 38 views
6

Oto mój kod do wysyłania poczty:javax.mail.MessagingException: Nie można połączyć się z hostem SMTP?

import java.util.Properties; 
import javax.mail.Authenticator; 
import javax.mail.Message; 
import javax.mail.Message.RecipientType; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 
public class SendMail { 
    public void sendMail(String m_from,String m_to,String m_subject,String m_body){ 
     try { 
      Session m_Session; 
      Message m_simpleMessage; 
      InternetAddress m_fromAddress; 
      InternetAddress m_toAddress; 
      Properties m_properties; 

      m_properties  = new Properties(); 
      m_properties.put("mail.smtp.host", "usdc2spam2.slingmedia.com"); 
      m_properties.put("mail.smtp.socketFactory.port", "465"); 
      m_properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory"); 
      m_properties.put("mail.smtp.auth", "true"); 
      m_properties.put("mail.smtp.port", "9000"); 

      m_Session=Session.getDefaultInstance(m_properties,new Authenticator() { 
       protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication("aaaaa","[email protected]"); // username and the password 
       } 
      }); 

      m_simpleMessage = new MimeMessage(m_Session); 
      m_fromAddress = new InternetAddress(m_from); 
      m_toAddress  = new InternetAddress(m_to); 

      m_simpleMessage.setFrom(m_fromAddress); 
      m_simpleMessage.setRecipient(RecipientType.TO, m_toAddress); 
      m_simpleMessage.setSubject(m_subject); 

      m_simpleMessage.setContent(m_body, "text/html"); 

      //m_simpleMessage.setContent(m_body,"text/plain"); 

      Transport.send(m_simpleMessage); 
     } catch (MessagingException ex) { 
      ex.printStackTrace(); 
     } 
    } 
    public static void main(String[] args) { 
     SendMail send_mail = new SendMail(); 
     String empName = "xxxxx"; 
     String title ="<b>Hi !"+empName+"</b>"; 
     send_mail.sendMail("[email protected]", "[email protected]", "Please apply for leave for the following dates", title+"<br>by<br><b>HR<b>"); 
    } 
} 

ale gdy uruchamiam kod daje mi następujący wyjątek.

javax.mail.MessagingException: Could not connect to SMTP host: usdc2spam2.slingmedia.com, port: 9000; 
    nested exception is: 
    java.net.ConnectException: Connection refused: connect 
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934) 
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638) 
    at javax.mail.Service.connect(Service.java:317) 
    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 samples.SendMail.sendMail(SendMail.java:46) 
    at samples.SendMail.main(SendMail.java:55) 
Caused by: java.net.ConnectException: Connection refused: connect 
    at java.net.PlainSocketImpl.socketConnect(Native Method) 
    at java.net.PlainSocketImpl.doConnect(Unknown Source) 
    at java.net.PlainSocketImpl.connectToAddress(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:288) 
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:231) 
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900) 

kiedy ping to usdc2spam2.slingmedia.com daje mi odpowiedzieć bez żadnego problemu. Używam pomocy: windows 7

Proszę mi pomóc rozwiązać ten problem.

+1

ping nie jest dowodem na to, że możesz wysłać wiadomość e-mail. Wypróbuj 'telnet usdc2spam2.slingmedia.com 9000'. Lub jeśli ty (Win7) nie masz "telnetu", użyj np. [Putty] (http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html) –

+1

Co stanie się, gdy spróbujesz 'telnet usdc2spam2.slingmedia.com 9000'? Podejrzewam, że określasz niewłaściwy port we właściwości 'mail.smtp.port'. – beny23

Odpowiedz

4

To właśnie te dwie linie, które rzucał mi problem:

m_properties.put("mail.smtp.socketFactory.port", "465"); 
    m_properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory"); 

i dodaje ten wiersz:

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

Po usunięciu i dodaniu powyższych wierszy kodu to działało dobrze.

+3

powinien wyjaśnić, dlaczego ta linia nie jest potrzebna – SAR

+0

Udało się, ale nie usunąłem powyżej dwóch linii! – vnshetty

+0

nie działa dla mnie. –

2

Co powoduje, że problem jest tam w ślad stosu:

java.net.ConnectException: Connection refused: connect 

jest potrzebne hasło, aby połączyć się z serwerem SMTP? Czy na pewno używasz właściwych ustawień (jak w przypadku numeru portu)? Czy jesteś za serwerem proxy lub zaporą ogniową? Czy możesz używać tych ustawień w zwykłym programie pocztowym (np. Thunderbird) i wysyłać wiadomości e-mail?

2

Spróbuj dodać port 9000 do reguł ruchu przychodzącego w zaporze systemu Windows.

1

Ten wyjątek zwykle występuje, gdy nie ma podsłuchu serwisowego na porcie, z którym próbujesz się połączyć.

Spróbuj się połączyć, używając putty lub telnet. Mogę się założyć, że dostaniesz ten sam błąd.

Sprawdź te rzeczy:

  • Nazwa hosta i port starasz się połączyć,
  • Serwer nasłuchuje poprawnie i
  • Istnieje blokuje połączenie nie firewall.
Powiązane problemy