Files
wisemapping-open-source/wise-api/src/main/java/com/wisemapping/service/MailerService.java

84 lines
3.0 KiB
Java
Raw Normal View History

2009-06-07 18:59:43 +00:00
/*
2022-03-17 18:47:34 -03:00
* Copyright [2022] [wisemapping]
2020-11-07 11:56:38 -08:00
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2009-06-07 18:59:43 +00:00
package com.wisemapping.service;
2009-06-07 18:59:43 +00:00
2020-11-07 11:56:38 -08:00
import com.wisemapping.util.VelocityEngineUtils;
import com.wisemapping.util.VelocityEngineWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
2009-06-07 18:59:43 +00:00
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
2023-07-28 22:46:38 -07:00
import jakarta.validation.constraints.NotNull;
import org.springframework.stereotype.Service;
2022-03-30 07:50:42 -03:00
import java.nio.charset.StandardCharsets;
2009-06-07 18:59:43 +00:00
import java.util.Map;
@Service
public final class MailerService {
2009-06-07 18:59:43 +00:00
//~ Instance fields ......................................................................................
2024-02-06 23:04:25 -08:00
@Autowired
2009-06-07 18:59:43 +00:00
private JavaMailSender mailSender;
@Autowired
2020-11-07 11:56:38 -08:00
private VelocityEngineWrapper velocityEngineWrapper;
2009-06-07 18:59:43 +00:00
2024-02-19 00:28:05 -08:00
@Value("${app.mail.sender-email}")
private String serverFromEmail;
2009-06-07 18:59:43 +00:00
2024-02-17 11:18:43 -08:00
@Value("${app.mail.enabled:true}")
private boolean isEnabled;
2024-02-19 00:28:05 -08:00
@Value("${app.mail.support-email}")
private String supportEmail;
//~ Methods ..............................................................................................
2009-06-07 18:59:43 +00:00
public String getServerSenderEmail() {
return serverFromEmail;
2009-06-07 18:59:43 +00:00
}
2022-02-13 08:25:09 -08:00
public void sendEmail(final String from, final String to, final String subject, final Map<String, Object> model,
2012-06-16 11:37:40 -03:00
@NotNull final String templateMail) {
2024-02-17 11:18:43 -08:00
if (isEnabled) {
final MimeMessagePreparator preparator =
mimeMessage -> {
final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, StandardCharsets.UTF_8.name());
message.setTo(to);
message.setFrom(from);
message.setSubject(subject);
final String messageBody = VelocityEngineUtils.mergeTemplateIntoString(velocityEngineWrapper.getVelocityEngine(), "/mail/" + templateMail, model);
message.setText(messageBody, true);
};
this.mailSender.send(preparator);
}
2009-06-07 18:59:43 +00:00
}
public String getSupportEmail() {
return supportEmail;
}
2009-06-07 18:59:43 +00:00
}