0

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

Here is the sample input

private String to = "junaidbinsarfraz@yahoo.com";
private String from = "juniad_rocku@yahoo.com";
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.

Junaid
  • 2,572
  • 6
  • 41
  • 77

2 Answers2

0

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);
Name Name
  • 175
  • 1
  • 2
  • 11
  • Thanks. But its giving me the error -> `Could not connect to SMTP host: smtp.mail.yahoo.com, port: 465, response: -1` – Junaid Dec 07 '14 at 12:26
  • 1
    There is no mail.password property. You can set it, but it does nothing. See the JavaMail FAQ for an [example for connecting to Yahoo! Mail](http://www.oracle.com/technetwork/java/javamail/faq/index.html#yahoomail), and for [debugging tips](http://www.oracle.com/technetwork/java/javamail/faq/index.html#condebug) if you still get that error. – Bill Shannon Dec 07 '14 at 20:39
  • Try with port 587. I can past my working configuration for yahoo smtp (xml configuration, but it is same thing) – Djordje Ivanovic Dec 10 '14 at 10:37
0

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********e@yahoo.com"/>
        <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>
Djordje Ivanovic
  • 4,151
  • 4
  • 27
  • 49