1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.hack23.cia.service.impl.email;
20
21 import java.util.Properties;
22
23 import javax.annotation.PostConstruct;
24
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.mail.SimpleMailMessage;
29 import org.springframework.mail.javamail.JavaMailSender;
30 import org.springframework.mail.javamail.JavaMailSenderImpl;
31 import org.springframework.security.access.annotation.Secured;
32 import org.springframework.stereotype.Component;
33
34 import com.hack23.cia.model.internal.application.system.impl.ApplicationConfiguration;
35 import com.hack23.cia.model.internal.application.system.impl.ConfigurationGroup;
36 import com.hack23.cia.service.data.api.ApplicationConfigurationService;
37
38
39
40
41 @Component("MailService")
42 @Secured({"ROLE_ANONYMOUS","ROLE_USER", "ROLE_ADMIN" })
43 public final class EmailServiceImpl implements EmailService {
44
45
46 private static final String MAIL_SMTP_STARTTLS_ENABLE = "mail.smtp.starttls.enable";
47
48
49 private static final String MAIL_SMTP_AUTH = "mail.smtp.auth";
50
51
52 private static final String APPLICATION_EMAIL_SMTP_STARTTLS_ENABLE = "application.email.smtp.starttls.enable";
53
54
55 private static final String APPLICATION_EMAIL_SMTP_AUTH = "application.email.smtp.auth";
56
57
58 private static final String APPLICATION_EMAIL_SMTP_SECRET = "application.email.smtp.password";
59
60
61 private static final String APPLICATION_EMAIL_SMTP_USERNAME = "application.email.smtp.username";
62
63
64 private static final String APPLICATION_EMAIL_SMTP_PORT = "application.email.smtp.port";
65
66
67 private static final String APPLICATION_EMAIL_SMTP_HOST = "application.email.smtp.host";
68
69
70 private static final String APPLICATION_EMAIL_FROM_EMAIL = "application.email.from.email";
71
72
73 private static final String APPLICATION_EMAIL_SEND_EMAIL = "application.email.send.email";
74
75
76 private static final String RESPONSIBLE_FOR_SENDING_EMAIL = "Responsible for sending email";
77
78
79 private static final String SMTP_STARTTLS_ENABLE = "Smtp starttls enable";
80
81
82 private static final String SMTP_AUTH = "Smtp auth";
83
84
85 private static final String SMTP_SECRET = "Smtp password";
86
87
88 private static final String SMTP_USERNAME = "Smtp username";
89
90
91 private static final String SMTP_PORT = "Smtp port";
92
93
94 private static final String SMTP_HOST = "Smtp Host";
95
96
97 private static final String FROM_EMAIL = "From email";
98
99
100 private static final String SEND_EMAIL = "Send email";
101
102
103 private static final String EMAIL_CONFIGURATION_SMTP_STARTTLS_ENABLE = "Email configuration smtp starttls enable";
104
105
106 private static final String EMAIL_CONFIGURATION_SMTP_AUTH = "Email configuration smtp auth";
107
108
109 private static final String EMAIL_CONFIGURATION_SMTP_SECRET = "Email configuration smtp password";
110
111
112 private static final String EMAIL_CONFIGURATION_SMTP_USERNAME = "Email configuration smtp username";
113
114
115 private static final String EMAIL_CONFIGURATION_SMTP_PORT = "Email configuration smtp port";
116
117
118 private static final String EMAIL_CONFIGURATION_SMTP_HOST = "Email configuration smtp host";
119
120
121 private static final String EMAIL_CONFIGURATION_FROM_EMAIL = "Email configuration from email";
122
123
124 private static final String EMAIL_CONFIGURATION_SEND_EMAILS = "Email configuration send emails";
125
126
127 private static final String EMAIL_SETTINGS = "Email settings:{}";
128
129
130 private static final Logger LOGGER = LoggerFactory.getLogger(EmailServiceImpl.class);
131
132
133 @Autowired
134 private ApplicationConfigurationService applicationConfigurationService;
135
136
137
138
139
140 public EmailServiceImpl() {
141 super();
142 }
143
144
145
146
147
148 @PostConstruct
149 public void initSettings() {
150 LOGGER.info(EMAIL_SETTINGS,applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SEND_EMAILS, SEND_EMAIL, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SEND_EMAIL, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SEND_EMAIL, "false"));
151 LOGGER.info(EMAIL_SETTINGS,applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_FROM_EMAIL, FROM_EMAIL, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SEND_EMAIL, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_FROM_EMAIL, "admin@hack23.com"));
152 LOGGER.info(EMAIL_SETTINGS,applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_HOST, SMTP_HOST, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_HOST, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_HOST, "localhost"));
153 LOGGER.info(EMAIL_SETTINGS,applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_PORT, SMTP_PORT, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_PORT, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_PORT, "587"));
154 LOGGER.info(EMAIL_SETTINGS,applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_USERNAME, SMTP_USERNAME, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_USERNAME, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_USERNAME, "username"));
155 LOGGER.info(EMAIL_SETTINGS,applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_SECRET, SMTP_SECRET, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_SECRET, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_SECRET, "password"));
156 LOGGER.info(EMAIL_SETTINGS,applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_AUTH, SMTP_AUTH, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_AUTH, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_AUTH, "true"));
157 LOGGER.info(EMAIL_SETTINGS,applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_STARTTLS_ENABLE, SMTP_STARTTLS_ENABLE, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_STARTTLS_ENABLE, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_STARTTLS_ENABLE, "true"));
158
159 }
160
161
162 @Override
163 public void sendEmail(final String toEmail,final String subject, final String content) {
164 final ApplicationConfiguration sendEmail = applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SEND_EMAILS, SEND_EMAIL, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SEND_EMAIL, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SEND_EMAIL, "false");
165 final ApplicationConfiguration fromEmail = applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_FROM_EMAIL, FROM_EMAIL, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SEND_EMAIL, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_FROM_EMAIL, "admin@hack23.com");
166
167 final JavaMailSender javaMailSender = getMailSender();
168
169 final SimpleMailMessage emailMessage = new SimpleMailMessage();
170 emailMessage.setFrom(fromEmail.getPropertyValue());
171 emailMessage.setTo(toEmail);
172 emailMessage.setText(content);
173 emailMessage.setSubject(subject);
174
175 if("true".equalsIgnoreCase(sendEmail.getPropertyValue())) {
176 LOGGER.info("Sending email:{}",emailMessage);
177 javaMailSender.send(emailMessage);
178 } else {
179 LOGGER.info("Email sending disabled, do not send email:{}",emailMessage);
180 }
181 }
182
183
184
185
186
187
188 private JavaMailSender getMailSender() {
189 final JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
190
191 final ApplicationConfiguration smtpHostConfig = applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_HOST, SMTP_HOST, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_HOST, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_HOST, "localhost");
192 final ApplicationConfiguration smtpPort = applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_PORT, SMTP_PORT, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_PORT, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_PORT, "587");
193 final ApplicationConfiguration smtpUsername = applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_USERNAME, SMTP_USERNAME, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_USERNAME, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_USERNAME, "username");
194 final ApplicationConfiguration smtpPassword = applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_SECRET, SMTP_SECRET, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_SECRET, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_SECRET, "password");
195 final ApplicationConfiguration smtpAuth = applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_AUTH, SMTP_AUTH, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_AUTH, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_AUTH, "true");
196 final ApplicationConfiguration smtpStartTlsEnable = applicationConfigurationService.checkValueOrLoadDefault(EMAIL_CONFIGURATION_SMTP_STARTTLS_ENABLE, SMTP_STARTTLS_ENABLE, ConfigurationGroup.EXTERNAL_SERVICES, EmailServiceImpl.class.getSimpleName(), SMTP_STARTTLS_ENABLE, RESPONSIBLE_FOR_SENDING_EMAIL, APPLICATION_EMAIL_SMTP_STARTTLS_ENABLE, "true");
197
198
199 javaMailSender.setHost(smtpHostConfig.getPropertyValue());
200 javaMailSender.setPort(Integer.parseInt(smtpPort.getPropertyValue()));
201 javaMailSender.setUsername(smtpUsername.getPropertyValue());
202 javaMailSender.setPassword(smtpPassword.getPropertyValue());
203
204 final Properties javaMailProperties = new Properties();
205
206 javaMailProperties.setProperty(MAIL_SMTP_AUTH, smtpAuth.getPropertyValue());
207 javaMailProperties.setProperty(MAIL_SMTP_STARTTLS_ENABLE, smtpStartTlsEnable.getPropertyValue());
208
209 javaMailSender.setJavaMailProperties(javaMailProperties);
210
211 return javaMailSender;
212 }
213
214 }