-
Notifications
You must be signed in to change notification settings - Fork 3
Add email sending functionality with Google SMTP #54
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
817875c
Add email sending functionality with Google SMTP
abdulkeremkilic 19e9ff7
Update src/main/java/org/justjava/gymcore/model/MailModel.java
abdulkeremkilic 1f3adca
Reformat MailModel record indentation
abdulkeremkilic 26cd517
Update src/main/java/org/justjava/gymcore/controller/MailController.java
abdulkeremkilic 8c27339
Update src/main/resources/application.properties
abdulkeremkilic 3dec3aa
Update src/main/java/org/justjava/gymcore/service/MailService.java
abdulkeremkilic fcc3b22
Update src/main/java/org/justjava/gymcore/service/MailService.java
abdulkeremkilic 0413e42
Improve MailService logging and response type
abdulkeremkilic 6c72aef
Improve mail sending error handling and validation
abdulkeremkilic 333cccc
Make googleSmtpConfig final in MailService
abdulkeremkilic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/main/java/org/justjava/gymcore/config/smtp/AbstractSmtpConfig.java
This file contains hidden or 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,58 @@ | ||
| package org.justjava.gymcore.config.smtp; | ||
|
|
||
| public abstract class AbstractSmtpConfig { | ||
| private String host; | ||
| private String port; | ||
| private String username; //use with encryption | ||
| private String password; //use with encryption | ||
| private String auth; | ||
| private String starttls; | ||
|
abdulkeremkilic marked this conversation as resolved.
abdulkeremkilic marked this conversation as resolved.
|
||
|
|
||
| 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 getUsername() { | ||
| return username; | ||
| } | ||
|
|
||
| public void setUsername(String username) { | ||
| this.username = username; | ||
| } | ||
|
|
||
| public String getPassword() { | ||
| return password; | ||
| } | ||
|
|
||
| public void setPassword(String password) { | ||
| this.password = password; | ||
| } | ||
|
|
||
| public String getAuth() { | ||
| return auth; | ||
| } | ||
|
|
||
| public void setAuth(String auth) { | ||
| this.auth = auth; | ||
| } | ||
|
|
||
| public String getStarttls() { | ||
| return starttls; | ||
| } | ||
|
|
||
| public void setStarttls(String starttls) { | ||
| this.starttls = starttls; | ||
| } | ||
| } | ||
9 changes: 9 additions & 0 deletions
9
src/main/java/org/justjava/gymcore/config/smtp/GoogleSmtpConfig.java
This file contains hidden or 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,9 @@ | ||
| package org.justjava.gymcore.config.smtp; | ||
|
|
||
| import org.springframework.boot.context.properties.ConfigurationProperties; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| @Configuration | ||
| @ConfigurationProperties(prefix = "smtp.google") | ||
| public class GoogleSmtpConfig extends AbstractSmtpConfig { | ||
| } |
35 changes: 35 additions & 0 deletions
35
src/main/java/org/justjava/gymcore/controller/MailController.java
This file contains hidden or 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,35 @@ | ||
| package org.justjava.gymcore.controller; | ||
|
|
||
| import jakarta.mail.MessagingException; | ||
| import jakarta.validation.Valid; | ||
| import jakarta.validation.constraints.Email; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.justjava.gymcore.model.MailModel; | ||
| import org.justjava.gymcore.service.MailService; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| @RestController | ||
| @RequestMapping(value = "/mail") | ||
| @RequiredArgsConstructor | ||
| public class MailController { | ||
|
|
||
| private final MailService mailService; | ||
|
|
||
| @PostMapping("/sendMail/{to}") | ||
| public ResponseEntity<String> sendMail( @PathVariable("to") | ||
| @Email(message = "Invalid email address format") | ||
| String to, | ||
| @Valid @RequestBody MailModel mailModel) { | ||
| try { | ||
| mailService.sendMail(to, mailModel.subject(), mailModel.body()); | ||
| return ResponseEntity.ok() | ||
| .body("Email sent successfully to: " + to); | ||
| } catch (MessagingException e) { | ||
| return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) | ||
| .body("Failed to send email: " + e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| } |
This file contains hidden or 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 org.justjava.gymcore.model; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.Size; | ||
|
|
||
| public record MailModel( | ||
| @NotBlank(message = "Email subject cannot be blank") | ||
| @Size(max = 998, message = "Email subject cannot exceed 998 characters") | ||
| String subject, | ||
|
|
||
| @NotBlank(message = "Email body cannot be blank") | ||
| @Size(max = 50000, message = "Email body cannot exceed 50KB") | ||
| String body | ||
| ) { | ||
| } |
47 changes: 47 additions & 0 deletions
47
src/main/java/org/justjava/gymcore/service/MailService.java
This file contains hidden or 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,47 @@ | ||
| package org.justjava.gymcore.service; | ||
|
|
||
| import jakarta.mail.*; | ||
| import jakarta.mail.internet.InternetAddress; | ||
| import jakarta.mail.internet.MimeMessage; | ||
| import lombok.RequiredArgsConstructor; | ||
|
abdulkeremkilic marked this conversation as resolved.
|
||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.justjava.gymcore.config.smtp.GoogleSmtpConfig; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.util.Properties; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class MailService { | ||
|
|
||
| private final GoogleSmtpConfig googleSmtpConfig; | ||
|
|
||
| // Host, Host e-mail, password and port will be provided under yaml file. If you want to get configurations from yaml file use smtpConfigurationService. | ||
| public void sendMail(String to, String subject, String body) throws MessagingException { | ||
|
|
||
| Properties props = new Properties(); | ||
| props.put("mail.transport.protocol", "smtp"); | ||
| props.put("mail.smtp.host", googleSmtpConfig.getHost()); | ||
| props.put("mail.smtp.auth", googleSmtpConfig.getAuth()); | ||
| props.put("mail.smtp.port", googleSmtpConfig.getPort()); | ||
| props.put("mail.smtp.starttls.enable", googleSmtpConfig.getStarttls()); | ||
|
|
||
|
|
||
| Authenticator authenticator = new Authenticator() { | ||
| protected PasswordAuthentication getPasswordAuthentication() { | ||
| return new PasswordAuthentication(googleSmtpConfig.getUsername(), googleSmtpConfig.getPassword()); | ||
|
abdulkeremkilic marked this conversation as resolved.
|
||
| } | ||
| }; | ||
|
|
||
| Session session = Session.getInstance(props, authenticator); | ||
| Message message = new MimeMessage(session); | ||
| message.setFrom(new InternetAddress(googleSmtpConfig.getUsername())); | ||
| message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false)); | ||
| message.setSubject(subject); | ||
| message.setText(body); | ||
|
|
||
| Transport.send(message); | ||
| log.info("Email sent successfully to: {}", to); | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.