2011-04-25 71 views
1

我第一次使用Java Mail API,但無法正常工作。我的服務器需要驗證,所以我不得不使用它。我不斷收到以下錯誤:Java郵件驗證錯誤

> 250-PIPELINING 250-SIZE 40960000 
> 250-ETRN 250-STARTTLS 250-AUTH PLAIN 
> LOGIN 250-AUTH=PLAIN LOGIN 
> 250-ENHANCEDSTATUSCODES 250 8BITMIME 
> DEBUG SMTP: Found extension 
> "PIPELINING", arg "" DEBUG SMTP: Found 
> extension "SIZE", arg "40960000" DEBUG 
> SMTP: Found extension "ETRN", arg "" 
> DEBUG SMTP: Found extension 
> "STARTTLS", arg "" DEBUG SMTP: Found 
> extension "AUTH", arg "PLAIN LOGIN" 
> DEBUG SMTP: Found extension 
> "AUTH=PLAIN", arg "LOGIN" DEBUG SMTP: 
> Found extension "ENHANCEDSTATUSCODES", 
> arg "" DEBUG SMTP: Found extension 
> "8BITMIME", arg "" DEBUG SMTP: Attempt 
> to authenticate DEBUG SMTP: check 
> mechanisms: LOGIN PLAIN AUTH LOGIN 334 
> Base64text base64text 334 base64text 
> base64text 235 2.7.0 Authentication 
> successful DEBUG: getProvider() 
> returning 
> javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun 
> Microsystems, Inc] DEBUG SMTP: useEhlo 
> true, useAuth true 
> javax.mail.AuthenticationFailedException: 
> failed to connect, no password 
> specified? 
>   at javax.mail.Service.connect(Service.java:329) 
>   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 Mailman.main(Mailman.java:61) 

正如你所看到的,我得到一個「驗證成功」的消息,但隨後它踢我出去的「AuthenticationFailedException。」我難倒...

這裏的源的相關部分:

properties.setProperty("mail.smtp.host", host);    
     properties.setProperty("mail.smtp.port", "25"); 


     properties.setProperty("mail.smtp.user", "myemailhere"); 
     properties.setProperty("mail.smtp.password", "mypasshere"); 
     properties.setProperty("mail.smtp.auth", "true");  
     properties.setProperty("mail.debug", "true"); 


     // 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("This is the Subject Line!"); 

     // Now set the actual message 
     message.setText("This is actual message"); 

     Transport transport = session.getTransport("smtp"); 
      transport.connect(host, 25, "myemailhere", "mypasshere"); 
      message.saveChanges(); 
      Transport.send(message); 



     // Send message 
     Transport.send(message); 
     System.out.println("Sent message successfully...."); 
     }catch (MessagingException mex) { 
     mex.printStackTrace();   
     }  

任何建議,將不勝感激...

回答

1

我改成了

message.setText("This is actual message"); 

      Transport transport = session.getTransport("smtp"); 
       transport.connect(null,smtpUser,smtpPassword); //host, 25, "myemailhere", "mypasshere"); 
       message.saveChanges(); 
       transport.sendMessage(message,message.getAllRecipients()); 

//    Transport.send(message); 



      // Send message 
      // Transport.send(message); 
      System.out.println("Sent message successfully...."); 

它的工作

+0

我通常不是所有大寫的支持者,但是......謝謝!我幾個小時都在盯着這件事。你的解決方案就像一個魅力。 – Jeff 2011-04-25 14:10:44

+0

沒問題。我找到了Javamail API ......這是慈善術語。這一切都有效,但從a點到b點並不總是很清楚 – MJB 2011-04-25 16:46:24