Skip to content

Commit

Permalink
#19 add email notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin POCHAT authored and Benjamin POCHAT committed Jan 5, 2021
1 parent 3fd35b0 commit 22c5fe3
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 0 deletions.
38 changes: 38 additions & 0 deletions core/src/main/java/com/localeat/core/commons/EmailService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.localeat.core.commons;

import com.localeat.core.config.email.EmailConfig;
import com.localeat.core.config.security.LocalEatRSAKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.mail.*;
import javax.mail.internet.MimeMessage;

@Controller
public class EmailController {

private static final Logger LOGGER = LoggerFactory.getLogger(EmailController.class);

@Autowired
EmailConfig emailConfig;

public void sendMail(String recipient, String subject, String body) {
Session session = Session.getInstance(emailConfig.getProperties(), new SimpleAuthenticator());

try {
MimeMessage msg = new MimeMessage(session);
msg.setFrom("[email protected]");
msg.setRecipients(Message.RecipientType.TO, recipient);
msg.setSubject(subject);
msg.setText(body, "UTF-8", "html");
Transport.send(msg, emailConfig.getUserName(), emailConfig.getPassword());
} catch (MessagingException mex) {
LOGGER.error("send mail failed", mex);
}
}

private class SimpleAuthenticator extends Authenticator {
}
}
64 changes: 64 additions & 0 deletions core/src/main/java/com/localeat/core/config/email/EmailConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.localeat.core.config.email;

import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.Properties;

@ConfigurationProperties("localeat.smtp")
public class EmailProperties {
private boolean authentication;
private boolean startTls;
private String host;
private String port;
private String sslTrust;

public boolean isAuthentication() {
return authentication;
}

public void setAuthentication(boolean authentication) {
this.authentication = authentication;
}

public boolean isStartTls() {
return startTls;
}

public void setStartTls(boolean startTls) {
this.startTls = startTls;
}

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}

public String getPort() {
return port;
}

public void setPort(String port) {
this.port = port;
}

public String getSslTrust() {
return sslTrust;
}

public void setSslTrust(String sslTrust) {
this.sslTrust = sslTrust;
}

public Properties getProperties() {
var properties = new Properties();
properties.put("mail.smtp.auth", authentication);
properties.put("mail.smtp.starttls.enable", startTls);
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", port);
properties.put("mail.smtp.ssl.trust", sslTrust);
return properties;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.localeat.core.domains.actor;

public interface BreederRepository {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.localeat.core.domains.order;

public class OrderNotificationService {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.localeat.core.domains.security;

public class PasswordRenewalController {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.localeat.core.commons;

public class TestEmailController {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.localeat.core.domains.order;

public class TestOrderNotificationService {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.localeat.core.domains.security;

public class TestPasswordRenewalController {
}

0 comments on commit 22c5fe3

Please sign in to comment.