1

I am trying to send a mail to a gmail as smpt from my host service but stacktrace says

com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; 

Though the code is working fine when executed on my Eclipse IDE. I asked hosting services if there any firewall that could be stopping the same. But they denied it by saying that your ip is not blocked in firewall and given port is also open on server and able to connect to the remote servers. Then what could be the reason for the problem. Here is the code

final String username = "xyz@gmail.com";
        final String password = "abcd";
        Properties properties = System.getProperties();
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.port", "587");
        Session session = Session.getInstance(properties,
                new javax.mail.Authenticator() {
                    protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
                        return new javax.mail.PasswordAuthentication(username,
                                password);
                    }
                });
        out.print(session);
        response.setContentType("text/html");
        try {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress("xyz@gmail.com"));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(
                    "abc@gmail.com"));
            message.setSubject("hello");
            message.setText("this is me");
            Transport.send(message);

        } catch (MessagingException mex) {
            out.print(mex);
        }

    }
user207421
  • 305,947
  • 44
  • 307
  • 483
Mayank Vaid
  • 330
  • 1
  • 7
  • 18

0 Answers0