-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dca0ec7
commit 25b4366
Showing
5 changed files
with
172 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/java/com/strong/speedsyncrestful/Service/AppConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
125
src/main/java/com/strong/speedsyncrestful/Service/MailService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters