与smtp服务器的连接池
就像我有5个smtp服务器,我想做批量邮件,并想在每台服务器上发布,那么我如何才能实现它? 我现在这样使用:与smtp服务器的连接池,smtp,connection-pooling,Smtp,Connection Pooling,就像我有5个smtp服务器,我想做批量邮件,并想在每台服务器上发布,那么我如何才能实现它? 我现在这样使用: String smtpHost=”smtp.gmail.com”; javaMailSender.setHost(smtpHost); Properties mailProps = new Properties(); mailProps.put(“mail.smtp.connectiontimeout”, “2000”); mailProps.put(“mail.smtp.timeout
String smtpHost=”smtp.gmail.com”;
javaMailSender.setHost(smtpHost);
Properties mailProps = new Properties();
mailProps.put(“mail.smtp.connectiontimeout”, “2000”);
mailProps.put(“mail.smtp.timeout”, “2000”);
mailProps.put(“mail.debug”, “false”);
javaMailSender.setJavaMailProperties(mailProps);
现在我想在多个VIP上发布
String smtpHost=”192.168.xx.xx,192.168.xx.xx,192.168.xx.xx”;
你能建议我如何做到这一点吗?你可以使用
使用不同服务器的属性创建会话,例如
Properties mailServerProperties = new Properties();
mailServerProperties.put("mail.smtp.port",String.valueOf(port));
Session session = Session.getDefaultInstance(mailServerProperties);
在应用程序开始时创建SmtpConnectionPool(例如每IP)
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxTotal(5);
SmtpConnectionFactory smtpConnectionFactory = SmtpConnectionFactoryBuilder.newSmtpBuilder()
.session(session).port(port).protocol("smtp")
.build();
SmtpConnectionPool smtpConnectionPool = new SmtpConnectionPool(smtpConnectionFactory, config);
然后,您可以在地图中按IP浏览池
pools.put(ip, smtpConnectionPool);
在发送邮件时,您可以从Map中获取一个池,然后从池中借用连接并发送邮件
SmtpConnectionPool smtpConnectionPool = pools.get(ip);
try (ClosableSmtpConnection transport = smtpConnectionPool.borrowObject()) {
MimeMessage mimeMessage = new MimeMessage(transport.getSession());
mimeMessage.setFrom(new InternetAddress(email.getFrom()));
mimeMessage.addRecipients(MimeMessage.RecipientType.TO, Util.getAddresses(email.getTo()));
mimeMessage.setSubject(email.getSubject());
mimeMessage.setContent(email.getBody(), "text/html");
transport.sendMessage(mimeMessage);
} catch (Exception e) {
e.printStackTrace();
}
你也应该考虑有一些排队排序机制,因为发送大邮件应该是后台作业。
谢谢AUJ,SMTP池是有帮助的。