Click here to Skip to main content
15,879,490 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am getting this error.This error occurs when I send the confirmation code to the email of the registered user, but either succeeds or fails.

    javax.mail.MessagingException: Exception reading response;
      nested exception is:
        java.net.SocketTimeoutException: Read timed out
        at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2445)
        at com.sun.mail.smtp.SMTPTransport.ehlo(SMTPTransport.java:1684)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:730)
        at javax.mail.Service.connect(Service.java:388)
        at javax.mail.Service.connect(Service.java:246)
        at javax.mail.Service.connect(Service.java:195)
        at javax.mail.Transport.send0(Transport.java:254)
        at javax.mail.Transport.send(Transport.java:124)
        at az.unibank.employee.mail.SendEmail.send(SendEmail.java:49)
        at az.unibank.employee.mail.SendEmail.lambda$sendAsync$0(SendEmail.java:13)
        at java.lang.Thread.run(Thread.java:748)
    Caused by: java.net.SocketTimeoutException: Read timed out
        at java.net.SocketInputStream.socketRead0(Native Method)
        at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
        at java.net.SocketInputStream.read(SocketInputStream.java:171)
        at java.net.SocketInputStream.read(SocketInputStream.java:141)
        at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:126)
        at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
        at java.io.BufferedInputStream.read(BufferedInputStream.java:265)
        at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:104)
        at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2425)
        ... 10 more

public class SendEmail {

    public static void sendAsync(String toEmail, String text, String subject) {

        new Thread(() -> {
            send(toEmail, text, subject);
        }).start();

    }

    public static void send(String toEmail, String text, String subject) {

        final String username = "anar.memmedov1501@gmail.com";
        final String password = "sAlam12345";

        Properties prop = new Properties();
        prop.put("mail.smtp.host", "smtp.gmail.com");
        prop.put("mail.smtp.port", "587");
        prop.put("mail.smtp.auth", "true");
        prop.put("mail.smtp.starttls.enable", "true");
        Session session = Session.getInstance(prop,
                new javax.mail.Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(username));
            message.setRecipients(
                    Message.RecipientType.TO,
                    InternetAddress.parse(toEmail)
            );
            message.setSubject(subject);
            message.setText(text);

            Transport.send(message);

        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

}

and this my send email code this problem occurs occasionally, but it wasn't like this before. I can't quite understand why. is there anyone,who help me?


What I have tried:

I seriously can't handle this problem, I don't understand why.
Posted
Updated 15-Aug-21 19:47pm
Comments
SeeSharp2 12-Jul-21 10:12am    
Sounds like your not connecting to the SMTP server properly.
[no name] 15-Jul-21 13:03pm    
so? how to fix it?
SeeSharp2 15-Jul-21 13:07pm    
No idea. You'll have to troubleshoot your connections, make sure your gmail account allows sending emails like this.
Member 15294621 19-Jul-21 13:53pm    
I think there might be a problem with the SMTP server that you are trying to connect as calls succeed or fail intermittently. You need to check with your service provider.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900