From d696daed058bd9d39054dc3983836e2ae5d0fd59 Mon Sep 17 00:00:00 2001 From: e20178dulanga Date: Mon, 14 Oct 2024 13:17:14 +0530 Subject: [PATCH] Email sending with the notification --- .../notification/NotificationService.java | 31 ++++++++++++++----- back-end/src/main/resources/application.yml | 9 +++--- front-end/src/components/Contact.module.css | 2 +- 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/back-end/src/main/java/com/example/demo/notification/NotificationService.java b/back-end/src/main/java/com/example/demo/notification/NotificationService.java index 98b708f0..baa448ab 100644 --- a/back-end/src/main/java/com/example/demo/notification/NotificationService.java +++ b/back-end/src/main/java/com/example/demo/notification/NotificationService.java @@ -2,9 +2,10 @@ import com.example.demo.appuser.AppUser; import com.example.demo.appuser.AppUserRepository; -import com.example.demo.appuser.AppUserService; -import com.example.demo.appuser.EmailRequest; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.MimeMessageHelper; +import org.springframework.mail.SimpleMailMessage; import org.springframework.stereotype.Service; import java.util.List; @@ -12,25 +13,39 @@ @Service public class NotificationService { private final NotificationRepository notificationRepository; - @Autowired - private AppUserRepository appUserRepository; + private final AppUserRepository appUserRepository; + private final JavaMailSender mailSender; @Autowired - public NotificationService(NotificationRepository notificationRepository){ + public NotificationService(NotificationRepository notificationRepository, AppUserRepository appUserRepository, JavaMailSender mailSender) { this.notificationRepository = notificationRepository; + this.appUserRepository = appUserRepository; + this.mailSender = mailSender; } - public Notification createNotification(String message, AppUser user, String notificationType){ + public Notification createNotification(String message, AppUser user, String notificationType) { Notification notification = new Notification(message, user, notificationType); - return notificationRepository.save(notification); + notification = notificationRepository.save(notification); + sendNotificationEmail(user.getEmail(), message); // Send email after saving notification + return notification; } public List getUserNotifications(Long userID) { - AppUser user = appUserRepository.findById(userID).get(); + AppUser user = appUserRepository.findById(userID).orElse(null); return notificationRepository.findByUser(user); } public void deleteNotification(Long notificationId) { notificationRepository.deleteById(notificationId); } + + // Method to send email + private void sendNotificationEmail(String toEmail, String message) { + SimpleMailMessage email = new SimpleMailMessage(); + email.setTo(toEmail); + email.setSubject("New Notification"); + email.setText(message); + + mailSender.send(email); // Send the email + } } diff --git a/back-end/src/main/resources/application.yml b/back-end/src/main/resources/application.yml index b0bfedee..d31a7ebd 100644 --- a/back-end/src/main/resources/application.yml +++ b/back-end/src/main/resources/application.yml @@ -19,10 +19,10 @@ spring: show-sql: true mail: - host: localhost - port: 1025 - username: hello - password: hello + host: smtp.gmail.com + port: 587 + username: cycleuop@gmail.com + password: rtwo vani qyiy kbgj properties: mail: smtp: @@ -35,6 +35,7 @@ spring: timeout: 3000 write timeout: 5000 + servlet: multipart: max-file-size: 100MB # Increase size for large files diff --git a/front-end/src/components/Contact.module.css b/front-end/src/components/Contact.module.css index 7b6d5a77..35acd17b 100644 --- a/front-end/src/components/Contact.module.css +++ b/front-end/src/components/Contact.module.css @@ -108,7 +108,7 @@ .contactUsForm img{ width: auto; height: 400px; - margin-top: 150px; + margin-top: 200px; margin-right: 20px; margin-left: 20px; }