0

I have an application, user requests report and gets it in her inbox as zipped csv file attachment. It works fine for sending emails with zip file with max ~ 44kb. But program get stuck forever on Transport.send(message); line, while sending reports with bigger size.

I search similar issues and applied the timeout solution, but it does not help me.

    try {
        Properties props = mailProperties.asProperties();
        props.put("mail.smtp.connectiontimeout", TIMEOUT);
        props.put("mail.smtp.timeout", TIMEOUT);
        Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(mailProperties.getUsername(), mailProperties.getPassword());
            }
        });
        session.setDebug(true);
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(mail.getFrom()));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mail.getTo()));
        message.setSubject(mail.getSubject());
        MimeBodyPart mbp1 = new MimeBodyPart();
        mbp1.setText(mail.getText());
        MimeBodyPart mbp2 = new MimeBodyPart();
        mbp2.attachFile(filePath);
        Multipart mp = new MimeMultipart();
        mp.addBodyPart(mbp1);
        mp.addBodyPart(mbp2);
        message.setContent(mp);
        Transport.send(message);  // get stuck here
    } catch (Exception e) {
        LOGGER.error("Error : ", e);
        throw new RuntimeException("Sending email failed.", e);
    }
Asisch
  • 41
  • 2

1 Answers1

0

Is it possible to put this method in a threaded task? If the threaded task takes longer than expected, timeout the thread?

How to timeout a thread

Kat
  • 447
  • 2
  • 7
  • 21
  • Thanks for answer. I'm looking for a solution, to be able to send all report mails requested by user, and wonder about why some can not be sen't and if the problem is related with size or not. – Asisch May 11 '18 at 15:49
  • It's not a really good answer. I should have made my answer a comment. So I see that you have set the timeouts - I'm surprised you are not returning at least a timeout exception. However, I don't see the construction of Transport. Is the Transport part of the Session? Could you use: Transport transport = session.getTransport(); transport.send(...) – Kat May 11 '18 at 16:35