Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add settings for TLS and mail server authentication to EmailService #933

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 56 additions & 2 deletions src/main/java/au/org/ala/biocache/service/EmailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import javax.annotation.PostConstruct;

import jakarta.mail.Transport;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
Expand All @@ -45,11 +46,18 @@ public class EmailService {
private String host;
@Value("${mail.smtp.port:25}")
private String port;
@Value("${mail.smtp.starttls.enable:false}")
private boolean starttls;
@Value("${mail.smtp.username:}")
private String username;
@Value("${mail.smtp.password:}")
private String password;

@PostConstruct
protected void init(){
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", port);
properties.put("mail.smtp.starttls.enable", starttls);
}

/**
Expand All @@ -62,7 +70,7 @@ protected void init(){
* @param sender
*/
public void sendEmail(String recipient, String copy, String subject, String content, String sender){

logger.debug("Send email to : " + recipient);
// logger.debug("Body: " + content);
Session session = Session.getDefaultInstance(properties);
Expand All @@ -78,7 +86,11 @@ public void sendEmail(String recipient, String copy, String subject, String cont
}
message.setSubject(subject);
message.setContent(content, "text/html" );
Transport.send(message);
if (StringUtils.isNotBlank(username)) {
Transport.send(message, username, password);
} else {
Transport.send(message);
}
} catch (Exception e){
logger.error("Unable to send email to " + recipient + ".\n"+content, e);
}
Expand Down Expand Up @@ -148,4 +160,46 @@ public String getSender() {
public void setSender(String sender) {
this.sender = sender;
}

/**
* @return the starttls flag
*/
public boolean getStarttls() {
return starttls;
}

/**
* @param starttls the starttls flag to set
*/
public void setStarttls(boolean starttls) {
this.starttls = starttls;
}

/**
* @return the username
*/
public String getUsername() {
return username;
}

/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}

/**
* @return the password
*/
public String getPassword() {
return password;
}

/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
}