From 0c15faa69b87740fd5b534e03378f3f845533bbd Mon Sep 17 00:00:00 2001 From: Faiz Akram <156657523+faizorg@users.noreply.github.com> Date: Sat, 23 Mar 2024 13:01:31 +0530 Subject: [PATCH 1/2] Update Readme file --- pom.xml | 5 ++- .../com/app/controller/EmailController.java | 35 +++++++++++++++++++ src/main/resources/application.yml | 20 +++++++++-- 3 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/app/controller/EmailController.java diff --git a/pom.xml b/pom.xml index 24a58e3..5133724 100644 --- a/pom.xml +++ b/pom.xml @@ -21,7 +21,10 @@ org.springframework.boot spring-boot-starter-web - + + org.springframework.boot + spring-boot-starter-mail + org.springframework.boot spring-boot-starter-test diff --git a/src/main/java/com/app/controller/EmailController.java b/src/main/java/com/app/controller/EmailController.java new file mode 100644 index 0000000..f981002 --- /dev/null +++ b/src/main/java/com/app/controller/EmailController.java @@ -0,0 +1,35 @@ +package com.app.controller; + +import jakarta.mail.MessagingException; +import jakarta.mail.internet.MimeMessage; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.MimeMessageHelper; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("email") +public class EmailController { + private final JavaMailSender javaMailSender; + private static final String NO_REPLY_ADDRESS = "faiz.krm@yopmail.com"; + + EmailController(JavaMailSender javaMailSender) { + this.javaMailSender = javaMailSender; + } + + @PostMapping + public ResponseEntity sendEmail(@RequestParam String[] to, @RequestParam String subject, @RequestParam String body) throws MessagingException { + MimeMessage message = javaMailSender.createMimeMessage(); + MimeMessageHelper helper = new MimeMessageHelper(message, "UTF-8"); + helper.setFrom(NO_REPLY_ADDRESS); + helper.setTo(to); + helper.setSubject(subject); + helper.setText(body); + javaMailSender.send(message); + return new ResponseEntity<>("Success", HttpStatus.OK); + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 56fbd1d..be44848 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,6 +1,20 @@ spring: - application: - name: QuickStartSpringBoot + application: + name: QuickStartSpringBoot + mail: + host: <> + port: <> + username: <> + password: <> + properties: + mail: + smtp: + starttls: + enable: true + auth: true + default-encoding: UTF-8 server: - port: 8080 + port: 8080 + + From 5acaa5f1c1c35659786dc3a2e04fc2291080e547 Mon Sep 17 00:00:00 2001 From: Faiz Akram <156657523+faizorg@users.noreply.github.com> Date: Sat, 23 Mar 2024 13:02:29 +0530 Subject: [PATCH 2/2] Update Readme file --- README.md | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 001bf80..2b18810 100644 --- a/README.md +++ b/README.md @@ -16,9 +16,9 @@ ## Project Structure - **src/main/java**: Contains Java source code. -- `com.example.demo`: Main package containing application code. +- `com.app`: Main package containing application code. - **src/main/resources**: Contains application properties, static files, and templates. -- `application.properties`: Configuration properties for the application. +- `application.yml`: Configuration properties for the application. - `static/`: Directory for static resources like CSS, JavaScript, etc. - `templates/`: Directory for HTML templates. - **src/test**: Contains test source code. @@ -26,22 +26,20 @@ ## Dependencies - Spring Boot Starter Web: `spring-boot-starter-web` +- Spring Boot Starter Mail: `spring-boot-starter-mail` - Spring Boot Starter Test: `spring-boot-starter-test` - [Add any additional dependencies here] ## API Documentation -- [Link to API documentation if available] +# Adding Email Service to Spring Boot Project -## Contributing -1. Fork the repository. -2. Create a new branch: `git checkout -b feature/[feature_name]` -3. Commit your changes: `git commit -am 'Add new feature'` -4. Push to the branch: `git push origin feature/[feature_name]` -5. Submit a pull request. +## 1. Add Dependency +Ensure that you have the necessary dependency in your `pom.xml` file to enable email functionality: -## License -[Specify the project's license, e.g., MIT License, Apache License] +```xml + + org.springframework.boot + spring-boot-starter-mail + -## Authors -[List project contributors and maintainers]