/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package es.devel.mail.impl;
import es.devel.mail.MailService;
import es.devel.mail.exception.MailServiceException;
import es.devel.mail.model.MailMessage;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
public class MailServiceImpl implements MailService, Serializable {
public static final String SUPPORT_EMAIL_ADDRESS = "account@mydomain.com";
public static final String FROM_ADDRESS_DEFAULT_VALUE = "default@mydomain.com";
private JavaMailSenderImpl sender;
private SimpleMailMessage defaultMessage;
public MailServiceImpl() {
}
public MailServiceImpl(JavaMailSenderImpl sender) {
this.sender = sender;
}
@Override
public void sendMail(String[] messageToAddresses, String sender, String senderName, String messageSubject, String messageBody) throws MailServiceException {
sendMail(messageToAddresses, sender, senderName, messageSubject, messageBody, null);
}
@Override
public void sendMail(Throwable cause) throws MailServiceException {
if (cause == null) {
throw new MailServiceException("La excepcion a enviar por correo es nula: " + cause);
}
StringBuilder html = new StringBuilder(cause.toString());
html.append("<p>");
html.append(System.getProperties().toString());
html.append("</p>");
sendMail(new String[]{SUPPORT_EMAIL_ADDRESS}, FROM_ADDRESS_DEFAULT_VALUE, "Sistema de notificacion de errores de devel", "Ha ocurrido una excepcion generica:", html.toString());
}
public void sendMail(MailMessage mailMessageArg) throws MailServiceException {
if (mailMessageArg.getMessageToAddresses() == null || mailMessageArg.getMessageToAddresses().isEmpty()) {
throw new MailServiceException("No se han especificado destinatarios");
}
String[] recipients = mailMessageArg.getMessageToAddresses().toArray(new String[mailMessageArg.getMessageToAddresses().size()]);
try {
sendMail(recipients, mailMessageArg.getSenderEmailAddress(), mailMessageArg.getSenderName(), mailMessageArg.getSubject(), mailMessageArg.getMessageText(), mailMessageArg.getAttachments());
} catch (MailServiceException ex) {
throw new MailServiceException(ex);
}
}
@Override
public void sendMail(String[] messageToAddresses, String sender, String senderName, String messageSubject, String messageBody, Collection<File> attachMents) throws MailServiceException {
if (messageToAddresses == null || messageToAddresses.length == 0) {
throw new MailServiceException("No se han especificado destinatarios");
}
if (sender == null || sender.equals("")) {
throw new MailServiceException("No se ha especificado direccion de respuesta");
}
if (senderName == null || senderName.equals("")) {
throw new MailServiceException("No se ha especificado nombre de remitente");
}
MimeMessage message = this.sender.createMimeMessage();
MimeMessageHelper helper;
try {
//INDICAMOS QUE EL MENSAJE ES UN MENSAJE MULTI-PARTE
helper = new MimeMessageHelper(message, true);
} catch (MessagingException e2) {
throw new MailServiceException(e2);
}
try {
helper.setTo(messageToAddresses);
helper.setSubject(messageSubject);
helper.setText(messageBody, true);//INDICAMOS QUE EL CUERPO DEL MENSAJE VA EN HTML
helper.setSentDate(new Date());
helper.setFrom(sender);
helper.setReplyTo(sender);
if (attachMents != null && attachMents.size() > 0) {
for (File item : attachMents) {
helper.addAttachment(item.getName(), item);
}
}
} catch (Exception ex) {
throw new MailServiceException("Ha habido problemas con la pasarela de correo: " + ex.toString(), ex);
}
this.sender.send(message);
}
public JavaMailSenderImpl getSender() {
return sender;
}
public void setSender(JavaMailSenderImpl sender) {
this.sender = sender;
}
public SimpleMailMessage getDefaultMessage() {
return defaultMessage;
}
public void setDefaultMessage(SimpleMailMessage defaultMessage) {
this.defaultMessage = defaultMessage;
}
}