public void send(MessageContent messageContent, List<Map<String, String>> personParameters
, boolean haltOnFailure)
throws QwertoMailerException, Exception
{
if(messageContent == null)
throw new QwertoMailerException("Message is null");
String charset = messageContent.getCharset();
Address from = messageContent.getAddressFrom();
if(from == null)
throw new QwertoMailerException("From email has not been specified");
if(personParameters == null || personParameters.isEmpty())
{
throw new QwertoMailerException("Recipients list is empty");
}
mailProp.put("mail.mime.charset", charset);
Velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
"org.apache.velocity.slf4j.Slf4jLogChute");
Velocity.setProperty("runtime.log.logsystem.slf4j.name",
"com.qwertovsky.mailer");
Velocity.init();
badEmails = new ArrayList<String>();
badParametersMap = new ArrayList<Map<String, String>>();
//create messages
ArrayList<Message> messages = new ArrayList<Message>();
logger.info("Create personal messages");
for(Map<String, String> parameters:personParameters)
{
//get emails
Set<InternetAddress> recipients = getRecipientsList(parameters);
//error if list is empty
if(recipients == null)
{
StringBuilder sb = new StringBuilder();
Set<String> keys = parameters.keySet();
for(String key:keys)
{
if(sb.length() > 0)
sb.append(", ");
sb.append("\"" + parameters.get(key) + "\"");
}
logger.error("Recipients list is empty: " + sb.toString());
continue;
}
//get attachments
List<File> attachments = getAttachments(parameters);
//create individual message content
MessageContent content = new MessageContent(messageContent);
if(attachments != null && !attachments.isEmpty())
content.addAttachments(attachments);
try
{
content.setParameters(parameters);
} catch (QwertoMailerException qme)
{
String errorMessage = "Message has not been created (" + qme.getMessage() + ") for: ";
StringBuilder sb = new StringBuilder();
Set<String> headers = parameters.keySet();
for(String header:headers)
{
if(sb.length() > 0)
sb.append(", ");
sb.append("\"" + parameters.get(header) + "\"");
}
logger.error(errorMessage + sb.toString());
badParametersMap.add(parameters);
continue;
}
Message message = new Message(session);
try
{
makeMessage(message, content);
message.setRecipients(RecipientType.TO, recipients.toArray(new InternetAddress[0]));
} catch (MessagingException e)
{
logger.error("Message has not been created for "
+ recipients.toArray() + "("+e.getMessage()+")");
throw e;
}
message.setParameters(parameters);
messages.add(message);
}
//halt on failure
if(haltOnFailure && (!badEmails.isEmpty() || !badParametersMap.isEmpty()))
throw new QwertoMailerException("Halt on failure");
//send messages
logger.info("Start sending");
notSentMessages = new ArrayList<Message>();
sentMessages = new ArrayList<Message>();