Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1

后端 未结 5 1150
一个人的身影
一个人的身影 2020-12-03 17:03

while sending mail I am getting this error

java.lang.RuntimeException: javax.mail.SendFailedException: Sending failed; nested exception is: cla

5条回答
  •  借酒劲吻你
    2020-12-03 17:31

    You need to tell it that you are using SSL:

    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    

    In case you miss anything, here is working code:

    String  d_email = "address@gmail.com",
                d_uname = "Name",
                d_password = "urpassword",
                d_host = "smtp.gmail.com",
                d_port  = "465",
                m_to = "toAddress@gmail.com",
                m_subject = "Indoors Readable File: " + params[0].getName(),
                m_text = "This message is from Indoor Positioning App. Required file(s) are attached.";
        Properties props = new Properties();
        props.put("mail.smtp.user", d_email);
        props.put("mail.smtp.host", d_host);
        props.put("mail.smtp.port", d_port);
        props.put("mail.smtp.starttls.enable","true");
        props.put("mail.smtp.debug", "true");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.socketFactory.port", d_port);
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
    
        SMTPAuthenticator auth = new SMTPAuthenticator();
        Session session = Session.getInstance(props, auth);
        session.setDebug(true);
    
        MimeMessage msg = new MimeMessage(session);
        try {
            msg.setSubject(m_subject);
            msg.setFrom(new InternetAddress(d_email));
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));
    
    Transport transport = session.getTransport("smtps");
                transport.connect(d_host, Integer.valueOf(d_port), d_uname, d_password);
                transport.sendMessage(msg, msg.getAllRecipients());
                transport.close();
    
            } catch (AddressException e) {
                e.printStackTrace();
                return false;
            } catch (MessagingException e) {
                e.printStackTrace();
                return false;
            }
    

提交回复
热议问题