3

I have impelemented send email via JavaMail on my app. But How to cancel sending email? The problem is that while sending large email the connection was closed and app is crashed. JavaMail cannot be close transport object.I'm sending msg like that:

Transport.send(msg);

Please advice me?

nAkhmedov
  • 3,522
  • 4
  • 37
  • 72
  • I don't get it. If the connection was closed, the send *was* aborted. All you have to do is catch the appropriate exception. – user207421 Jan 25 '16 at 22:31

1 Answers1

0

There's no simple way to abort an in-progress sending of a message. You should be able to use Thread.interrupt to interrupt the sending thread and abort the I/O in progress.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
  • What part are you confused about? An example would probably be very different than your application because I have no idea how your application is using threads. One thread calls Transport.send. The other thread needs to know about the Thread object for the first thread, then it calls the interrupt method on that thread object when you want to abort sending. How the second thread finds out about the first thread depends on your application. The second thread probably only wants to interrupt the first thread while it's trying to send and not at other times so you have to handle that too. – Bill Shannon Jan 27 '16 at 04:14
  • JavaMail uses java.net sockets AFAIK, which are non-interruptible by definition. – user207421 Jan 27 '16 at 19:40
  • You may be right; I don't remember. You may have to close the socket to cause the I/O to abort. There's no way to do that through JavaMail. – Bill Shannon Jan 28 '16 at 00:05
  • @EJP Do you have any idea how to stop sendind email while socket is being closed? – nAkhmedov Jan 29 '16 at 12:16