2013-10-24 99 views
1

郵件的例子,我下載了以下例子:發送使用javax.mail

https://app.box.com/shared/etj0lts287r5lc1hqeex

當我直接執行的jar文件,它完美地工作。但是在使用Netbeans 7.0運行項目時,它會拋出一個錯誤:

error: could not connect to SMTP host:smtp.gmail.com,port:465 

可能是什麼問題?

這是我對按鈕的代碼的actionPerformed 私人無效sendButtonActionPerformed(EVT java.awt.event.ActionEvent中){

boolean isSent = true; 

    try { 

    Properties properties = new Properties(); 
properties.setProperty("mail.smtp.submitter", txtfrom.getText()); 
properties.setProperty("mail.smtp.auth", "true"); 
properties.setProperty("mail.smtp.host", txthostName.getText());  
    properties.put("mail.smtp.user", txtfrom.getText()); 
    properties.put("mail.smtp.port", txtPort.getText()); 
    properties.put("mail.smtp.socketFactory.port", txtPort.getText()); 
    properties.put("mail.smtp.starttls.enable","true"); 
    properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
    properties.put("mail.smtp.socketFactory.fallback", "false"); 


    Authenticator mailAuthenticator = new MailAuthenticator(); 
Session mailSession = Session.getDefaultInstance(properties,mailAuthenticator); 
    Message message = new MimeMessage(mailSession); 

    Transport transport = mailSession.getTransport("smtps"); 
    transport.connect("smtp.gmail.com",465,"[email protected]","12345"); 

    InternetAddress fromAddress = new InternetAddress(txtfrom.getText()); 
    InternetAddress toAddress = new InternetAddress(txtto.getText()); 

    message.setFrom(fromAddress); 
    message.setRecipient(RecipientType.TO, toAddress); 

    message.setSubject(txtsubject.getText()); 
    message.setText(txtcontent.getText()); 

    Transport.send(message); 

    } catch (Exception e) { 
     lblInfo.setText("ERROR:" + e.getMessage());    
     isSent = false; 
    } 

    if(isSent == true) { 
     sendButton.setEnabled(false); 
     lblInfo.setText("Your e-mail has been sent."); 
    } 

}  
+0

我嘗試更改端口號...請看看也許可以幫助您:https://support.google.com/mail/answer/13287?hl = en – ZaoTaoBao

+0

我嘗試更改端口號,但是相同錯誤被拋出。 –

回答

0

名單請考慮以下更改代碼,

//使用「 SMTPS」 :)

transport = mailSession.getTransport("smtps"); 

//端口465的工作進行身份驗證,並與Gmail,雅虎主機解決

+0

tried.getting相同的錯誤。 –

+0

你能分享你的代碼嗎? –