1

I am using java mail for sending emails. As the service is multithreaded every thread is creating a new connection to the email server. Is there a way to create an SMTP connection pool so every thread will use the same connection for sending emails?

One thing I have tried is- I am storing the Session transport object in hashmap so I can use that transport object in each thread. I am not sure if it is the correct way or not.

Vaibhav
  • 113
  • 1
  • 5
  • Regarding _"use that transport object in each thread"_, if you have not seen it already, take a look at the answers/notes in this question: [Threadsafety in Javamail](https://stackoverflow.com/questions/12732584/threadsafety-in-javamail). – andrewJames Nov 11 '20 at 19:52
  • @andrewjames- It helped me to understand Threadsafety in Javamail. Is there any better option to send bulk emails using SMTP? – Vaibhav Nov 24 '20 at 07:25
  • Have you researched that for yourself? Take a look at [How to Send bulk mails using javax.mail API efficiently](https://stackoverflow.com/questions/13287515/how-to-send-bulk-mails-using-javax-mail-api-efficiently-can-we-use-reuse-auth) or any of these [other existing questions](https://www.google.com/search?q=how+to+send+bulk+emails+using+java+site:stackoverflow.com&client=firefox-b-d&sa=X&ved=2ahUKEwivhsjtnpvtAhVZTDABHVj5BUcQrQIoBHoECAUQBQ&biw=1186&bih=771) on Stack Overflow. – andrewJames Nov 24 '20 at 13:09
  • Yes, I have gone through most of these but my problem is I am getting requests on different threads. So I don't have the same session for each thread. – Vaibhav Nov 25 '20 at 05:43

0 Answers0