2013-03-12 79 views
0

我遇到了發送電子郵件的問題。我有良好的SMTP和端口,而是因爲這個錯誤而無法發送的消息:發送Gmail錯誤

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1 

下面是代碼:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
     final String username = "[email protected]"; 
     final String password = "my password"; 
     Properties props = System.getProperties(); 
     props.put("mail.smtp.auth.", "true"); 
     props.put("mail.smtp.starttls.enable", "true"); 
     props.put("mail.smtp.host", "smtp.gmail.com"); 
     props.put("mail.smtp.port", "465"); 
     Session session; 
     session = Session.getInstance(props, 
       new javax.mail.Authenticator() { 
      @Override 
      protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication(username, password); 
      } 
     }); 
     try { 
      // Create a default MimeMessage object. 
      MimeMessage message = new MimeMessage(session); 
      // Set From: header field of the header. 
      message.setFrom(new InternetAddress("[email protected]")); 
      // Set To: header field of the header. 
      message.setRecipient(Message.RecipientType.TO, 
        new InternetAddress("[email protected]")); 
      //subject message 
      message.setSubject("Help Needed"); 
      // Now set the actual message 
      message.setText("I need help please help me with this program"); 
      message.setContent("<h:body style=background-color :white;font-family:verdana;color:#0066CC;>" 
        + "If you are seeing this its OK !!!<br/><br/>" 
        + "</body>", "text/html; charset=utf-8"); 
      // Send message 
      Transport.send(message); 
      System.out.println("Sent message successfully..."); 
     } catch (MessagingException e) { 
      throw new RuntimeException(e); 
      //System.out.println("MessagingException: "+mex.getMessage()); 
      //JOptionPane.showMessageDialog(null, "[!]Cant connect to the database."); 
     } 
    }           

,如果你知道我問daniweb但請回復不能快速響應...... 感謝

+0

檢查這個(HTTP:// stackoverflow.com/questions/46663/how-to-send-an-email-by-java-application-using-gmail-yahoo-hotmail) – 2013-03-12 21:50:48

+0

我已經cheched,但沒有工作給了我很多錯誤... – user2133393 2013-03-12 22:02:12

+0

你可能有防火牆阻止你。你是否在防火牆網絡?如果您使用Windows,當您運行命令「telnet smtp.gmail.com 465」(不含引號)時,您會看到什麼?你的Windows防火牆阻止你嗎? – Anand 2013-03-12 22:02:31

回答

1

您可能要做到這一點:

Properties props = new Properties(); 
props.put("mail.smtp.auth", "true"); 

相反的:

Properties props = System.getProperties(); 
props.put("mail.smtp.auth.", "true"); 

你必須在mail.smtp.auth末點,並使用所有的系統屬性似乎是一個壞主意。

下面是一個例子http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/

+0

我tryed,已經仍然不能:/ – user2133393 2013-03-12 22:41:00

+0

順便說一句,我有4代碼tryed,沒有一個工作... – user2133393 2013-03-12 22:44:12

+0

同樣的錯誤?不同?如何運行「ping smtp.gmail.com」或「telnet smtp.gmail.com 587」?你的代碼中也有587端口,但錯誤中有465端口。可能是你沒有運行你認爲你正在運行的東西。 – jdb 2013-03-12 22:46:19

1

我也有這個問題,這是解決禁用avast和我的代碼,它看起來像這樣做了一些變化:

public void simpleEmail2(String to, String subject, String message){ 
     Properties props = new Properties(); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.starttls.enable", "true"); 
     props.put("mail.smtp.host", "smtp.gmail.com"); 
     props.put("mail.smtp.port", "587"); 

     Session session = Session.getInstance(props, 
      new javax.mail.Authenticator() { 
      protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication(username, password); 
      } 
      }); 

     try { 

      Message mmessage = new MimeMessage(session); 
      mmessage.setFrom(new InternetAddress(username)); 
      mmessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); 
      mmessage.setSubject("Testing Subject"); 
      mmessage.setText("Dear Mail Crawler," 
       + "\n\n No spam to my email, please!"); 

      Transport.send(mmessage); 

      System.out.println("Done"); 

     } catch (MessagingException e) { 
      throw new RuntimeException(e); 
     } 
    } 
相關問題