Multiple mail servers on a Spring application


I'm developing an application with Spring. This application has customers, and each customer has his own mail server configuration (port, host, username, password,etc). My application must send emails through the Customers mail servers. I mean, I cannot use the classic:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">

There are going to be many MailServerImpls, one per customer, and I will have hundreds of them. Each time a Customer signs in, he is asked for this mail server configuration.

Upon certain actions of the customer, my application must send an email using the Customer mail server.

So, what is the best way to do this with Spring? I hope the best solution is not to do a new MailServerImpl() and set the attributes each time I have to send an email...

Thank you very much.


Read the port, host, username, password,etc from a .properties file or from the database. This way you will have one mailSender implementation but configured different per client.

You could use <a href="http://commons.apache.org/proper/commons-email/userguide.html" rel="nofollow">Apache Commons Email</a> and not using a bean but creating an object everytime and sending the mail:

Email email = new SimpleEmail(); email.setHostName("smtp.googlemail.com"); email.setSmtpPort(465); email.setAuthenticator(new DefaultAuthenticator("username", "password")); email.setSSLOnConnect(true); email.setFrom("user@gmail.com"); email.setSubject("TestMail"); email.setMsg("This is a test mail ... :-)"); email.addTo("foo@bar.com"); email.send();

The values you would store in the database in a relationship to the user.


This question is a bit vague to me, without going into details how you represent customer and their email server configuration.

What you could do is to have mapping between Customers and their configuration in some dictionary structure.

Accept concurrent requests from customer(with their identifier) and add it to some FIFO queue, then have a thread pool accepting and processing these requests as they come. You would have control on number of worker threads and Email implementation instances which could be reused.

Each thread could be associated with instance of whichever Email implementation you are using if this implementation has class variables. You could maybe use Email abstraction singleton shared between all threads, where connection properties are only in local scope of method.


  • How to register UDF/RTD within VSTO project
  • How to fix this iterative python code and reduce repetition?
  • What to do when your ant build process craps on your Version Control
  • What are the maximum columns in an Access 2003 query?
  • How the game will search for other online users and will display the list of all users?
  • Matlab: “grouping mean”
  • MS Access query with dynamic from statements
  • mysqli_result::free increase php memory usage
  • WPF Listbox commands
  • CSS Grid, position absolute an element in a css grid item: IMPOSSIBLE
  • Neo4j: Filter nodes based on aggregate function
  • Command line installation of Code Signing certificates, .p12 files, and mobileprovisions
  • in batch how do i use taskkill properly
  • how to get data attributes of dynamically generated element
  • Magento get URL before current
  • Clear activity stack before launching another activity
  • ListItem.Attributes.Add not working
  • How do I access an unhandled exception in an MVC Error view?
  • How to change the font size of a single index for UISegmentedControl?
  • Email verification using google app script and google forms
  • Refering to the class itself from within a class mehod in Objective C
  • formatting the colorbar ticklabels with SymLogNorm normalization in matplotlib
  • Email format validation in mvc3 view
  • Excel - Autoshape get it's name from cell (value)
  • Check if a string to interpolate provides expected placeholders
  • Does CUDA 5 support STL or THRUST inside the device code?
  • sending/ receiving email in Java
  • RestKit - RKRequestDelegate does not exist
  • Traverse Array and Display in markup
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • SQL merge duplicate rows and join values that are different
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • Qt: Run a script BEFORE make
  • LevelDB C iterator
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • Can't mass-assign protected attributes when import data from csv file
  • reshape alternating columns in less time and using less memory