Optimizing Java mail client

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?
LVL 17
Dejan PažinHead of SW DevelopmentAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
If it's in the network/transport layer then you can look into that independently of the Java.
0
 
CEHJCommented:
You are using the same Session throughout the loop? How many mails take 5-6hrs?
0
 
Dejan PažinHead of SW DevelopmentAuthor Commented:
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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
CEHJCommented:
Creating a new Session in a loop (if that's what's happening) will give you unnecessary overhead
0
 
Dejan PažinHead of SW DevelopmentAuthor Commented:
No, this class is a singleton in my app.
Hence single Session instance.
0
 
CEHJCommented:
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
 
CEHJCommented:
OK.

a. Create a Session, s
b. Create JavaMailSenderImpl, jmsi
c. Call jmsi.setSession(s)
d. Now send in a loop
0
 
Dejan PažinHead of SW DevelopmentAuthor Commented:
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
 
CEHJCommented:
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
 
Dejan PažinHead of SW DevelopmentAuthor Commented:
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
 
CEHJCommented:
Have you found where the bottleneck is yet?
0
 
Dejan PažinHead of SW DevelopmentAuthor Commented:
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
 
CEHJCommented:
You could turn up logging to debug in all components and get them to send you the logs
0
 
Dejan PažinHead of SW DevelopmentAuthor Commented:
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
 
CEHJCommented:
Well it's not 'debugging' but logging. You should particularly look at logging around timings in the transport classes
0
 
Dejan PažinHead of SW DevelopmentAuthor Commented:
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
 
Dejan PažinHead of SW DevelopmentAuthor Commented:
No idea what I could do then.
So I'm only interested in Java part of this optimization.
0
 
CEHJCommented:
Well there's nothing you can do in any layer until you've identified where the bottleneck is.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.