vairam
vairam

Reputation: 471

Better Approach for sending bulk mail using JavaMail API

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

Answers (2)

bvyas
bvyas

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

Bill Shannon
Bill Shannon

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

Related Questions