Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

AuthenticationFailedException :((

authentication

  • Please log in to reply
1 reply to this topic

#1 namkingkong

namkingkong

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 53 posts
  • Location:Hanoi, Vietnam
  • Programming Language:Java, C++, C#, PHP, JavaScript
  • Learning:Java, C++, C#, PHP, JavaScript

Posted 31 March 2012 - 12:35 PM

I am writing a gmail client in java, but It always throws AuthenticationFailedException. Anyone help me plz!!!

This is my code:

try {
    btnSend.setEnabled(false);
    btnSend.setText("Sending...");

    user = txtUser.getText();
    password = getPassword(txtPassword);
    to = txtTo.getText();
    subject = txtSubject.getText();
    content = txtContent.getText();

    Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
    
    // Get system properties
    Properties properties = System.getProperties();
    
    // SMTP settings
    properties.put("mail.smtp.auth", "true");
    properties.setProperty("mail.smpt.user", user);
    properties.setProperty("mail.smpt.password", password);
    properties.setProperty("mail.smtp.host", host);
    properties.setProperty("mail.smtp.port", "465");
    properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    properties.setProperty("mail.smtp.socketFactory.port", "465");
    properties.setProperty("mail.smtp.socketFactory.fallback", "false");
    
    // Get the default session
    Session session = Session.getInstance(properties, new GmailAuthenticator(user, password));
    
    // Create new message and set it up
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(user));
    message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
    message.setSubject(subject);
    message.setText(content);

    Transport transport = session.getTransport("smtp");
    transport.connect(host, user, password);
    Transport.send(message);
    transport.close();

    JOptionPane.showMessageDialog(null, "Message sent", "Success", JOptionPane.INFORMATION_MESSAGE);
} catch (NoSuchProviderException ex) {
    ex.printStackTrace();
    JOptionPane.showMessageDialog(null, "Unknown Provider", "Error", JOptionPane.ERROR_MESSAGE);
} catch (AddressException ex) {
    ex.printStackTrace();
    JOptionPane.showMessageDialog(null, "Unknown Address", "Error", JOptionPane.ERROR_MESSAGE);
} catch (MessagingException ex) {
    ex.printStackTrace();
    JOptionPane.showMessageDialog(null, "Unknown Username\nor Wrong Password", "Error", JOptionPane.ERROR_MESSAGE);
} finally {
    btnSend.setText("Send");
    btnSend.setEnabled(true);
}

  • 0

#2 thechef

thechef

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 73 posts

Posted 08 April 2012 - 04:32 AM

Have you seen this tutorial? It looks like they use a password authenticator to abstract their username and password.

http://www.mkyong.co...l-smtp-example/
  • 1
I don't document code. If it was hard to write, it should be hard to read ;)





Also tagged with one or more of these keywords: authentication

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download