5

I got following error when trying to send email via Java Mail API? What does this error mean?

javax.mail.MessagingException: Exception reading response;
  nested exception is:
        java.net.SocketTimeoutException: Read timed out
javax.mail.MessagingException: Exception reading response;
  nested exception is:
        java.net.SocketTimeoutException: Read timed out
        at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2210)
        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1950)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)
        at javax.mail.Service.connect(Service.java:317)
        at javax.mail.Service.connect(Service.java:176)
        at javax.mail.Service.connect(Service.java:125)
        at javax.mail.Transport.send0(Transport.java:194)
        at javax.mail.Transport.send(Transport.java:124)

Here is my code, i set all parameters (from, to , subjects and attachments)

public static void send(MailUtil mailUtil) throws MessagingException {
            MimeMessage message = new MimeMessage(session);
            if (props.getProperty(IConstants.DL_MAIL_CONFIGURATION.MAIL_SENDER) != null) {              
                message.setFrom(new InternetAddress(props.getProperty(IConstants.DL_MAIL_CONFIGURATION.MAIL_SENDER)));
            } else {
                message.setFrom(new InternetAddress(mailUtil.getFrom()));
            }
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(mailUtil.getTo()));
            if (mailUtil.getBcc() != null && mailUtil.getBcc().trim().length() > 0) {
                message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(mailUtil.getBcc()));
            } else {
                message.setRecipients(Message.RecipientType.BCC,InternetAddress.parse(""));
            }

            message.setSubject(mailUtil.getSubject(), "UTF-8");

            // Check for files list and attach them.
            if (mailUtil.attachmentFiles != null && mailUtil.attachmentFiles.size() > 0) {
                Multipart multipart = new MimeMultipart();

                // Set content.
                BodyPart messageBodyPart =new MimeBodyPart();
                messageBodyPart.setContent(mailUtil.getContent(), "text/plain; charset=utf-8");
                multipart.addBodyPart(messageBodyPart);

                // Attach files.
                for (File file : mailUtil.attachmentFiles) {
                    messageBodyPart = new MimeBodyPart();
                    DataSource source = new FileDataSource(file);
                    messageBodyPart.setDataHandler(new DataHandler(source));
                    messageBodyPart.setFileName(file.getName());
                    multipart.addBodyPart(messageBodyPart);
                }

                message.setContent(multipart);
            } else {
                //message.setContent("<h1>Hello world</h1>", "text/html");
                message.setContent(mailUtil.getContent(), "text/html; charset=UTF-8");
            }           
            Transport.send(message);
    }

I just think is there any problem with my paramters?

Belows is my configuration

mail.smtp.port=465
mail.smtp.starttls.enable=true
mail.smtp.auth=true
mail.smtp.socketFactory.port=465
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.timeout=25000

mail.smtp.host=smtp.gmail.com
mail.username = [email protected]
mail.password = mypassword
mail.sender = [email protected]
mail.receiver = [email protected]
mail.subject = mysubject

I am using google mail server! I dont' think there is problem there!

Belows is session initiation

final String userName = props.getProperty(IConstants.DL_MAIL_CONFIGURATION.MAIL_USERNAME);
final String passWord = props.getProperty(IConstants.DL_MAIL_CONFIGURATION.MAIL_PASSWORD);

session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(userName,
                                passWord);
                    }
            });
18
  • 2
    Please share code as well without it hard to help you Commented Jul 21, 2015 at 9:58
  • 1
    You need the information about MessageingException check with url stackoverflow.com/questions/5659325/… Commented Jul 21, 2015 at 10:01
  • There's a time out there, so probably your email server is either unreachable or the properties setup is not correct. Commented Jul 21, 2015 at 10:12
  • I am using google mail server. I dont think having problem there. I tried to telnet smtp.gmail.com 465. It's reachable Commented Jul 21, 2015 at 10:16
  • Could you post your Session instantiation? Commented Jul 21, 2015 at 14:23

3 Answers 3

13

I believe it absolutely relates to server configuration. When I change the port configuration from 465 to 587, it solves my problem! Anyway, thank you guy for your help!

Sign up to request clarification or add additional context in comments.

1 Comment

Correct, Port 465 expects you to start an SSL session and then run smtp over it, compared to port 587 where the session begins in plaintext SMTP, and then starttls is used to negotiate an encrypted TLS session. So, the first two lines of your config are in conflict.
0

Your code and configuration contain many of these common mistakes. In particular, the use of Session.getDefaultInstance may mean you're not using the configuration you think you're using. If fixing that doesn't solve your problem, post the JavaMail session debug output.

Comments

0

I'm not sure why you use a "mail" object to setup the connection properties, instead try this, it should work, I've tested myself:

private Properties props;
props = System.getProperties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587"); 

session = Session.getInstance(props,
    new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
      });
try {
        message.setFrom(new InternetAddress("YOUR EMAIL ADDRESS HERE"));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress("RECEIVER EMAIL ADDRESS HERE"));
        message.setSubject("SUBJECT");

        message.setText("THE EMAIL TEXT");
        Transport.send(message);
    } catch (MessagingException e) {e.printStackTrace();}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.