Closed rohandodeja closed 7 years ago
Could you post a full example of how you send an email, including factory configuration and connection borrows?
Factory Config:-
Properties props = conn.getSmtpPropeprties(); Authenticator auth=null; props.put("mail.smtp.auth", "true"); //enable authentication auth = new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(props.getProperty("user"), props.getProperty("password")); } }; props.setProperty("mail.smtp.timeout", GatewayConstants.SMTP_TIMEOUT); props.setProperty("mail.smtp.connectiontimeout",GatewayConstants.SMTP_CONNECTION_TIMEOUT); props.setProperty("mail.smtp.writetimeout",GatewayConstants.SMTP_WRITE_TIMEOUT); Session session = Session.getInstance(props, auth); SmtpConnectionFactory factory = SmtpConnectionFactories.newSmtpFactory(session); GenericObjectPoolConfig config = new GenericObjectPoolConfig();
SmtpConnectionPool smtpConnectionPool = new SmtpConnectionPool(factory, config);
Sending email :-
try(ClosableSmtpConnection transport = SmtpConnectionPools.getPoolConnection(getConnectionId())){
if(!transport.isConnected()){ //got disconnected transport object reconnecting it. logger.info("got disconnected transport object reconnecting it."); ((SMTPTransport) transport.getDelegate()).connect(); } transport.sendMessage(mmm, mmm.getAllRecipients()); String response=((SMTPTransport) transport.getDelegate()).getLastServerResponse();
What is SmtpConnectionPools
and what does this method do SmtpConnectionPools.getPoolConnection(getConnectionId())
?
Actually this method internally fetch a connection from pool :-
transport = fetchedPool.borrowObject();
Try to activate the test on borrow, eg.:
GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
genericObjectPoolConfig.setTestOnBorrow(true);
Ok , so this method will ensure that pooled connection will be returned in connected state for use, and also when connection is returned back to pool after use then also it will be saved in connected state?
This method only ensure that pooled transport will be returned in connected state for use.
If the transport is not connected, the primer is destroyed and a new connection is created.
Appreciate for help, no issues for now :+1: .
Great!
I have added a PoolConfigs
to ease pool configuration
I used
transport.isConnected() which results false sometime .
now I'm using connect method as follows :-
((SMTPTransport) transport.getDelegate()).connect();
now how do i set this connected connection to pool again as i got disconnected transport object .