title: java send email through yahoo
body: <p>This is my code </p>
  
  <pre><code> import java.util.Properties;
   import javax.mail.Message;
   import javax.mail.MessagingException;
   import javax.mail.PasswordAuthentication;
   import javax.mail.Session;
   import javax.mail.Transport;
   import javax.mail.internet.InternetAddress;
   import javax.mail.internet.MimeMessage;
  
   public class SendMail 
   {
     String host, port, emailid,username, password;
     Properties props = System.getProperties();
     Session l_session = null;
     public SendMail() 
     {
       host = "smtp.mail.yahoo.com";
       port = "587";
       emailid = "xxx@yahoo.com";
       username = "xxx@yahoo.com";
       password = "xxxxx";
       emailSettings();
       createSession();
       sendMessage("xxx@yahoo.com", "yyyy@yahoo.com","Test","test Mail");
     }
     public void emailSettings() 
     {
       props.put("mail.smtp.host", host);
       props.put("mail.smtp.auth", "true");
       props.put("mail.debug", "false");
       props.put("mail.smtp.port", port);
     }
     public void createSession() 
     {
       l_session = Session.getInstance(props,new javax.mail.Authenticator() 
                        {
                          protected PasswordAuthentication getPasswordAuthentication()
                          {
                            return new PasswordAuthentication(username, password);
                          }
                        });
      l_session.setDebug(true); // Enable the debug mode
     }
  
     public boolean sendMessage(String emailFromUser, String toEmail, String subject, String msg) 
     {       
      try 
      {
          MimeMessage message = new MimeMessage(l_session);
          emailid = emailFromUser;
          message.setFrom(new InternetAddress(this.emailid));
          message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
          message.setSubject(subject);
          message.setContent(msg, "text/html");
          //message.setText(msg);
          Transport.send(message);
          System.out.println("Message Sent");
      } 
      catch (MessagingException mex) 
      {
          mex.printStackTrace();
      } 
      catch (Exception e) 
      {
          e.printStackTrace();
      }//end catch block
      return true;
    }
    public static void main(String args[])
    {
       new SendMail();
    }
   }
  </code></pre>
  
  <p>when i run this code i got <strong>com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.1 Authentication required</strong> this error.i'm using PasswordAuthentication. i don't know why this exception is coming.please anyone can you tell me how to overcome this error? </p>