diff --git a/README.md b/README.md index 96c7159..1c858fb 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,10 @@ _Require ssh tunnel to an ldap server and an WAR archive_ LDAP_NEW_USER_BASE_DN=ou=people,dc=jenkins-ci,dc=org RECAPTCHA_PRIVATE_KEY=recaptcha_private_key RECAPTCHA_PUBLIC_KEY=recaptcha_public_key - SMTP_SERVER=localhost + SMTP_SERVER=smtp.jenkins.io + SMTP_USER=user@jenkins.io + SMTP_AUTH=true + SMTP_PASSWORD=password ``` * Run docker-compose ```docker-compose up --build accountapp``` @@ -89,6 +92,9 @@ we may want to use environment variable. * RECAPTCHA_PUBLIC_KEY * RECAPTCHA_PRIVATE_KEY * SMTP_SERVER +* SMTP_USER +* SMTP_PASSWORD +* SMTP_AUTH ``` ## Makefile @@ -96,3 +102,10 @@ we may want to use environment variable. ``` make build```: Build build/libs/accountapp-2.5.war and docker image ``` make run ```: Run docker container ``` make clean ```: Clean build environment + +## SMTP +The accountapp support different types of SMTP configuration to send emails. +* Nothing is configured, the application try to connect on localhost:25 +* SMTP_AUTH is set to false, the accountapp will connect on $SMTP_SERVER:25 +* SMTP_AUTH is set to true, the accountapp will connect on $SMTP_SERVER:587 with tls authentication + and will use username: $SMTP_USER with password $SMTP_PASSWORD. diff --git a/config.properties.example b/config.properties.example index 7f3302d..c139a5a 100644 --- a/config.properties.example +++ b/config.properties.example @@ -10,6 +10,9 @@ newUserBaseDN=LDAP_NEW_USER_BASE_DN # Host which accountapp can use for sending out password reset and other emails # Optional: Default value set to localhost smtpServer=SMTP_SERVER +smtpUser=SMTP_USER +smtpAuth=SMTP_AUTH +smtpPassword=SMTP_PASSWORD recaptchaPublicKey=RECAPTCHA_PUBLIC_KEY recaptchaPrivateKey=RECAPTCHA_PRIVATE_KEY diff --git a/entrypoint.sh b/entrypoint.sh index 9b35431..bc16d6c 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -11,6 +11,9 @@ init_config_properties() { # /etc/accountapp/config.properties : "${SMTP_SERVER:? SMTP Server required}" + : "${SMTP_USER:? SMTP User required}" + : "${SMTP_AUTH:? SMTP Auth required}" + : "${SMTP_PASSWORD:? SMTP Password required}" : "${RECAPTCHA_PUBLIC_KEY:? Recaptcha private key}" : "${RECAPTCHA_PRIVATE_KEY:? Recaptcha private key}" : "${APP_URL:? Application url required}" @@ -35,6 +38,9 @@ init_config_properties() { # Using # as variables may contain / sed -i "s#SMTP_SERVER#$SMTP_SERVER#" /etc/accountapp/config.properties + sed -i "s#SMTP_USER#$SMTP_USER#" /etc/accountapp/config.properties + sed -i "s#SMTP_AUTH#$SMTP_AUTH#" /etc/accountapp/config.properties + sed -i "s#SMTP_PASSWORD#$SMTP_PASSWORD#" /etc/accountapp/config.properties sed -i "s#LDAP_URL#$LDAP_URL#" /etc/accountapp/config.properties sed -i "s#LDAP_PASSWORD#$LDAP_PASSWORD#" /etc/accountapp/config.properties sed -i "s#RECAPTCHA_PUBLIC_KEY#$RECAPTCHA_PUBLIC_KEY#" /etc/accountapp/config.properties diff --git a/src/main/java/org/jenkinsci/account/Application.java b/src/main/java/org/jenkinsci/account/Application.java index 6954690..6bf9bb1 100644 --- a/src/main/java/org/jenkinsci/account/Application.java +++ b/src/main/java/org/jenkinsci/account/Application.java @@ -26,6 +26,7 @@ import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; +import javax.mail.PasswordAuthentication; import javax.naming.AuthenticationException; import javax.naming.Context; import javax.naming.NameAlreadyBoundException; @@ -487,9 +488,24 @@ public class Application { } private Session createJavaMailSession() { + Session session; Properties props = new Properties(System.getProperties()); + System.out.printf(params.smtpAuth()); props.put("mail.smtp.host",params.smtpServer()); - return Session.getInstance(props); + if(params.smtpAuth().equals("true")) { + props.put("mail.smtp.auth", params.smtpAuth()); + props.put("mail.smtp.starttls.enable", true); + props.put("mail.smtp.port", 587); + session = Session.getInstance(props, + new javax.mail.Authenticator() { + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(params.smtpUser(), params.smtpPassword()); + } + }); + } else { + session = Session.getInstance(props); + } + return session; } /** diff --git a/src/main/java/org/jenkinsci/account/Parameters.java b/src/main/java/org/jenkinsci/account/Parameters.java index 6361fec..3edb9fa 100644 --- a/src/main/java/org/jenkinsci/account/Parameters.java +++ b/src/main/java/org/jenkinsci/account/Parameters.java @@ -20,7 +20,16 @@ public interface Parameters { String managerPassword(); String server(); + /** + * smtpServer: The SMTP server to connect to. + * smtpUser: Default user name for SMTP. + * smtpAuth: If true, attempt to authenticate the user using the AUTH command. + * smtpPassword: SMTP password for SMTP server. + */ String smtpServer(); + String smtpUser(); + String smtpAuth(); + String smtpPassword(); String recaptchaPublicKey(); String recaptchaPrivateKey();