Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #26412 > unrolled thread

java mail and thread

Started byandros976@gmail.com
First post2013-10-24 14:28 -0700
Last post2013-10-27 01:46 -0700
Articles 6 — 5 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  java mail and thread andros976@gmail.com - 2013-10-24 14:28 -0700
    Re: java mail and thread Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2013-10-24 15:07 -0700
    Re: java mail and thread lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-10-25 12:30 +0100
    Re: java mail and thread Joerg Meier <joergmmeier@arcor.de> - 2013-10-25 13:45 +0200
      Re: java mail and thread Robert Klemme <shortcutter@googlemail.com> - 2013-10-26 13:07 +0200
      Re: java mail and thread andros976@gmail.com - 2013-10-27 01:46 -0700

#26412 — java mail and thread

Fromandros976@gmail.com
Date2013-10-24 14:28 -0700
Subjectjava mail and thread
Message-ID<6615a997-f18f-42b0-812e-de1cbadf8341@googlegroups.com>
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

[toc] | [next] | [standalone]


#26413

FromKnute Johnson <nospam@rabbitbrush.frazmtn.com>
Date2013-10-24 15:07 -0700
Message-ID<l4c5ng$q1j$1@dont-email.me>
In reply to#26412
On 10/24/2013 14: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
>

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...

[toc] | [prev] | [next] | [standalone]


#26417

Fromlipska the kat <"nospam at neversurrender dot co dot uk">
Date2013-10-25 12:30 +0100
Message-ID<yrCdnYcjW9fXy_fPnZ2dnUVZ8o2dnZ2d@bt.com>
In reply to#26412
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<String> 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

[toc] | [prev] | [next] | [standalone]


#26418

FromJoerg Meier <joergmmeier@arcor.de>
Date2013-10-25 13:45 +0200
Message-ID<1tainohfwpuj1.1v1jokfkdmezh.dlg@40tude.net>
In reply to#26412
On Thu, 24 Oct 2013 14:28:49 -0700 (PDT), andros976@gmail.com wrote:

> 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

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

[toc] | [prev] | [next] | [standalone]


#26433

FromRobert Klemme <shortcutter@googlemail.com>
Date2013-10-26 13:07 +0200
Message-ID<bd1m45Fme69U1@mid.individual.net>
In reply to#26418
On 25.10.2013 13:45, Joerg Meier wrote:
> On Thu, 24 Oct 2013 14:28:49 -0700 (PDT), andros976@gmail.com wrote:
>
>> 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.

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

Cheers

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

[toc] | [prev] | [next] | [standalone]


#26443

Fromandros976@gmail.com
Date2013-10-27 01:46 -0700
Message-ID<8cb73799-ac43-4951-b6a6-c6c3c55734f8@googlegroups.com>
In reply to#26418
Il giorno venerdì 25 ottobre 2013 13:45:14 UTC+2, Joerg Meier ha scritto:
> On Thu, 24 Oct 2013 14:28:49 -0700 (PDT), andros976@gmail.com wrote:
> 
> 
> 
> > 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
> 
> 
> 
> -- 
> 
> 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 add more than one user and put a sleep time 60000 (1minute).

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web