Skip to content

Commit

Permalink
added email
Browse files Browse the repository at this point in the history
  • Loading branch information
sayeedajmal committed May 27, 2024
1 parent 25b4366 commit 8d5b759
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class VehicleController {
@PostMapping("challan")
public ResponseEntity<Void> generateChallan(@RequestBody Vehicle vehicle) throws SpeedSyncException {
vehicleService.challanSave(vehicle);
mailService.sendEmail(vehicle.getEmail(), vehicle);
mailService.sendEmail(vehicle);
return new ResponseEntity<>(HttpStatus.CREATED);
}
}
7 changes: 3 additions & 4 deletions src/main/java/com/strong/speedsyncrestful/Entity/Vehicle.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ public class Vehicle {

@Indexed(direction = IndexDirection.ASCENDING)
private String carNumber;
private Map<String, Object> vehicleDetails;
private Location location;
private int currentSpeed;
private String email;
private String highway;

private float currentSpeed;
private Location location;
private Map<String, Object> vehicleDetails;
private long timestamp;

}
190 changes: 94 additions & 96 deletions src/main/java/com/strong/speedsyncrestful/Service/MailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

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

/**
* Service class for sending emails for various events in the Blood Donation
Expand All @@ -21,105 +20,104 @@
@EnableAsync
public class MailService {

@Autowired
private JavaMailSender emailSender;
@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);
/**
* 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(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 email = vehicle.getEmail();
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);
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);
helper.setTo(email);
helper.setSubject("Challan Notification");
helper.setText(htmlContent, true);

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

}

0 comments on commit 8d5b759

Please sign in to comment.