Reputation: 471
Is there any better approach for sending bulk mail using JavaMail API.I use the below approach.
enter code here Transport tr = session.getTransport("smtp");
tr.connect(smtphost, username, password);
tr.sendMessage(msg, msg.getAllRecipients());
i used to send 'n'number of mails using the same connection. I is there any other separate way for sending bulk mail.Kindly help me in this for getting better solution.
Upvotes: 0
Views: 2752
Reputation: 451
You can use Thread pooling as it gives very good performance.I have implemented and sharing you the below code snippet.
try {
ExecutorService executor = Executors.newFixedThreadPool("no. of threads");
// no. of threads is depend on your cpu/memory usage it's better to test with diff. no. of threads.
Runnable worker = new MyRunnable(message);
// message is the javax.mail.Message
executor.execute(worker);
executor.shutdown();
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
}
Upvotes: 0
Reputation: 29971
In what way do you want it to be "better"?
You can use multiple threads to send more messages in parallel, up to the limit of what your mail server will allow.
Upvotes: 0