Initial commit
This commit is contained in:
38
store-email-sender/pom.xml
Normal file
38
store-email-sender/pom.xml
Normal file
@@ -0,0 +1,38 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>store-email-sender</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<artifactId>hexagonal-architecture</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<relativePath>../../hexagonal-architecture</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>store-core</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.mail</groupId>
|
||||
<artifactId>javax.mail</artifactId>
|
||||
<version>${javax.mail.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.context.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<spring.context.version>5.1.5.RELEASE</spring.context.version>
|
||||
<javax.mail.version>1.5.5</javax.mail.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.hexagonal.store.emailsender;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = {"com.baeldung.hexagonal.store.emailsender"})
|
||||
public class EmailSenderConfig {
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.hexagonal.store.emailsender;
|
||||
|
||||
import com.baeldung.hexagonal.store.core.context.order.infrastructure.EmailNotificationSender;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.mail.Message;
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.Session;
|
||||
import javax.mail.Transport;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import java.util.Properties;
|
||||
|
||||
@Component
|
||||
public class SMTPEmailSender implements EmailNotificationSender {
|
||||
|
||||
public static final String FROM_MAIL = "example@baeldung.com";
|
||||
|
||||
@Override
|
||||
public void sendEmailMessage(String targetEmail, String subject, String body) {
|
||||
try {
|
||||
Session session = createSession();
|
||||
MimeMessage message = new MimeMessage(session);
|
||||
message.setContent(body, "text/html; charset=utf-8");
|
||||
message.setFrom(new InternetAddress(FROM_MAIL));
|
||||
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(targetEmail));
|
||||
message.setSubject(subject);
|
||||
Transport.send(message);
|
||||
} catch (MessagingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static Session createSession() {
|
||||
Properties props = new Properties();
|
||||
props.put("mail.smtp.auth", "true");
|
||||
props.put("mail.smtp.starttls.enable", "true");
|
||||
props.put("mail.smtp.host", "smtp.baeldung.com");
|
||||
props.put("mail.smtp.port", "587");
|
||||
return Session.getInstance(props);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user