Solved

Optimizing Java mail client

Posted on 2008-06-12
21
331 Views
Last Modified: 2013-11-13
I have an (Java) application that daily sends thousands of emails.
Usually it's one email per few 1K users.
In order to make it more 'personal', each of the users gets the email via "To" field, with only him being the single "To" recipient.
But this sending email in a for-loop takes ages, usually 5-6h.

So, I'm looking if there is some better/faster way to do this.
Some better Java mail client, than the usual JDK stuff.
I'm thinking (out-loud) of the following things:
 - instructing smtp server to lay off on the strict checks since he'll receive few 1K emails from trusted source
 - instructing server to do 'batch' processing, at the beginning only receiving emails, and after 'commit' sending them
 - perhaps server could have option to receive email with all recipients, but sending the email then one-by-one
 - ...

The solution that I see here is to have some client that knows to adjust to server type.
To execute some custom operations that speed things up.

Any ideas / such clients out there?
0
Comment
Question by:Dejan Pažin
  • 10
  • 9
21 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 21768904
You are using the same Session throughout the loop? How many mails take 5-6hrs?
0
 
LVL 17

Author Comment

by:Dejan Pažin
ID: 21769728
I'm using this class:
 - http://static.springframework.org/spring/docs/2.0.x/api/org/springframework/mail/javamail/JavaMailSenderImpl.html
Where I only set host.
The code in this class uses single session, created at instantiation:
      private Session session = Session.getInstance(new Properties(), null);

Around 4-5K emails take 5-6h.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21769784
Creating a new Session in a loop (if that's what's happening) will give you unnecessary overhead
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Author Comment

by:Dejan Pažin
ID: 21771562
No, this class is a singleton in my app.
Hence single Session instance.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21771908
I think i'd need to see how you're using that class. From the doc, the safest way of using it in accord with what i've said is to supply it with your own Session and use only the one
0
 
LVL 17

Author Comment

by:Dejan Pažin
ID: 21773765
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21776587
OK.

a. Create a Session, s
b. Create JavaMailSenderImpl, jmsi
c. Call jmsi.setSession(s)
d. Now send in a loop
0
 
LVL 17

Author Comment

by:Dejan Pažin
ID: 21776856
Why?
That's exactly what getSession does - creates a single instance for the whole JMSI usage.

153       public synchronized Session getSession() {
154             if (this.session == null) {
155                   this.session = Session.getInstance(this.javaMailProperties);
156             }
157             return this.session;
158       }

And I only have one JMSI instance.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21776884
Oh OK - i haven't studied their code. If it's only using one Session for the lifetime of your loop then you're OK on that front
0
 
LVL 17

Author Comment

by:Dejan Pažin
ID: 21776949
Sure, but that's not what I'm looking for.
This one was obvious. ;-)

My question is more of 'is there some API that does this better'?
Or even, is there some smtp extension that does that?
Or, is there some server that does that, and it has a complementary Java client to 'communicate'.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21776992
Have you found where the bottleneck is yet?
0
 
LVL 17

Author Comment

by:Dejan Pažin
ID: 21777100
No, what exactly should I look for?

The issue here is, that this performance downgrade is at customer's site,
where I don't have good access to monitor things.
And I don't see usefulness in testing my own env with different smtp server.
Where I also doubt they would allow/open customer's smtp server for my 'spam' tests.

I would resort to this as the last option - running my own tests against my own smtp server.
That's why I'm asking if somebody has encountered similar issue before. :-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21777204
You could turn up logging to debug in all components and get them to send you the logs
0
 
LVL 17

Author Comment

by:Dejan Pažin
ID: 21777497
This must be classical Java Mail issue.
 - http://java.sun.com/products/javamail/
I don't think debugging that will do me any good.
Unless there is something specific (in that lib) you have in mind?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21778069
Well it's not 'debugging' but logging. You should particularly look at logging around timings in the transport classes
0
 
LVL 17

Author Comment

by:Dejan Pažin
ID: 21778404
OK, even if I do see where the bottleneck is, what can I do then?
It's Java Mail, no way it's gonna change anytime soon.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 21778644
If it's in the network/transport layer then you can look into that independently of the Java.
0
 
LVL 17

Author Comment

by:Dejan Pažin
ID: 21779321
No idea what I could do then.
So I'm only interested in Java part of this optimization.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21780700
Well there's nothing you can do in any layer until you've identified where the bottleneck is.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We are happy to announce a brand new addition to our line of acclaimed email signature management products – CodeTwo Email Signatures for Office 365.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question