Skip to content

Commit

Permalink
#bbottema/simple-java-mail#430: reconnect disconnected Transport inst…
Browse files Browse the repository at this point in the history
…ances when being claimed for imminent reuse.
  • Loading branch information
bbottema committed Jan 5, 2023
1 parent 6444d01 commit e401f8b
Showing 1 changed file with 24 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package org.simplejavamail.smtpconnectionpool;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jakarta.mail.MessagingException;
import jakarta.mail.NoSuchProviderException;
import jakarta.mail.Session;
import jakarta.mail.Transport;
import jakarta.mail.*;
import lombok.val;
import org.bbottema.genericobjectpool.Allocator;
import org.jetbrains.annotations.NotNull;
Expand All @@ -30,26 +27,35 @@ public Transport allocate() {
LOGGER.trace("opening transport connection...");
try {
Transport transport = session.getTransport();
connectTransport(transport, session);
connectTransport(transport);
return transport;
} catch (NoSuchProviderException e) {
throw new TransportHandlingException("unable to get transport from session:\n\t" + session.getProperties(), e);
} catch (MessagingException e) {
throw new TransportHandlingException("Error when trying to open connection to the server, session:\n\t" + session.getProperties(), e);
}
}

private static void connectTransport(Transport transport, Session session) throws MessagingException {
val oauth2Token = (String) session.getProperties().getOrDefault(OAUTH2_TOKEN_PROPERTY, null);
if (oauth2Token != null) {
/*
* To connect using OAuth2 authentication, we need to connect slightly differently as we can't use only Session properties and the traditional Authenticator class for
* providing password. Instead, <em>mail.smtp.auth</em> is set to {@code false} and the OAuth2 authenticator should take over, but this is only triggered succesfully if we
* provide an empty non-null password, which is only possible using the alternative {@link Transport#connect(String, String)}.
*/
transport.connect(session.getProperties().getProperty("mail.smtp.user"), oauth2Token);
} else {
transport.connect();
@Override
public void allocateForReuse(Transport transport) {
if (!transport.isConnected()) {
connectTransport(transport);
}
}

private void connectTransport(Transport transport) {
try {
val oauth2Token = (String) session.getProperties().getOrDefault(OAUTH2_TOKEN_PROPERTY, null);
if (oauth2Token != null) {
/*
* To connect using OAuth2 authentication, we need to connect slightly differently as we can't use only Session properties and the traditional Authenticator class for
* providing password. Instead, <em>mail.smtp.auth</em> is set to {@code false} and the OAuth2 authenticator should take over, but this is only triggered succesfully if we
* provide an empty non-null password, which is only possible using the alternative {@link Transport#connect(String, String)}.
*/
transport.connect(session.getProperties().getProperty("mail.smtp.user"), oauth2Token);
} else {
transport.connect();
}
} catch (MessagingException e) {
throw new TransportHandlingException("Error when trying to open connection to the server, session:\n\t" + session.getProperties(), e);
}
}

Expand Down

0 comments on commit e401f8b

Please sign in to comment.