Skip to content

Commit

Permalink
Merge pull request #139 from e20178dulanga/main
Browse files Browse the repository at this point in the history
Email sending with the notification
  • Loading branch information
e20178dulanga authored Oct 14, 2024
2 parents 359e55a + d696dae commit b09cb64
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,50 @@

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;

@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<Notification> 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
}
}
9 changes: 5 additions & 4 deletions back-end/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ spring:
show-sql: true

mail:
host: localhost
port: 1025
username: hello
password: hello
host: smtp.gmail.com
port: 587
username: [email protected]
password: rtwo vani qyiy kbgj
properties:
mail:
smtp:
Expand All @@ -35,6 +35,7 @@ spring:
timeout: 3000
write timeout: 5000


servlet:
multipart:
max-file-size: 100MB # Increase size for large files
Expand Down
2 changes: 1 addition & 1 deletion front-end/src/components/Contact.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
.contactUsForm img{
width: auto;
height: 400px;
margin-top: 150px;
margin-top: 200px;
margin-right: 20px;
margin-left: 20px;
}
Expand Down

0 comments on commit b09cb64

Please sign in to comment.