Java Mail身份验证错误

时间:2011-04-25 06:09:49

标签: java email

我第一次使用Java Mail API而且我无法正常工作。我的服务器需要身份验证,所以我不得不使用它。我一直收到以下错误:

> 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 个答案:

答案 0 :(得分: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....");

并且有效