Java mail without ssl - PKIX path building failed:

后端 未结 3 1353
感动是毒
感动是毒 2020-12-19 08:59

I am using java mail to send emails over smtp. The smtp settings given below:

        Properties props = new Properties();
        Object put = props.put(\"m         


        
相关标签:
3条回答
  • 2020-12-19 09:22

    This JavaMail FAQ entry should help.

    Try using MailSSLSocketFactory like this:

      MailSSLSocketFactory sf = new MailSSLSocketFactory();
      sf.setTrustAllHosts(true);
      props.put("mail.smtp.ssl.socketFactory", sf);
    
    0 讨论(0)
  • 2020-12-19 09:24

    I had this problem with java 8. After updating this property problem solved

    props.put("mail.smtp.ssl.trust", "smtp.gmail.com")
    
    if used spring boot in application.property
    
    spring.mail.properties.mail.smtp.ssl.trust = smtp.gmail.com
    

    I think this will help.

    0 讨论(0)
  • 2020-12-19 09:37

    Worked for me :)

    Properties props = new Properties();
            props.put("mail.transport.protocol", "smtp");
            props.put("mail.smtp.host", "smtp.companydomain.biz"); // 
            props.put("mail.smtp.auth", "true");
            props.put("mail.debug", "true"); 
            props.put("mail.smtp.starttls.enable", "true");`enter code here`
            props.put("mail.smtp.port", "25");
            props.put("mail.smtp.socketFactory.port", "25");
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.socketFactory.fallback", "true");
    
            MailSSLSocketFactory sf = null;
            try {
                sf = new MailSSLSocketFactory();
            } catch (GeneralSecurityException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            sf.setTrustAllHosts(true);
            props.put("mail.smtp.ssl.socketFactory", sf);
    
            Session mailSession = Session.getInstance(props, new javax.mail.Authenticator() {
    
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("mail@companydomain.biz", "password");
                }
            });
    
            mailSession.setDebug(true); // Enable the debug mode
    
            Message msg = new MimeMessage( mailSession );
    
            //--[ Set the FROM, TO, DATE and SUBJECT fields
            try {
                msg.setFrom( new InternetAddress( "mail@companydomain.biz" ) );
            } catch (AddressException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (MessagingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                msg.setRecipients( Message.RecipientType.TO,InternetAddress.parse("myemailid@companydomain.biz") );
            } catch (AddressException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (MessagingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //msg.setSentDate(new Date());
            try {
                msg.setSubject( "Hello World!" );
            } catch (MessagingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            //--[ Create the body of the mail
            try {
                msg.setText( "Hello from my first e-mail sent with JavaMail" );
            } catch (MessagingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            //--[ Ask the Transport class to send our mail message
            try {
                Transport.send( msg );
            } catch (MessagingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }   
        }
    
    0 讨论(0)
提交回复
热议问题