Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Authentication error when using javamail

Status
Not open for further replies.

faeton13

Programmer
Mar 7, 2005
9
CO
Hi. I'm new to this javamail thing, but I managed to send an email to my gmail account. However, when I tried to do this for my yahoo account, I got the following Exception:
"javax.mail.AuthenticationFailedException"
I checked both my username and password and they are allright.
Do you have any Idea of what the problem can be?????? I'm really desperate with this issue. Any help will be appreciated.

Thanks in advance.
PS: here is the code
Code:
public static void main(String[] args)
    {
        Mailer obj = new Mailer();
        String server = "smtp.mail.yahoo.com";
        String userName = "pepito";
        String password = "xxxxx";
        String fromAddres = "pepito@yahoo.com";
        String toAddres = "pepito@yahoo.com";
        String cc = "";
        String bcc = "";
        boolean htmlFormat = false;
        String subject = "tema";
        String body = "prueba";
        
        obj.sendMail(server, userName, password, fromAddres, toAddres, cc, bcc,
                     htmlFormat, subject, body);
        
    }

    public void sendMail(String server, String userName, String password, String fromAddress, String toAddress, String cc, String bcc, boolean htmlFormat, String subject, String body)
    {
    
        Properties properties = System.getProperties();
        properties.put("mail.smtps.host", server);
        properties.put("mail.smtps.auth", "true");
        Session ses  = Session.getInstance(properties);

        ses.setDebug(true);

        try{
        
            MimeMessage msg = new MimeMessage(ses);
    
            msg.setFrom(new InternetAddress(fromAddress));
    
            if (toAddress != null)
            {
               msg.addRecipients(Message.RecipientType.TO, toAddress);
            }
    
            if (cc != null)
            {
                msg.setRecipients(Message.RecipientType.CC
                        ,InternetAddress.parse(cc, false));
            }
    
            if (bcc != null)
            {
                msg.setRecipients(Message.RecipientType.BCC
                        ,InternetAddress.parse(bcc, false));
            }
    
            if (htmlFormat)
            {
                msg.setContent(body, "text/html");
            }
            else
            {
                msg.setContent(body, "text/plain");
            }
    
            msg.setSubject(subject);
            msg.saveChanges();
    
            Transport tr = ses.getTransport("smtps");
            tr.connect(server,userName, password);
            tr.sendMessage(msg, msg.getAllRecipients());
            tr.close();
        }
        
        catch(MessagingException e)
        {
            e.printStackTrace();
        }
        
        

    }
}

class MyPasswordAuthenticator extends Authenticator
{
   String user;
   String pw;

   public MyPasswordAuthenticator (String username, String password)
   {
      super();
      this.user = username;
      this.pw = password;
   }
   public PasswordAuthentication getPasswordAuthentication()
   {
      return new PasswordAuthentication(user, pw);
   }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top