Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Had this exact same problem. You have to close the transport in the catch block. The reason the code freezes is because the smtp server connection never gets shut down on the client side unless you do it manually. </p> <pre><code> try { simpleMessage.setFrom(fromAddress); simpleMessage.setRecipient(RecipientType.TO, toAddress); simpleMessage.setSubject(subject); simpleMessage.setText(text); Transport.send(simpleMessage); // this is where code hangs } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); Transport.close() } </code></pre> <p>But the most efficient way to make sure javamail exits correctly is to bundle the javamail logic into one try block and close the transport in a finally block. Try this. <strong>[EDIT]</strong> After being alerted that the above code didn't work here is the code that compiles correctly.</p> <pre><code>import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.Message.RecipientType; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; //first from, to, subject, &amp; text values are set public class SendMail { private String from; private String to; private String subject; private String text; public SendMail(String from, String to, String subject, String text) { this.from = from; this.to = to; this.subject = subject; this.text = text; } // send method is called in the end public void send() throws MessagingException { Properties props = new Properties(); props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.host", "localhost"); props.put("mail.smtp.auth", "false");// set to false for no username props.put("mail.debug", "false"); props.put("mail.smtp.port", "25"); Session session = Session.getDefaultInstance(props); InternetAddress fromAddress = null; InternetAddress toAddress = null; Transport transport = session.getTransport("smtp"); transport.connect(); try { Message simpleMessage = new MimeMessage(session); fromAddress = new InternetAddress(from); toAddress = new InternetAddress(to); simpleMessage.setFrom(fromAddress); simpleMessage.setRecipient(RecipientType.TO, toAddress); simpleMessage.setSubject(subject); simpleMessage.setText(text); transport.sendMessage(simpleMessage, simpleMessage.getAllRecipients()); } catch (MessagingException e) { e.printStackTrace(); } finally { transport.close(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload