Spring应用程序上的多个邮件服务器 [英] Multiple mail servers on a Spring application

查看:144
本文介绍了Spring应用程序上的多个邮件服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring开发应用程序。该应用程序有客户,每个客户都有自己的邮件服务器配置(端口,主机,用户名,密码等)。我的应用程序必须通过客户邮件服务器发送电子邮件。
我的意思是,我不能使用经典版本:

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>

会有许多MailServerImpls,每个客户一个,我将有数百个。每次客户登录时,都会要求他提供此邮件服务器配置。

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.

那么,用Spring做到这一点的最佳方法是什么?
我希望最好的解决方案是不要执行 new MailServerImpl()并在每次必须发送电子邮件时设置属性...

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...

非常感谢。

推荐答案

读取端口,主机,用户名,密码等从.properties文件或数据库中获取。这样,您将有一个mailSender实现,但每个客户端配置不同。

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.

您可以使用 Apache Commons Email ,而不使用bean,而是每次创建一个对象并发送邮件:

You could use Apache Commons Email 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.

这篇关于Spring应用程序上的多个邮件服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆