Error sending email with Javamail

Asked

Viewed 742 times

2

It was working normally. Now when I run the program the following error appears:

    Could not connect to SMTP host: smtp.gmail.com, port: 465;

This is my way

public void enviaEmail() {
    Properties props = new Properties();

    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class",
            "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");

    Session session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(
                            "[email protected]", "1111111");
                }
            });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("[email protected]"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("[email protected]"));
        message.setSubject("Novo Cadastro efetuado na Ouvidoria!!!");
        message.setText("Nome do Beneficiário: ," + "" + nome);
        System.out.println("Nome email:"+nome);

        Transport.send(message);

        System.out.println("Done");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }

}
  • Do you continue with gmail smtp server connectivity? On your cmd, try telnet smtp.gmail.com 465, If it fails to open the connection (not showing a whole black screen), it is an indication that you have a network restriction. Also, someone changed the password or smtp settings of the email account?

  • Does not open anything, says that telnet is not recognized as an internal command

  • Enable telnet on Windows: http://social.technet.microsoft.com/wiki/contents/articles/910.windows-7-enabling-telnet-client.aspx

2 answers

1

Change :

Transport transport = session.getTransport("smtps");

To:

Transport transport = session.getTransport("smtp");

1

Taken from this OS question in English: https://stackoverflow.com/questions/15378133/could-not-connect-to-smtp-host-smtp-gmail-com-port-465-response-1

You need to tell him that you are using SSL:

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

Below a working source code:

String  d_email = "[email protected]",
        d_uname = "Name",
        d_password = "urpassword",
        d_host = "smtp.gmail.com",
        d_port  = "465",
        m_to = "[email protected]",
        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;
    }
  • 1

    But I’m telling you I’m wearing: props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

  • 1

    @Diegoaugusto, did you try to use the code I posted? And does your Antivirus not have the blocking option? I’ve had several problems like this and was the antivirus (usually Avast)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.