19
19
import java .util .HashSet ;
20
20
import java .util .Set ;
21
21
22
- import javax .annotation .PostConstruct ;
23
22
import javax .mail .MessagingException ;
24
23
import javax .mail .internet .MimeMessage ;
25
24
43
42
*/
44
43
45
44
@ Component
46
- public class SendMail {
45
+ public class EmailSender {
47
46
48
- private static final Logger LOGGER = LoggerFactory .getLogger (SendMail .class );
47
+ private static final Logger LOGGER = LoggerFactory .getLogger (EmailSender .class );
49
48
50
49
@ Getter
51
50
@ Value ("${email.sender}" )
@@ -58,74 +57,82 @@ public class SendMail {
58
57
@ Autowired
59
58
private JavaMailSender emailSender ;
60
59
61
- public void setMailSender (JavaMailSender emailSender ) {
60
+ public void setEmailSender (JavaMailSender emailSender ) {
62
61
this .emailSender = emailSender ;
63
62
}
64
63
65
64
/**
66
- * This method takes two arguments i.e receiver mail-id and aggregatedObject and send mail to the
67
- * receiver with aggregatedObject as the body .
65
+ * This method sends mail to the given receivers mail address(es) with the
66
+ * given email subject and body from the mapNotificationMessage .
68
67
*
69
- * @param receiver
68
+ * @param receivers
69
+ * Who to send the mail to
70
70
* @param mapNotificationMessage
71
+ * A String to be used as the body of the email
72
+ * @param emailSubject
73
+ * The subject of the email to send
71
74
*/
72
- public void sendMail (String receiver , String mapNotificationMessage , String emailSubject )
73
- throws MessagingException {
74
- Set <String > extEmails = new HashSet <>();
75
- try {
76
- extEmails = extractEmails (receiver );
77
- } catch (SubscriptionValidationException e ) {
78
- LOGGER .error (e .getMessage ());
79
- e .printStackTrace ();
80
- }
75
+ public void sendEmail (String receivers , String mapNotificationMessage , String emailSubject ) {
76
+ Set <String > emails = new HashSet <>();
77
+ emails = extractEmails (receivers );
78
+ String [] to = emails .toArray (new String [0 ]);
79
+ MimeMessage message = prepareEmail (mapNotificationMessage , emailSubject , to );
80
+ emailSender .send (message );
81
+ }
81
82
83
+ /**
84
+ * This method creates a MimeMessageHelper and prepares the email to send
85
+ *
86
+ * @param mapNotificationMessage
87
+ * A String to be used by the body of the email
88
+ * @param emailSubject
89
+ * The subject of the email to send
90
+ * @param receivers
91
+ * Who to send the email to
92
+ * */
93
+ private MimeMessage prepareEmail (String mapNotificationMessage , String emailSubject , String [] receivers ) {
82
94
MimeMessage message = emailSender .createMimeMessage ();
83
- MimeMessageHelper helper = new MimeMessageHelper (message , true );
84
- String [] to = extEmails .toArray (new String [0 ]);
85
95
try {
96
+ MimeMessageHelper helper = new MimeMessageHelper (message , true );
86
97
helper .setFrom (sender );
87
98
helper .setSubject (getSubject (emailSubject ));
88
99
helper .setText (mapNotificationMessage );
89
- helper .setTo (to );
100
+ helper .setTo (receivers );
90
101
} catch (MessagingException e ) {
91
102
LOGGER .error (e .getMessage ());
92
103
e .printStackTrace ();
93
104
}
94
- emailSender . send ( message ) ;
105
+ return message ;
95
106
}
96
107
97
108
/**
98
- * This method takes string of comma separated email addresses and return the Set of validated email
99
- * addresses
109
+ * This method takes a string of comma separated email addresses and
110
+ * puts them in a Set of email addresses to return.
100
111
*
101
- * @param contents
112
+ * @param receivers
113
+ * A string containing one or more comma separated email addresses
114
+ * @return emailAdd
102
115
*/
103
- public Set <String > extractEmails (String contents ) throws SubscriptionValidationException {
116
+ public Set <String > extractEmails (String receivers ) {
104
117
Set <String > emailAdd = new HashSet <>();
105
- String [] addresses = contents .split ("," );
118
+ String [] addresses = receivers .split ("," );
106
119
for (String add : addresses ) {
107
- SubscriptionValidator .validateEmail (add .trim ());
108
120
emailAdd .add (add );
109
121
}
110
122
return emailAdd ;
111
123
}
112
124
113
125
/**
114
- * This method takes the user provided email subject and if it is not empty, return it. Otherwise,
115
- * it return the default subject
126
+ * This method takes the user provided email subject and if it is not empty,
127
+ * returns it. Otherwise, it returns the default subject.
116
128
*
117
129
* @param emailSubject
130
+ * @return emailSubject
118
131
*/
119
- public String getSubject (String emailSubject ) {
132
+ private String getSubject (String emailSubject ) {
120
133
if (emailSubject .isEmpty ()) {
121
134
return subject ;
122
135
}
123
136
return emailSubject ;
124
137
}
125
-
126
- @ PostConstruct
127
- public void display () {
128
- LOGGER .debug ("Email Sender : " + sender );
129
- LOGGER .debug ("Email Subject : " + subject );
130
- }
131
138
}
0 commit comments