generated from it-at-m/oss-repository-en-template
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(#1023): init digiwf-email lib with the functionality of the d…
…igiwf-email-integration-core
- Loading branch information
Showing
12 changed files
with
344 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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> | ||
<parent> | ||
<groupId>de.muenchen.oss.digiwf</groupId> | ||
<artifactId>digiwf-email</artifactId> | ||
<version>1.3.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>digiwf-email-api</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-mail</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.commons</groupId> | ||
<artifactId>commons-lang3</artifactId> | ||
</dependency> | ||
|
||
<!-- testing --> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-junit-jupiter</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
19 changes: 19 additions & 0 deletions
19
...email/digiwf-email-api/src/main/java/de/muenchen/oss/digiwf/email/api/DigiwfEmailApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package de.muenchen.oss.digiwf.email.api; | ||
|
||
import de.muenchen.oss.digiwf.email.model.FileAttachment; | ||
import jakarta.mail.MessagingException; | ||
|
||
import java.util.List; | ||
|
||
public interface DigiwfEmailApi { | ||
|
||
|
||
void sendMail(String receivers, String subject, String body, String replyTo) throws MessagingException; | ||
|
||
void sendMail(String receivers, String subject, String body, String replyTo, String receiversCc, String receiversBcc) throws MessagingException; | ||
|
||
void sendMailWithAttachments(String receivers, String subject, String body, String replyTo, List<FileAttachment> attachments) throws MessagingException; | ||
|
||
void sendMailWithAttachments(String receivers, String subject, String body, String replyTo, String receiversCc, String receiversBcc, List<FileAttachment> attachments) throws MessagingException; | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
.../digiwf-email-api/src/main/java/de/muenchen/oss/digiwf/email/impl/DigiwfEmailApiImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package de.muenchen.oss.digiwf.email.impl; | ||
|
||
import de.muenchen.oss.digiwf.email.api.DigiwfEmailApi; | ||
import de.muenchen.oss.digiwf.email.model.FileAttachment; | ||
import jakarta.mail.Message; | ||
import jakarta.mail.MessagingException; | ||
import jakarta.mail.internet.InternetAddress; | ||
import jakarta.mail.internet.MimeMessage; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import lombok.val; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.mail.javamail.MimeMessageHelper; | ||
|
||
import java.util.List; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class DigiwfEmailApiImpl implements DigiwfEmailApi { | ||
|
||
private final JavaMailSender mailSender; | ||
private final String fromAddress; | ||
|
||
@Override | ||
public void sendMail(String receivers, String subject, String body, String replyTo) throws MessagingException { | ||
this.sendMailWithAttachments(receivers, subject, body, replyTo, null, null, List.of()); | ||
} | ||
|
||
@Override | ||
public void sendMail(String receivers, String subject, String body, String replyTo, String receiversCc, String receiversBcc) throws MessagingException { | ||
this.sendMailWithAttachments(receivers, subject, body, replyTo, receiversCc, receiversBcc, List.of()); | ||
} | ||
|
||
@Override | ||
public void sendMailWithAttachments(String receivers, String subject, String body, String replyTo, List<FileAttachment> attachments) throws MessagingException { | ||
this.sendMailWithAttachments(receivers, subject, body, replyTo, null, null, attachments); | ||
} | ||
|
||
@Override | ||
public void sendMailWithAttachments(String receivers, String subject, String body, String replyTo, String receiversCc, String receiversBcc, List<FileAttachment> attachments) throws MessagingException { | ||
final MimeMessage mimeMessage = this.mailSender.createMimeMessage(); | ||
|
||
mimeMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(receivers)); | ||
|
||
if (StringUtils.isNotEmpty(receiversCc)) { | ||
mimeMessage.setRecipients(Message.RecipientType.CC, InternetAddress.parse(receiversCc)); | ||
} | ||
if (StringUtils.isNotEmpty(receiversCc)) { | ||
mimeMessage.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(receiversBcc)); | ||
} | ||
|
||
final var helper = new MimeMessageHelper(mimeMessage, true); | ||
|
||
helper.setSubject(subject); | ||
helper.setText(subject); | ||
helper.setFrom(this.fromAddress); | ||
|
||
if (StringUtils.isNotBlank(replyTo)) { | ||
helper.setReplyTo(replyTo); | ||
} | ||
|
||
// mail attachments | ||
if (attachments != null) { | ||
for (val attachment : attachments) { | ||
helper.addAttachment(attachment.getFileName(), attachment.getFile()); | ||
} | ||
} | ||
|
||
this.mailSender.send(mimeMessage); | ||
log.info("Mail {} sent to {}.", subject, receivers); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
...ail/digiwf-email-api/src/main/java/de/muenchen/oss/digiwf/email/model/FileAttachment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package de.muenchen.oss.digiwf.email.model; | ||
|
||
import jakarta.mail.util.ByteArrayDataSource; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Data | ||
@RequiredArgsConstructor | ||
@AllArgsConstructor | ||
public class FileAttachment { | ||
|
||
private String fileName; | ||
|
||
private ByteArrayDataSource file; | ||
|
||
} |
95 changes: 95 additions & 0 deletions
95
...l/digiwf-email-api/src/test/java/de/muenchen/oss/digiwf/email/DigiwfEmailApiImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package de.muenchen.oss.digiwf.email; | ||
|
||
import de.muenchen.oss.digiwf.email.api.DigiwfEmailApi; | ||
import de.muenchen.oss.digiwf.email.impl.DigiwfEmailApiImpl; | ||
import de.muenchen.oss.digiwf.email.model.FileAttachment; | ||
import jakarta.mail.MessagingException; | ||
import jakarta.mail.Session; | ||
import jakarta.mail.internet.MimeMessage; | ||
import jakarta.mail.util.ByteArrayDataSource; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.ArgumentCaptor; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
|
||
import java.util.List; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
class DigiwfEmailApiImplTest { | ||
|
||
|
||
private final JavaMailSender javaMailSender = mock(JavaMailSender.class); | ||
private DigiwfEmailApi digiwfEmailApi; | ||
|
||
// test data | ||
private final String receiver = "[email protected],[email protected]"; | ||
private final String receiverCC = "[email protected]"; | ||
private final String receiverBCC = "[email protected]"; | ||
private final String subject = "Test Mail"; | ||
private final String body = "This is a test mail"; | ||
private final String replyTo = "[email protected]"; | ||
|
||
|
||
@BeforeEach | ||
void setUp() { | ||
when(this.javaMailSender.createMimeMessage()).thenReturn(new MimeMessage((Session) null)); | ||
this.digiwfEmailApi = new DigiwfEmailApiImpl(this.javaMailSender, "[email protected]"); | ||
} | ||
|
||
@Test | ||
void sendSimpleMail() throws MessagingException { | ||
this.digiwfEmailApi.sendMail(this.receiver, this.subject, this.body, this.replyTo); | ||
|
||
final ArgumentCaptor<MimeMessage> messageArgumentCaptor = ArgumentCaptor.forClass(MimeMessage.class); | ||
verify(this.javaMailSender).send(messageArgumentCaptor.capture()); | ||
|
||
Assertions.assertEquals(2, messageArgumentCaptor.getValue().getAllRecipients().length); | ||
Assertions.assertEquals(1, messageArgumentCaptor.getValue().getReplyTo().length); | ||
Assertions.assertEquals(this.subject, messageArgumentCaptor.getValue().getSubject()); | ||
} | ||
|
||
@Test | ||
void sendMailWithCCAndBCC() throws MessagingException { | ||
this.digiwfEmailApi.sendMail(this.receiver, this.subject, this.body, this.replyTo, this.receiverCC, this.receiverBCC); | ||
|
||
final ArgumentCaptor<MimeMessage> messageArgumentCaptor = ArgumentCaptor.forClass(MimeMessage.class); | ||
verify(this.javaMailSender).send(messageArgumentCaptor.capture()); | ||
|
||
Assertions.assertEquals(4, messageArgumentCaptor.getValue().getAllRecipients().length); | ||
Assertions.assertEquals(1, messageArgumentCaptor.getValue().getReplyTo().length); | ||
Assertions.assertEquals(this.subject, messageArgumentCaptor.getValue().getSubject()); | ||
} | ||
|
||
@Test | ||
void sendMailWithAttachments() throws MessagingException { | ||
final List<FileAttachment> fileAttachments = List.of( | ||
new FileAttachment("Testanhang", new ByteArrayDataSource("FooBar".getBytes(), "text/plain")) | ||
); | ||
this.digiwfEmailApi.sendMailWithAttachments(this.receiver, this.subject, this.body, this.replyTo, fileAttachments); | ||
|
||
final ArgumentCaptor<MimeMessage> messageArgumentCaptor = ArgumentCaptor.forClass(MimeMessage.class); | ||
verify(this.javaMailSender).send(messageArgumentCaptor.capture()); | ||
|
||
Assertions.assertEquals(2, messageArgumentCaptor.getValue().getAllRecipients().length); | ||
Assertions.assertEquals(1, messageArgumentCaptor.getValue().getReplyTo().length); | ||
Assertions.assertEquals(this.subject, messageArgumentCaptor.getValue().getSubject()); | ||
} | ||
|
||
@Test | ||
void sendMailWithReceiversCCAndBCCAndAttachments() throws MessagingException { | ||
final List<FileAttachment> fileAttachments = List.of( | ||
new FileAttachment("Testanhang", new ByteArrayDataSource("FooBar".getBytes(), "text/plain")) | ||
); | ||
this.digiwfEmailApi.sendMailWithAttachments(this.receiver, this.subject, this.body, this.replyTo, this.receiverCC, this.receiverBCC, fileAttachments); | ||
|
||
final ArgumentCaptor<MimeMessage> messageArgumentCaptor = ArgumentCaptor.forClass(MimeMessage.class); | ||
verify(this.javaMailSender).send(messageArgumentCaptor.capture()); | ||
|
||
Assertions.assertEquals(4, messageArgumentCaptor.getValue().getAllRecipients().length); | ||
Assertions.assertEquals(1, messageArgumentCaptor.getValue().getReplyTo().length); | ||
Assertions.assertEquals(this.subject, messageArgumentCaptor.getValue().getSubject()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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> | ||
<parent> | ||
<groupId>de.muenchen.oss.digiwf</groupId> | ||
<artifactId>digiwf-email</artifactId> | ||
<version>1.3.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>digiwf-email-starter</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>de.muenchen.oss.digiwf</groupId> | ||
<artifactId>digiwf-email-api</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-configuration-processor</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
24 changes: 24 additions & 0 deletions
24
...rc/main/java/de/muenchen/oss/digiwf/email/configuration/DigiwfEmailAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package de.muenchen.oss.digiwf.email.configuration; | ||
|
||
import de.muenchen.oss.digiwf.email.api.DigiwfEmailApi; | ||
import de.muenchen.oss.digiwf.email.impl.DigiwfEmailApiImpl; | ||
import de.muenchen.oss.digiwf.email.properties.CustomMailProperties; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
|
||
@RequiredArgsConstructor | ||
@EnableConfigurationProperties(value = CustomMailProperties.class) | ||
public class DigiwfEmailAutoConfiguration { | ||
|
||
private final CustomMailProperties customMailProperties; | ||
|
||
@ConditionalOnMissingBean | ||
@Bean | ||
public DigiwfEmailApi digiwfEmailApi(final JavaMailSender javaMailSender) { | ||
return new DigiwfEmailApiImpl(javaMailSender, this.customMailProperties.getFromAddress()); | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
...l-starter/src/main/java/de/muenchen/oss/digiwf/email/properties/CustomMailProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package de.muenchen.oss.digiwf.email.properties; | ||
|
||
import lombok.Data; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
@Data | ||
@ConfigurationProperties(prefix = "io.muenchendigital.digiwf.mail") | ||
public class CustomMailProperties { | ||
|
||
/** | ||
* Sender mail address. | ||
*/ | ||
private String fromAddress; | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
...esources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
de.muenchen.oss.digiwf.email.configuration.DigiwfEmailAutoConfiguration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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> | ||
<parent> | ||
<groupId>de.muenchen.oss.digiwf</groupId> | ||
<artifactId>digiwf-libs</artifactId> | ||
<version>1.3.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>digiwf-email</artifactId> | ||
<packaging>pom</packaging> | ||
|
||
<modules> | ||
<module>digiwf-email-api</module> | ||
<module>digiwf-email-starter</module> | ||
</modules> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters