NNTP-Posting-Date: Fri, 25 Oct 2013 06:30:18 -0500 Date: Fri, 25 Oct 2013 12:30:16 +0100 From: lipska the kat <"nospam at neversurrender dot co dot uk"> Organization: Trollbusters 3 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130308 Thunderbird/17.0.4 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: java mail and thread References: <6615a997-f18f-42b0-812e-de1cbadf8341@googlegroups.com> In-Reply-To: <6615a997-f18f-42b0-812e-de1cbadf8341@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit Message-ID: Lines: 99 X-Usenet-Provider: http://www.giganews.com X-AuthenticatedUsername: NoAuthUser X-Trace: sv3-hhhpS0VfGm0UufcjztU3LajKiTaventgYPyR6uCeFgt4bd9g5BpeiYUUfZW3yQogS9h7IUc26TF/LX2!3gCYuYTzwvxLxHHJ6a68dxR97qAObpcKf2ZBBA3UdpQKFCG8KZAuL+b1xj/75Y6/MtcFTRX66P6J X-Complaints-To: abuse@btinternet.com X-DMCA-Complaints-To: abuse@btinternet.com X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 4454 Path: csiph.com!usenet.pasdenom.info!news.stben.net!border3.nntp.ams.giganews.com!Xl.tags.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!local2.nntp.ams.giganews.com!nntp.bt.com!news.bt.com.POSTED!not-for-mail Xref: csiph.com comp.lang.java.programmer:26417 On 24/10/13 22:28, andros976@gmail.com wrote: > 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 > How about something like this (outline only, off the top of my head) If you want to send the same message to all recipients then simply. ... Transport transport = session.getTransport(TRANSPORT); //set up session //set up message MimeMessage message = new MimeMessage(session); ... //for each recipient message.addRecipient(Message.RecipientType.TO, toAddress); //or CC, BCC ... Transport transport = session.getTransport("smtp"); transport.connect(HOST, USERNAME, PASSWORD); transport.sendMessage(message, message.getAllRecipients()); transport.close(); wrap it in a thread if you don't want the main to block If you want to send a different message to each recipient then something like class MailMachine implements Runnable{ EmailAddressServer eas; EmailMessageServer ems; ... List emailAddresses = eas.getAddresses(); ... //set up mail session Session session = Session.getInstance(properties); ... public void run(){ for(String email: emailAddresses){ MimeMessage message = new MimeMessage(session); String msg = ems.getMessage(email); //whatever //blocking IO ... Transport.send(message); ... } } } public class Foo{ main(){ // do stuff ... MailMachine mm = new MailMachine(); Thread t = new Thread(mm); t.start(); ... //do more stuff } } -- Lipska the KatŠ: Troll hunter, sandbox destroyer and farscape dreamer of Aeryn Sun GNU/Linux user #560883 - http://www.linuxcounter.net