89 lines
3.3 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.mail;
2020-11-07 11:56:38 -08:00
import com.wisemapping.util.VelocityEngineUtils;
import com.wisemapping.util.VelocityEngineWrapper;
import org.jetbrains.annotations.NotNull;
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;
import javax.mail.internet.MimeMessage;
import java.util.Map;
public final class Mailer {
//~ Instance fields ......................................................................................
private JavaMailSender mailSender;
2020-11-07 11:56:38 -08:00
private VelocityEngineWrapper velocityEngineWrapper;
private final String serverFromEmail;
private final String supportEmail;
private final String errorReporterEmail;
2009-06-07 18:59:43 +00:00
//~ Methods ..............................................................................................
2022-02-13 08:25:09 -08:00
public Mailer(@NotNull String siteEmail, @NotNull String supportEmail, @NotNull String errorReporterEmail) {
this.serverFromEmail = siteEmail;
this.supportEmail = supportEmail;
this.errorReporterEmail = errorReporterEmail;
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) {
2015-11-02 08:57:12 -03:00
final MimeMessagePreparator preparator =
new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage)
throws Exception {
final MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
message.setTo(to);
message.setFrom(from);
message.setSubject(subject);
2020-11-07 11:56:38 -08:00
final String messageBody = VelocityEngineUtils.mergeTemplateIntoString(velocityEngineWrapper.getVelocityEngine(), "/mail/" + templateMail, model);
2015-11-02 08:57:12 -03:00
message.setText(messageBody, true);
}
};
this.mailSender.send(preparator);
2009-06-07 18:59:43 +00:00
}
public void setMailSender(JavaMailSender mailer) {
this.mailSender = mailer;
}
2020-11-07 11:56:38 -08:00
public void setVelocityEngineWrapper(VelocityEngineWrapper engine) {
this.velocityEngineWrapper = engine;
2009-06-07 18:59:43 +00:00
}
public String getSupportEmail() {
return supportEmail;
}
public String getErrorReporterEmail() {
return errorReporterEmail;
}
2009-06-07 18:59:43 +00:00
}