2012-06-14 12 views
6

segue è il mio codice per inviare la posta:javax.mail.MessagingException: Impossibile connettersi all'host 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>"); 
    } 
} 

ma quando ho eseguito il codice che mi dà la seguente eccezione.

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) 

quando faccio un rumore metallico questo usdc2spam2.slingmedia.com mi dà rispondo senza alcun problema. Sto usando windows 7

Per favore aiutami a risolvere questo.

+1

ping non è una prova che è possibile inviare una e-mail. Prova 'telnet usdc2spam2.slingmedia.com 9000'. Oppure se tu (Win7) non hai "telnet", usa ad es. [Putty] (http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html) –

+1

Cosa succede quando provi 'telnet usdc2spam2.slingmedia.com 9000'? Sospetto che tu stia specificando la porta sbagliata nella proprietà 'mail.smtp.port'. – beny23

risposta

4

Si tratta di queste due linee che mi stava gettando il problema:

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

e ha aggiunto questa linea:

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

Dopo aver rimosso e aggiungendo le righe precedenti di codice ha funzionato bene.

+3

dovrebbe spiegare perché questa linea non è necessaria – SAR

+0

Ha funzionato, ma non rimuovere oltre due righe! – vnshetty

+0

non funziona per me. –

2

Quali sono le cause il problema è proprio lì nella traccia dello stack:

java.net.ConnectException: Connection refused: connect 

Avete bisogno di un password per connettersi al server SMTP? Sei sicuro di utilizzare le impostazioni corrette (come nel numero di porta)? Sei dietro un proxy o un firewall? Puoi usare queste impostazioni in un normale programma di posta (ad esempio Thunderbird) e inviare mail?

2

Provare ad aggiungere la porta 9000 alle regole in entrata nel firewall di Windows.

1

Questa eccezione si verifica in genere quando non è in ascolto alcun servizio sulla porta a cui si sta tentando di connettersi.

Provare a connettersi utilizzando putty o telnet. Posso scommettere che otterrai lo stesso errore.

Verificare queste cose:

  • nome host e la porta si sta cercando di connettersi,
  • Il server è in ascolto in modo corretto, e
  • Non c'è firewall che blocca la connessione.
Problemi correlati