Skip to content

Commit

Permalink
added Email Integration
Browse files Browse the repository at this point in the history
  • Loading branch information
sayeedajmal committed May 27, 2024
1 parent dca0ec7 commit 25b4366
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import com.strong.speedsyncrestful.Entity.Vehicle;
import com.strong.speedsyncrestful.Exception.SpeedSyncException;
import com.strong.speedsyncrestful.Service.MailService;
import com.strong.speedsyncrestful.Service.VehicleService;

@RestController
Expand All @@ -18,10 +19,13 @@ public class VehicleController {

@Autowired
private VehicleService vehicleService;
@Autowired
private MailService mailService;

@PostMapping("challan")
public ResponseEntity<Void> generateChallan(@RequestBody Vehicle vehicle) throws SpeedSyncException {
vehicleService.challanSave(vehicle);
mailService.sendEmail(vehicle.getEmail(), vehicle);
return new ResponseEntity<>(HttpStatus.CREATED);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class Vehicle {
private Map<String, Object> vehicleDetails;
private Location location;
private int currentSpeed;
private String email;
private String highway;

private long timestamp;
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/strong/speedsyncrestful/Service/AppConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.strong.speedsyncrestful.Service;

import java.util.Properties;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

@Configuration
public class AppConfig {

@Value("${speedSync.Email}")
private String email;

@Value("${speedSync.Password}")
private String password;

@Bean
public JavaMailSender getJavaMailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost("smtp.gmail.com");
mailSender.setPort(587);

mailSender.setUsername(email);
mailSender.setPassword(password);

Properties props = mailSender.getJavaMailProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.debug", "false");

return mailSender;
}

}
125 changes: 125 additions & 0 deletions src/main/java/com/strong/speedsyncrestful/Service/MailService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package com.strong.speedsyncrestful.Service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;

import com.strong.speedsyncrestful.Entity.Vehicle;

import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import lombok.NonNull;

/**
* Service class for sending emails for various events in the Blood Donation
* application.
*/
@Service
@EnableAsync
public class MailService {

@Autowired
private JavaMailSender emailSender;

/**
* Sends a MimeMessage email with the provided details.
*
* @param to the recipient email address
* @param subject the subject of the email
* @param text the content of the email
*/
@Async
public void sendEmail(@NonNull String to, Vehicle vehicle) {
try {
MimeMessage message = emailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);

String email = to;
String carNumber = vehicle.getCarNumber();
String currentSpeed = vehicle.getCurrentSpeed() + " km/h";
double latitude = vehicle.getLocation().getLatitude();
double longitude = vehicle.getLocation().getLongitude();
String currentLocation = String.valueOf(latitude + ":" + longitude);
String challanPrice = "$" + 500;

String htmlContent = String.format(
"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Challan Notification</title>
<style>
body {
font-family: Arial, sans-serif;
}
.container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.header {
text-align: center;
margin-bottom: 20px;
}
.header h1 {
margin: 0;
font-size: 24px;
color: #333;
}
.content {
line-height: 1.6;
}
.content p {
margin: 10px 0;
}
.footer {
text-align: center;
margin-top: 20px;
font-size: 12px;
color: #777;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>Challan Notification</h1>
</div>
<div class="content">
<p><strong>Email:</strong> %s</p>
<p><strong>Car Number:</strong> %s</p>
<p><strong>Current Speed:</strong> %s</p>
<p><strong>Current Location:</strong> %s</p>
<p><strong>Challan Price:</strong> %s</p>
<p>Dear Valued Driver,</p>
<p>We understand that the road can sometimes be unpredictable. However, ensuring everyone's safety is our top priority. Your recent speed exceeded the limit in a monitored area. Please take this as a gentle reminder to drive safely and adhere to speed regulations. Your attention to road safety not only protects you but also contributes to a safer community.</p>
<p>Thank you for your cooperation.</p>
</div>
<div class="footer">
<p>This is an automated notification. Please do not reply to this email.</p>
</div>
</div>
</body>
</html>
""",
email, carNumber, currentSpeed, currentLocation, challanPrice);

helper.setTo(to);
helper.setSubject("Challan Notification");
helper.setText(htmlContent, true);

emailSender.send(message);
} catch (MessagingException e) {
System.out.println("Failed to send email: " + e.getMessage());
}
}

}
5 changes: 4 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ spring.data.mongodb.database=${SpeedMongoDB}
#server.ssl.key-store-type=PKCS12
#server.ssl.key-store=SpeedSyncAPI.p12
#server.ssl.key-store-password=SpeedSyncAPI
#server.ssl.key-alias=SpeedSyncAPI
#server.ssl.key-alias=SpeedSyncAPI

speedSync.Email=${EMAIL}
speedSync.Password=${PASSWORD}

0 comments on commit 25b4366

Please sign in to comment.