2010-01-12 24 views
16

Sto facendo un sendMailServlet con JavaMail. Ho javax.mail.AuthenticationFailedException sulla mia uscita. Per favore qualcuno può aiutarmi? Grazie. CodiceCome risolvere il problema javax.mail.AuthenticationFailedException?

sendMailServlet:

try { 
     String host = "smtp.gmail.com"; 
     String from = "[email protected]"; 
     String pass = "pass"; 
     Properties props = System.getProperties(); 
     props.put("mail.smtp.starttls.enable", "true"); 
     props.put("mail.smtp.host", host); 
     props.put("mail.smtp.user", from); 
     props.put("mail.smtp.password", pass); 
     props.put("mail.smtp.port", "587"); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.debug", "true"); 

     Session session = Session.getDefaultInstance(props, null); 
     MimeMessage message = new MimeMessage(session); 
     Address fromAddress = new InternetAddress(from); 
     Address toAddress = new InternetAddress("[email protected]"); 

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

     message.setSubject("Testing JavaMail"); 
     message.setText("Welcome to JavaMail"); 
     Transport transport = session.getTransport("smtp"); 
     transport.connect(host, from, pass); 
     message.saveChanges(); 
     Transport.send(message); 
     transport.close(); 

    }catch(Exception ex){ 

     out.println("<html><head></head><body>"); 
     out.println("ERROR: " + ex); 
     out.println("</body></html>"); 
    } 

uscita su GlassFish 2.1:

DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false 
220 mx.google.com ESMTP 36sm10907668yxh.13 
DEBUG SMTP: connected to host "smtp.gmail.com", port: 587 
EHLO platform-4cfaca 
250-mx.google.com at your service, [203.126.159.130] 
250-SIZE 35651584 
250-8BITMIME 
250-STARTTLS 
250-ENHANCEDSTATUSCODES 
250 PIPELINING 
DEBUG SMTP: Found extension "SIZE", arg "35651584" 
DEBUG SMTP: Found extension "8BITMIME", arg "" 
DEBUG SMTP: Found extension "STARTTLS", arg "" 
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg "" 
DEBUG SMTP: Found extension "PIPELINING", arg "" 
STARTTLS 
220 2.0.0 Ready to start TLS 
EHLO platform-4cfaca 
250-mx.google.com at your service, [203.126.159.130] 
250-SIZE 35651584 
250-8BITMIME 
250-AUTH LOGIN PLAIN 
250-ENHANCEDSTATUSCODES 
250 PIPELINING 
DEBUG SMTP: Found extension "SIZE", arg "35651584" 
DEBUG SMTP: Found extension "8BITMIME", arg "" 
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN" 
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg "" 
DEBUG SMTP: Found extension "PIPELINING", arg "" 
DEBUG SMTP: Attempt to authenticate 
AUTH LOGIN 
334 VXNlcm5hbWU6 
aWpveWNlbGVvbmdAZ21haWwuY29t 
334 UGFzc3dvcmQ6 
MTIzNDU2Nzhf 
235 2.7.0 Accepted 
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc] 
DEBUG SMTP: useEhlo true, useAuth true 
+0

quando si verifica esattamente l'eccezione? Non riesco a vederlo nei tuoi registri –

risposta

20

È necessario implementare un costume Authenticator

import javax.mail.Authenticator; 
import javax.mail.PasswordAuthentication; 


class GMailAuthenticator extends Authenticator { 
    String user; 
    String pw; 
    public GMailAuthenticator (String username, String password) 
    { 
     super(); 
     this.user = username; 
     this.pw = password; 
    } 
    public PasswordAuthentication getPasswordAuthentication() 
    { 
     return new PasswordAuthentication(user, pw); 
    } 
} 

Ora usarlo nel Session

Session session = Session.getInstance(props, new GMailAuthenticator(username, password)); 

controllare anche le JavaMail FAQ

+0

Dolce, questo funziona per me! – PapaFreud

+0

@ n002213f: Come utilizzare questo GMAILAuthenticator quando sto ottenendo la sessione da mailservice.xml da una chiamata jndi (jboss). – Ashwin

+0

@Ashwin - dai un'occhiata alla risposta a una domanda simile http://stackoverflow.com/questions/3475971/configure-the-mail-service-xml-in-jboss-with-a-gmail-account – n002213f

2

mi mancava questo argomento oggetto autenticatore nella riga sotto

Session session = Session.getInstance(props, new GMailAuthenticator(username, password)); 

Questa linea risolto il mio problema ora posso inviare la posta attraverso il mio applicazione Java. Il resto del codice è semplice come sopra.

0

Il problema è, si sta creando un oggetto transport e si utilizza il suo metodo di connessione per autenticarsi. Tuttavia, si utilizza un metodo static per inviare il messaggio che ignora l'autenticazione eseguita dall'oggetto.

Quindi, è necessario utilizzare il metodo sendMessage(message, message.getAllRecipients()) sull'oggetto o utilizzare un autenticatore come suggerito da altri per ottenere l'autorizzazione attraverso la sessione.

Ecco il Java Mail FAQ, è necessario leggere.

0

Volevo solo condividere con voi:
Mi è capitato di ottenere questo errore dopo aver cambiato la macchina Digital Ocean (indirizzo IP). Apparentemente Gmail lo ha riconosciuto come un attacco di hacking. Dopo aver seguito le loro indicazioni e approvando il nuovo indirizzo IP, il codice è tornato e funzionante.

20

Ciao a tutti Questo errore proviene da Google sicurezza ... Questo può essere risolto rendendo meno sicuro.

Vai a questo collegamento: "https://www.google.com/settings/security/lesssecureapps" e fai "ACCEN.RE" quindi l'applicazione viene eseguita Per sicuro.

+0

Semplice e pulito. +5 per l'ottimo link –

+2

Oh, tq :). Ho lottato per questo per così tanto tempo. Questo non dovrebbe accadere a nessuno. così, l'ho fatto Clean and Clear Phil C – UmaShankar

+0

Funziona per me ma come farlo funzionare senza dover consentire alle applicazioni meno sicure di usare IMAP? – gouessej

Problemi correlati