-2

new guy here. I'm trying to send an email through java, the following code that I am using is from this site;

public class NewClass extends Object{

public static void main(String [] args)
{

try{

    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.mail.yahoo.com"); // for gmail use smtp.gmail.com
    props.put("mail.smtp.auth", "true");
    props.put("mail.debug", "true"); 
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.socketFactory.port", "587");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");

    Session mailSession = Session.getInstance(props, new javax.mail.Authenticator() {

        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("abcd@yahoo.com", "abcd");
        }
    });

    mailSession.setDebug(true); // Enable the debug mode

    Message msg = new MimeMessage( mailSession );

    //--[ Set the FROM, TO, DATE and SUBJECT fields
    msg.setFrom( new InternetAddress( "abcd@yahoo.com" ) );
    msg.setRecipients( Message.RecipientType.TO,InternetAddress.parse("efgh@gmail.com") );
    msg.setSentDate( new Date());
    msg.setSubject( "Hello World!" );

    //--[ Create the body of the mail
    msg.setText( "Hello from my first e-mail sent with JavaMail" );

    //--[ Ask the Transport class to send our mail message
    Transport.send( msg );

}catch(Exception E){
    System.out.println("Unable to send Mail");
    System.out.println( E );
}

} }

This is the Error I am Getting:

javax.mail.MessagingException: Could not connect to SMTP host: smtp.mail.yahoo.com, port: 587;  nested exception is: java.net.SocketException: Connection reset

What am I doing wrong here ? any help would be appreciated, TIA ! PS. sorry for any format errors.

NewbProg
  • 1
  • 1
  • Look here for a working example: http://stackoverflow.com/questions/11356237/sending-mail-from-yahoo-id-to-other-email-ids-using-javamail-api – StephaneM Jul 18 '16 at 12:13
  • Thank you very much. I visited the link and found the code posted by Rahul Agrawal there to be more functioning than the other codes I've come across albeit I'm getting an authentication error with his, more specifically: "com.sun.mail.smtp.smtpsendfailedexception: 530 5.7.1 authentication required". I have allowed "Less secure login" in the yahoo account settings but the problem persists. Any suggestions ? – NewbProg Jul 18 '16 at 21:05

1 Answers1

0

The SSL SMTP port for smtp.mail.yahoo.com is 465, not 587.

Change your code from:

props.put("mail.smtp.port", "587");
props.put("mail.smtp.socketFactory.port", "587");

to

props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
Jokab
  • 2,939
  • 1
  • 15
  • 26
  • The Yahoo Help (https://help.yahoo.com/kb/SLN4724.html) says "Port - 465 or 587", so this shouldn't be the problem – Sergej Jul 18 '16 at 14:33
  • @Sergej It seems you are right. I googled a bit and found this site https://www.arclab.com/en/kb/email/list-of-smtp-and-pop3-servers-mailserver-list.html which claims that 587 is for TSL and 465 is for SSL. Since the question uses SSLSocketFactory, 465 should likely be used. – Jokab Jul 18 '16 at 14:39
  • @Jokab I apologize for my neglect, I assumed it doesn't matter (I don't entirely understand TSL/SSL) the port was originally 465, the error I got with 465: javax.mail.MessagingException: Could not connect to SMTP host: smtp.mail.yahoo.com, port: 465; nested exception is: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target – NewbProg Jul 18 '16 at 19:55
  • @NewbProg That sounds like an error on your end - you might be missing the certificate. Try this solution http://stackoverflow.com/a/21507568/2152511 – Jokab Jul 18 '16 at 21:01
  • @Jokab I followed the link and I think I need the InstallCert, but first I need wget and stuff, I'll update if something happens. – NewbProg Jul 20 '16 at 19:44
  • @NewbProg Did this work out for you? If so, consider accepting the answer. – Jokab Jul 28 '16 at 12:29