Skip to content

Commit

Permalink
Merge pull request #7 from migangqui/release/1.2.0
Browse files Browse the repository at this point in the history
[release/1.2.0]
  • Loading branch information
migangqui committed Jan 30, 2023
2 parents bc194c6 + 5e15d9a commit 8538652
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 77 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ Spring Email API (Java/Kotlin)
![Maven Central](https://img.shields.io/maven-central/v/com.github.migangqui/spring-email-api-java?style=for-the-badge)


It is an API for Java and Kotlin to send emails with Spring. To add to the project, only do these things.
It is an API for Java and Kotlin to send emails with Spring. To add to your project...

### Add dependency to pom.xml
### Add dependency to Maven or Gradle:

For Java:

Expand All @@ -18,6 +18,9 @@ For Java:
<version>${currentVersion}</version>
</dependency>
```
```groovy
implementation 'com.github.migangqui:spring-email-api-java:${currentVersion}'
```

For Kotlin:

Expand All @@ -28,8 +31,11 @@ For Kotlin:
<version>${currentVersion}</version>
</dependency>
```
```groovy
implementation 'com.github.migangqui:spring-email-api-kotlin:${currentVersion}'
```

```${currentVersion}``` is ```1.1.0```
```${currentVersion}``` is ```1.2.0```

### Add the following properties in application.yml of the project

Expand Down
12 changes: 3 additions & 9 deletions spring-email-java-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<version>2.7.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<java.version>17</java.version>
</properties>

<dependencies>
Expand All @@ -33,13 +33,7 @@
<dependency>
<groupId>com.github.migangqui</groupId>
<artifactId>spring-email-api-java</artifactId>
<version>1.0.1</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>1.2.0</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import com.github.migangqui.spring.email.model.Email;
import com.github.migangqui.spring.email.model.SendEmailResult;
import com.github.migangqui.spring.email.service.EmailService;
import com.github.migangqui.spring.email.service.EmailSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand All @@ -26,15 +25,15 @@ public static void main(String[] args) {
}

@Autowired
private EmailService emailService;
private EmailSender emailSender;

@PostMapping
public SendEmailResult sendEmail(@RequestBody Email email) {
return emailService.send(email);
return emailSender.send(email);
}

@PostMapping("/async")
public SendEmailResult sendEmailAsync(@RequestBody Email email) throws ExecutionException, InterruptedException {
return emailService.sendAsync(email).get();
return emailSender.sendAsync(email).get();
}
}
2 changes: 1 addition & 1 deletion spring-email-java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ It is an Java API to send email with Spring.
</dependency>
```

```${currentVersion}``` is ```1.1.0```
```${currentVersion}``` is ```1.2.0```
13 changes: 3 additions & 10 deletions spring-email-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.migangqui</groupId>
<artifactId>spring-email-api-java</artifactId>
<version>1.1.0</version>
<version>1.2.0</version>

<name>spring-email-api-java</name>
<description>Java API to send emails in Spring</description>
Expand Down Expand Up @@ -32,9 +32,8 @@
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<lombok.version>1.18.22</lombok.version>
<commons-io.version>2.11.0</commons-io.version>
<spring-boot.version>2.6.6</spring-boot.version>
<spring-boot.version>2.7.8</spring-boot.version>
</properties>

<dependencies>
Expand All @@ -48,12 +47,6 @@
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -97,7 +90,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.3.2</version>
<version>3.4.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@

import com.github.migangqui.spring.email.service.EmailSender;
import com.github.migangqui.spring.email.service.EmailSenderImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;

@Slf4j
@Configuration
public class EmailSenderConfig {

@Bean
public EmailSender emailSender(final JavaMailSender javaMailSender) {
log.debug("Registering EmailSender bean");
return new EmailSenderImpl(javaMailSender);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
import java.io.InputStream;
import java.util.Optional;

public record Email(String from, String to, String subject, String body,
Optional<InputStream> file, Optional<String> filename) {
public record Email(String from, String to, String subject, String body, Optional<EmailAttachment> attachment) {

public Email(String from, String to, String subject, String body) {
this(from, to, subject, body, Optional.empty(), Optional.empty());
this(from, to, subject, body, Optional.empty());
}

public Email(String from, String to, String subject, String body, InputStream file, String filename) {
this(from, to, subject, body, Optional.ofNullable(file), Optional.ofNullable(filename));
this(from, to, subject, body, Optional.of(new EmailAttachment(file, filename)));
}

public record EmailAttachment(InputStream file, String filename) {}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.github.migangqui.spring.email.service;

import com.github.migangqui.spring.email.model.SendEmailResult;
import com.github.migangqui.spring.email.model.Email;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.github.migangqui.spring.email.model.SendEmailResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.InputStreamResource;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
Expand All @@ -16,15 +16,19 @@
import javax.mail.internet.MimeMessage;
import java.util.concurrent.Future;

@Slf4j
@RequiredArgsConstructor
public class EmailSenderImpl implements EmailSender {

private static final Logger log = LoggerFactory.getLogger(EmailSenderImpl.class);

private final JavaMailSender javaMailSender;

public EmailSenderImpl(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}

@Override
public SendEmailResult send(final Email email) {
log.debug("Sending email");
log.info("Sending email");
try {
javaMailSender.send(generateMailMessage(email));
log.debug("Email sent successfully");
Expand All @@ -45,15 +49,16 @@ public Future<SendEmailResult> sendAsync(final Email email) {

private MimeMessage generateMailMessage(final Email email) throws MessagingException {
final MimeMessageHelper helper = new MimeMessageHelper(
javaMailSender.createMimeMessage(), email.file().isPresent());
javaMailSender.createMimeMessage(), email.attachment().isPresent());

helper.setFrom(new InternetAddress(email.from()));
helper.setTo(email.to());
helper.setSubject(email.subject());
helper.setText(email.body(), true);

if (email.file().isPresent()) {
helper.addAttachment(email.filename().get(), new InputStreamResource(email.file().get()));
if (email.attachment().isPresent()) {
helper.addAttachment(email.attachment().get().filename(),
new InputStreamResource(email.attachment().get().file()));
}

return helper.getMimeMessage();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.github.migangqui.spring.email.config.EmailSenderConfig
30 changes: 9 additions & 21 deletions spring-email-kotlin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.github.migangqui</groupId>
<artifactId>spring-email-api-kotlin</artifactId>
<version>1.1.0</version>
<version>1.2.0</version>

<name>spring-email-api-kotlin</name>
<description>Kotlin API to send emails in Spring</description>
Expand Down Expand Up @@ -34,8 +34,8 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<kotlin.version>1.6.20</kotlin.version>
<spring-boot.version>2.6.6</spring-boot.version>
<kotlin.version>1.8.0</kotlin.version>
<spring-boot.version>2.7.8</spring-boot.version>
<commons-io.version>2.11.0</commons-io.version>
</properties>

Expand All @@ -60,30 +60,18 @@
<artifactId>kotlin-reflect</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>io.github.microutils</groupId>
<artifactId>kotlin-logging</artifactId>
<version>1.6.10</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version> <!-- or whatever current version -->
<version>3.10.1</version> <!-- or whatever current version -->
<configuration>
<source>1.8</source>
<target>1.8</target>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<plugin>
Expand Down Expand Up @@ -113,7 +101,7 @@
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
<jvmTarget>1.8</jvmTarget>
<jvmTarget>17</jvmTarget>
</configuration>
<dependencies>
<dependency>
Expand Down Expand Up @@ -153,7 +141,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.3.2</version>
<version>3.4.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
Expand All @@ -180,7 +168,7 @@
<plugin>
<groupId>org.jetbrains.dokka</groupId>
<artifactId>dokka-maven-plugin</artifactId>
<version>1.6.10</version>
<version>1.7.20</version>
<executions>
<execution>
<phase>prepare-package</phase>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.github.migangqui.spring.email.model

import java.io.InputStream

data class Email(
val from: String,
val to: String,
val subject: String? = null,
val body: String,
val file: InputStream? = null,
val filename: String? = null
val attachment: EmailAttachment?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.github.migangqui.spring.email.model

import java.io.InputStream

data class EmailAttachment(
val file: InputStream,
val filename: String
)
Loading

0 comments on commit 8538652

Please sign in to comment.