JAVA Mail Authentication Fail

7.1k views Asked by At

I am using JAVAMail API to send email. But Authentication fails.

Here is the sample input

private String to = "[email protected]";
private String from = "[email protected]";
private String username = "juniad_rocku";
private String password = "myactualpassword";
private String subject = "My Subject";
private String body = "Please see the attached file";

and code is

private void emailFile(){

    String host = "relay.jangosmtp.net";

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.port", "25");

    // Get the Session object.
    Session session = Session.getInstance(props,
       new javax.mail.Authenticator() {
          protected PasswordAuthentication getPasswordAuthentication() {
             return new PasswordAuthentication(username, password);
          }
       });

    try {
       Message message = new MimeMessage(session);

       message.setFrom(new InternetAddress(from));

       message.setRecipients(Message.RecipientType.TO,
          InternetAddress.parse(to));

       message.setSubject(this.subject);

       BodyPart messageBodyPart = new MimeBodyPart();

       messageBodyPart.setText(this.body);

       Multipart multipart = new MimeMultipart();

       multipart.addBodyPart(messageBodyPart);

       for(String filePath : UniversityForm.allAttachedFilesPath) {
           if(filePath != null && !(filePath.equals("")))
                this.addAttachment(multipart, filePath);
       }

       message.setContent(multipart);

       Transport.send(message);

       System.out.println("Sent message successfully....");

    } catch (MessagingException e) {
       throw new RuntimeException(e);
    }
}

ERROR

java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 535 5.7.8 Authentication credentials invalid

I have given good username, password ... I don't know why Authentication Failed. I don't know about host. Where am I mistaken? Please help.

2

There are 2 answers

3
Name Name On

Yahoo Mail Server is "smtp.mail.yahoo.com" with the port number 465. You forget to add the "mail.password" property. You need add the following additional properties:

Properties props = new Properties();
props.put("mail.smtp.host", "smtp.mail.yahoo.com");
props.put("mail.smtp.port", "465");
props.put("mail.transport.protocol", "smtps");
props.put("mail.password", password);
0
Djordje Ivanovic On

Here is my working configuration for yahoo smtp, hope it will help:

<property name="host" value="smtp.mail.yahoo.com"/>
        <property name="port" value="587 "/>
        <property name="username" value="z********[email protected]"/>
        <property name="password" value="**********"/>
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.starttls.enable">true</prop>
                <prop key="mail.debug">true</prop>
            </props>
        </property>