Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: JavaMail Error: javax.mail.AuthenticationFailedException

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs down JavaMail Error: javax.mail.AuthenticationFailedException

    Hello!

    I am making a program that sends information to my email. Here is the source:
    package client;
     
    /*
    Some SMTP servers require a username and password authentication before you
    can use their Server for Sending mail. This is most common with couple
    of ISP's who provide SMTP Address to Send Mail.
     
    This Program gives any example on how to do SMTP Authentication
    (User and Password verification)
     
    This is a free source code and is provided as it is without any warranties and
    it can be used in any your code for free.
     
    Author : Sudhir Ancha
    */
     
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.activation.*;
    import java.util.*;
    import java.io.*;
     
    /*
      To use this program, change values for the following three constants,
     
        SMTP_HOST_NAME -- Has your SMTP Host Name
        SMTP_AUTH_USER -- Has your SMTP Authentication UserName
        SMTP_AUTH_PWD  -- Has your SMTP Authentication Password
     
      Next change values for fields
     
      emailMsgTxt  -- Message Text for the Email
      emailSubjectTxt  -- Subject for email
      emailFromAddress -- Email Address whose name will appears as "from" address
     
      Next change value for "emailList".
      This String array has List of all Email Addresses to Email Email needs to be sent to.
     
     
      Next to run the program, execute it as follows,
     
      SendMailUsingAuthentication authProg = new SendMailUsingAuthentication();
     
      java -Dmail.smtp.starttls.enable=true -Dmail.smtp.port=587
    	smtpsend -d -M smtp.live.com
    	-U user@hotmail.com -P passwd -A someotheruser@hotmail.com
     
    */
     
    public class Emailer
    {
     
      private static final String SMTP_HOST_NAME = "smtp.live.com";
      private static final String SMTP_AUTH_USER = "mokee@live.com";
      private static final String SMTP_AUTH_PWD  = "spidermonkey";
     
      private static final String emailMsgTxt      = "Online Order Confirmation Message. Also include the Tracking Number.";
      private static final String emailSubjectTxt  = "HELEEEELEQWEWEQ!!!";
      private static final String emailFromAddress = "mokee@live.com";
     
      // Add List of Email address to who email needs to be sent to
      private static final String[] emailList = {"mokee@live.com"};
     
      public static void main(String emailBody) throws Exception
      {
        Emailer smtpMailSender = new Emailer();
        smtpMailSender.postMail( emailList, emailSubjectTxt, emailBody, emailFromAddress);
        System.out.println("Sucessfully Sent mail to All Users");
      }
     
      public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException {
     
         boolean debug = false;
         //Set the host smtp address
         Properties props = new Properties();
         props.put("mail.smtp.host", SMTP_HOST_NAME);
         props.put("mail.smtp.auth", "true");
     
        Authenticator auth = new SMTPAuthenticator();
        Session session = Session.getDefaultInstance(props, auth);
     
        session.setDebug(debug);
     
        // create a message
        Message msg = new MimeMessage(session);
     
        // set the from and to address
        InternetAddress addressFrom = new InternetAddress(from);
        msg.setFrom(addressFrom);
     
        InternetAddress[] addressTo = new InternetAddress[recipients.length];
        for (int i = 0; i < recipients.length; i++)
        {
            addressTo[i] = new InternetAddress(recipients[i]);
        }
        msg.setRecipients(Message.RecipientType.TO, addressTo);
     
     
        // Setting the Subject and Content Type
        msg.setSubject(subject);
        msg.setContent(message, "text/plain");
        Transport.send(msg);
     }
     
     
    /**
    * SimpleAuthenticator is used to do simple authentication
    * when the SMTP server requires it.
    */
    private class SMTPAuthenticator extends javax.mail.Authenticator
    {
    	private Session createSmtpSession() {
    		  final Properties props = new Properties();
    		  props.setProperty("mail.smtp.host", "smtp.live.com");
    		  props.setProperty("mail.smtp.auth", "true");
    		  props.setProperty("mail.smtp.port", "" + 587);
    		  props.setProperty("mail.smtp.starttls.enable", "true");
    		  // props.setProperty("mail.debug", "true");
     
    		  return Session.getDefaultInstance(props, new javax.mail.Authenticator() {
     
    		    protected PasswordAuthentication getPasswordAuthentication() {
    		      return new PasswordAuthentication("mokee@live.com", "spidermonkey");
    		    }
    		  });
    		}
    }
     
    }

    However, whenever I run this I get the following error message:
    Exception in thread "main" 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 client.Emailer.postMail(Emailer.java:102)
    	at client.Emailer.main(Emailer.java:67)
    	at client.RemoteFileClient.main(RemoteFileClient.java:28)

    I am relatively new to JavaMail and can not identify the problem. Can someone please lend me a hand?


  2. #2
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: JavaMail Error: javax.mail.AuthenticationFailedException

    did you read the exception? it says no password specified, so I would look up how to tell your code what the password is.
    That being said I have never used JavaMail. So I don't know how why your String SMTP_AUTH_PWD = "xxxxxxxxxxxxxx";"
    never gets used properly.
    Last edited by JonLane; February 28th, 2012 at 12:56 AM.

  3. #3
    Member
    Join Date
    Feb 2012
    Posts
    58
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: JavaMail Error: javax.mail.AuthenticationFailedException

    The SMTPAuthenticator is complex unnecessarily. It should override only the method getPasswordAuthentication().

Similar Threads

  1. How to Send emails from Google Mail using JavaMail API
    By JavaPF in forum Java SE API Tutorials
    Replies: 3
    Last Post: February 26th, 2012, 04:21 PM
  2. How to Send emails from Google Mail using JavaMail API
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: November 14th, 2010, 11:00 AM
  3. Javamail html mail sended as text and headers problem
    By johnymj in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 29th, 2010, 10:22 AM
  4. Javamail
    By johniem in forum Java Theory & Questions
    Replies: 1
    Last Post: February 3rd, 2010, 07:42 AM
  5. Javax.Mail Query Help Me Please
    By ravjot28 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: January 19th, 2010, 11:06 AM