java mail and thread

A

andros976

Good evening to all .
I'm designing an application that has the task of sending the email automatically taking the recipients from a db.
To send email I created a special class that receives the string with the recipient's address and sends mail.
The instruction to send the message:

Transport.send (msg ) ;

I noticed that this statement takes a bit of seconds before releasing
flow and therefore are unable to use the other functions of the application . I thought , therefore, to use a technology where each object of the class that sends the email implements Runnable and then i use it like a thread.
I noticed , however, that there are problems because sometimes 20 over 50 emails fail and the instruction that give me the error is

Transport.send (msg ) ;

java.net.SocketException : Connection closed by remote host

and seems to be the fact that the thread dies too fast after the statement
Transport.send (msg ) ;
So I inserted a Thread.sleep ( 10000 ) ;
but sometimes I goes equally mistaken, then I tried to employ
t.join ();
but it blocks the flow (basically waiting for the thread to end it all before giving way to another )
What can I do ?
If you need I can write a few lines of code to make you understand how I'm implementing the functionality described


I look forward ideas

thanks
 
K

Knute Johnson

Good evening to all .
I'm designing an application that has the task of sending the email automatically taking the recipients from a db.
To send email I created a special class that receives the string with the recipient's address and sends mail.
The instruction to send the message:

Transport.send (msg ) ;

I noticed that this statement takes a bit of seconds before releasing
flow and therefore are unable to use the other functions of the application . I thought , therefore, to use a technology where each object of the class that sends the email implements Runnable and then i use it like a thread.
I noticed , however, that there are problems because sometimes 20 over 50 emails fail and the instruction that give me the error is

Transport.send (msg ) ;

java.net.SocketException : Connection closed by remote host

and seems to be the fact that the thread dies too fast after the statement
Transport.send (msg ) ;
So I inserted a Thread.sleep ( 10000 ) ;
but sometimes I goes equally mistaken, then I tried to employ
t.join ();
but it blocks the flow (basically waiting for the thread to end it all before giving way to another )
What can I do ?
If you need I can write a few lines of code to make you understand how I'm implementing the functionality described


I look forward ideas

thanks

I think your plan to use a separate thread to spool the messages out
makes perfect sense.

I'm curious what is causing the SocketException. I looked at some of my
code and I don't catch SocketExceptions when sending mail.

From the docs on Transport "Note that send is a static method that
creates and manages its own connection. Any connection associated with
any Transport instance used to invoke this method is ignored and not
used. This method should only be invoked using the form
Transport.send(msg);, and should never be invoked using an instance
variable."

knute...
 
J

Joerg Meier

I noticed that this statement takes a bit of seconds before releasing
flow and therefore are unable to use the other functions of the application . I thought , therefore, to use a technology where each object of the class that sends the email implements Runnable and then i use it like a thread.
I noticed , however, that there are problems because sometimes 20 over 50 emails fail and the instruction that give me the error is
Transport.send (msg ) ;
java.net.SocketException : Connection closed by remote host

Possibly the remote server doesn't allow multiple (or more than 2 or 3)
connections at the same time. I recommend writing a thread class that keeps
an internal list of mails to send, and sends them one by one. That way,
when you send two mails at the same time, the 2nd gets added to the list
and sent after the first finishes, instead of at the same time.

Liebe Gruesse,
Joerg
 
R

Robert Klemme

Possibly the remote server doesn't allow multiple (or more than 2 or 3)
connections at the same time. I recommend writing a thread class that keeps
an internal list of mails to send, and sends them one by one. That way,
when you send two mails at the same time, the 2nd gets added to the list
and sent after the first finishes, instead of at the same time.

Or create a thread pool with as many threads as concurrent connections
are allowed. Then feed send requests through a queue.

Cheers

robert
 
A

andros976

Il giorno venerdì 25 ottobre 2013 13:45:14 UTC+2, Joerg Meier ha scritto:
Possibly the remote server doesn't allow multiple (or more than 2 or 3)

connections at the same time. I recommend writing a thread class that keeps

an internal list of mails to send, and sends them one by one. That way,

when you send two mails at the same time, the 2nd gets added to the list

and sent after the first finishes, instead of at the same time.



Liebe Gruesse,

Joerg



--

Ich lese meine Emails nicht, replies to Email bleiben also leider

ungelesen.

I thanx for the answer, i think, instead, the problem is to send mail one by one, cause the server mail goes in spam protection. I would to try to addmore than one user and put a sleep time 60000 (1minute).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top