-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathEmailServiceImpl.java
More file actions
155 lines (134 loc) · 8.76 KB
/
EmailServiceImpl.java
File metadata and controls
155 lines (134 loc) · 8.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package com.webapp.bankingportal.service;
import java.io.File;
import java.util.concurrent.CompletableFuture;
import com.webapp.bankingportal.dto.UserResponse;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import jakarta.mail.MessagingException;
import lombok.val;
import lombok.extern.slf4j.Slf4j;
@Service
@Slf4j
public class EmailServiceImpl implements EmailService {
private final JavaMailSender mailSender;
public EmailServiceImpl(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
@Override
@Async
public CompletableFuture<Void> sendEmail(String to, String subject, String text) {
val future = new CompletableFuture<Void>();
try {
val message = mailSender.createMimeMessage();
val helper = new MimeMessageHelper(message, true);
helper.setTo(to);
// From address is automatically set by Spring Boot based on your properties
helper.setSubject(subject);
helper.setText(text, true); // Set the second parameter to true to send HTML content
mailSender.send(message);
log.info("Sent email to {}", to);
future.complete(null);
} catch (MessagingException | MailException e) {
log.error("Failed to send email to {}", to, e);
future.completeExceptionally(e);
}
return future;
}
@Override
public String getLoginEmailTemplate(String name, String loginTime, String loginLocation) {
return "<div style=\"font-family: Helvetica, Arial, sans-serif; min-width: 320px; max-width: 1000px; margin: 0 auto; overflow: auto; line-height: 2; background-color: #f1f1f1; padding: 20px;\">"
+ "<div style=\"margin: 50px auto; width: 100%; max-width: 600px; padding: 20px; background-color: #ffffff; border-radius: 8px; box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);\">"
+ "<div style=\"border-bottom: 1px solid #ddd; padding-bottom: 10px; text-align: center;\">"
+ "<a href=\"https://onestopbank.netlify.app/\" style=\"text-decoration: none;\">"
+ "<img src=\"https://onestopbank.netlify.app/assets/onestoplogo.jpg\" alt=\"OneStopBank\" style=\"height: 50px; margin-bottom: 10px;\">"
+ "</a>" + "<h1 style=\"font-size: 1.8em; color: #3f51b5; margin: 10px 0;\">OneStopBank</h1>" + "</div>"
+ "<div style=\"padding: 20px;\">" + "<p style=\"font-size: 1.2em; color: #333;\">Hi, " + name + ",</p>"
+ "<p style=\"font-size: 1em; color: #333;\">A login attempt was made on your account at:</p>"
+ "<p style=\"font-size: 1em; color: #555;\">Time: <strong style=\"color: #3f51b5;\">" + loginTime
+ "</strong></p>"
+ "<p style=\"font-size: 1em; color: #555;\">Location: <strong style=\"color: #3f51b5;\">"
+ loginLocation + "</strong></p>"
+ "<p style=\"font-size: 1em; color: #333;\">If this was you, no further action is required. If you suspect any unauthorized access, please change your password immediately and contact our support team.</p>"
+ "<p style=\"font-size: 1em; color: #555;\">Regards,<br />The OneStopBank Team</p>" + "</div>"
+ "<hr style=\"border: none; border-top: 1px solid #ddd; margin: 20px 0;\" />"
+ "<div style=\"text-align: center; font-size: 0.9em; color: #888;\">"
+ "<p>Need help? Contact our support team:</p>"
+ "<p>Email: <a href=\"mailto:onestopbank@google.com\" style=\"color: #3f51b5; text-decoration: none;\">onestopbank@google.com</a></p>"
+ "<div style=\"margin-top: 20px;\">"
+ "<p style=\"font-size: 1em; color: #333;\">Show your support here ❤️</p>"
+ "<a href=\"https://github.com/abhi9720/BankingPortal-API\" style=\"margin: 0 10px; color: #3f51b5; text-decoration: none;\">GitHub</a>"
+ "</div>" + "</div>" + "</div>" + "</div>";
}
@Override
public String getOtpLoginEmailTemplate(String name, String accountNumber, String otp) {
return "<div style=\"font-family: Helvetica, Arial, sans-serif; min-width: 320px; max-width: 1000px; margin: 0 auto; overflow: auto; line-height: 2; background-color: #f1f1f1; padding: 20px;\">"
+ "<div style=\"margin: 50px auto; width: 100%; max-width: 600px; padding: 20px; background-color: #ffffff; border-radius: 8px; box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);\">"
+ "<div style=\"border-bottom: 1px solid #ddd; padding-bottom: 10px; text-align: center;\">"
+ "<a href=\"https://onestopbank.netlify.app/\" style=\"text-decoration: none;\">"
+ "<img src=\"https://onestopbank.netlify.app/assets/onestoplogo.jpg\" alt=\"OneStopBank\" style=\"height: 50px; margin-bottom: 10px;\">"
+ "</a>" + "<h1 style=\"font-size: 1.8em; color: #3f51b5; margin: 10px 0;\">OneStopBank</h1>" + "</div>"
+ "<div style=\"padding: 20px;\">" + "<p style=\"font-size: 1.2em; color: #333;\">Hi, " + name + ",</p>"
+ "<p style=\"font-size: 1em; color: #555;\">Account Number: <strong style=\"color: #3f51b5;\">"
+ accountNumber + "</strong></p>"
+ "<p style=\"font-size: 1em; color: #333;\">Thank you for choosing OneStopBank. Use the following OTP to complete your login procedures. The OTP is valid for "
+ OtpServiceImpl.OTP_EXPIRY_MINUTES + " minutes:</p>"
+ "<h2 style=\"background: #3f51b5; margin: 20px 0; width: max-content; padding: 10px 20px; color: #fff; border-radius: 4px;\">"
+ otp + "</h2>" + "<p style=\"font-size: 1em; color: #555;\">Regards,<br />The OneStopBank Team</p>"
+ "</div>" + "<hr style=\"border: none; border-top: 1px solid #ddd; margin: 20px 0;\" />"
+ "<div style=\"text-align: center; font-size: 0.9em; color: #888;\">"
+ "<p>Need help? Contact our support team:</p>"
+ "<p>Email: <a href=\"mailto:onestopbank@google.com\" style=\"color: #3f51b5; text-decoration: none;\">onestopbank@google.com</a></p>"
+ "<div style=\"margin-top: 20px;\">"
+ "<p style=\"font-size: 1em; color: #333;\">Show your support here ❤️</p>"
+ "<a href=\"https://github.com/abhi9720/BankingPortal-API\" style=\"margin: 0 10px; color: #3f51b5; text-decoration: none;\">GitHub</a>"
+ "</div>" + "</div>" + "</div>" + "</div>";
}
@Override
public String getBankStatementEmailTemplate(String name, String statementText) {
return "<div style=\"font-family: Arial, sans-serif; padding: 20px;\">" +
"<h2>Bank Statement</h2>" +
"<p>Dear " + name + ",</p>" +
"<p>Here is your latest bank statement:</p>" +
"<pre style=\"background: #f4f4f4; padding: 10px; border-radius: 5px;\">" +
statementText +
"</pre>" +
"<p>Regards,<br/>OneStopBank Team</p>" +
"</div>";
}
public String getReconciliationReportTemplate(String user,
String introSentence,
String reportContent) {
return """
<html>
<body style="font-family: Arial, sans-serif;">
<h2>Dear %s,</h2>
<p>%s</p>
<h3>Your Reconciliation Report</h3>
<pre style="background-color:#f4f4f4;padding:10px;border:1px solid #ddd;">
%s
</pre>
<p>Thank you for banking with OneStopBank.</p>
<p>Regards,<br/>OneStopBank Team</p>
</body>
</html>
""".formatted(user, introSentence, reportContent);
}
public void sendEmailWithAttachment(String to, String subject, String text, String attachmentFilePath) {
try {
val message = mailSender.createMimeMessage();
val helper = new MimeMessageHelper(message, true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(text, true); // Set the second parameter to true to send HTML content
// Add an attachment to the email
val attachmentFile = new File(attachmentFilePath);
helper.addAttachment(attachmentFile.getName(), attachmentFile);
mailSender.send(message);
} catch (MessagingException e) {
log.error("Failed to send email to {}", to, e);
}
}
}