use of com.manydesigns.mail.queue.model.Email in project Portofino by ManyDesigns.
the class DefaultLoginAction method sendMail.
protected void sendMail(String from, String to, String subject, String body) {
if (mailQueue == null) {
throw new UnsupportedOperationException("Mail queue is not enabled");
}
Email email = new Email();
email.getRecipients().add(new Recipient(Recipient.Type.TO, to));
email.setFrom(from);
email.setSubject(subject);
email.setHtmlBody(body);
email.setTextBody(Jsoup.parse(body).text().toString());
try {
mailQueue.enqueue(email);
} catch (QueueException e) {
throw new RuntimeException(e);
}
}
use of com.manydesigns.mail.queue.model.Email in project Portofino by ManyDesigns.
the class FileSystemMailQueue method loadEmail.
public Email loadEmail(String id) throws QueueException {
try {
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
File emailFile = getEmailFile(id);
if (emailFile.exists()) {
logger.debug("Found email with id: {}", id);
Email email = (Email) unmarshaller.unmarshal(emailFile);
return email;
} else {
logger.debug("Email with id {} not found", id);
return null;
}
} catch (JAXBException e) {
throw new MailParseException("Couldn't parse email", e);
} catch (Exception e) {
throw new QueueException("Couldn't load email", e);
}
}
use of com.manydesigns.mail.queue.model.Email in project Portofino by ManyDesigns.
the class DefaultMailSender method send.
protected void send(Email emailBean) throws EmailException {
logger.debug("Entering send(Email)");
org.apache.commons.mail.Email email;
String textBody = emailBean.getTextBody();
String htmlBody = emailBean.getHtmlBody();
if (null == htmlBody) {
if (emailBean.getAttachments().isEmpty()) {
SimpleEmail simpleEmail = new SimpleEmail();
simpleEmail.setMsg(textBody);
email = simpleEmail;
} else {
MultiPartEmail multiPartEmail = new MultiPartEmail();
multiPartEmail.setMsg(textBody);
for (Attachment attachment : emailBean.getAttachments()) {
EmailAttachment emailAttachment = new EmailAttachment();
emailAttachment.setName(attachment.getName());
emailAttachment.setDisposition(attachment.getDisposition());
emailAttachment.setDescription(attachment.getDescription());
emailAttachment.setPath(attachment.getFilePath());
multiPartEmail.attach(emailAttachment);
}
email = multiPartEmail;
}
} else {
HtmlEmail htmlEmail = new HtmlEmail();
htmlEmail.setHtmlMsg(htmlBody);
if (textBody != null) {
htmlEmail.setTextMsg(textBody);
}
for (Attachment attachment : emailBean.getAttachments()) {
if (!attachment.isEmbedded()) {
EmailAttachment emailAttachment = new EmailAttachment();
emailAttachment.setName(attachment.getName());
emailAttachment.setDisposition(attachment.getDisposition());
emailAttachment.setDescription(attachment.getDescription());
emailAttachment.setPath(attachment.getFilePath());
htmlEmail.attach(emailAttachment);
} else {
FileDataSource dataSource = new FileDataSource(new File(attachment.getFilePath()));
htmlEmail.embed(dataSource, attachment.getName(), attachment.getContentId());
}
}
email = htmlEmail;
}
if (null != login && null != password) {
email.setAuthenticator(new DefaultAuthenticator(login, password));
}
email.setHostName(server);
email.setSmtpPort(port);
email.setSubject(emailBean.getSubject());
email.setFrom(emailBean.getFrom());
String replyTo = emailBean.getReplyTo();
if (null != replyTo)
email.addReplyTo(replyTo);
for (Recipient recipient : emailBean.getRecipients()) {
switch(recipient.getType()) {
case TO:
email.addTo(recipient.getAddress());
break;
case CC:
email.addCc(recipient.getAddress());
break;
case BCC:
email.addBcc(recipient.getAddress());
break;
}
}
email.setSSLOnConnect(ssl);
email.setStartTLSEnabled(tls);
email.setSslSmtpPort(port + "");
email.setCharset("UTF-8");
email.send();
logger.debug("Exiting send(Email)");
}
use of com.manydesigns.mail.queue.model.Email in project Portofino by ManyDesigns.
the class DefaultMailSender method runOnce.
public int runOnce(Set<String> idsToMarkAsSent) {
List<String> ids;
try {
ids = queue.getEnqueuedEmailIds();
} catch (Throwable e) {
logger.error("Couldn't read email queue", e);
return -1;
}
int serverErrors = 0;
for (String id : ids) {
if (idsToMarkAsSent.contains(id)) {
logger.info("Mail with id {} already sent but mark failed, retrying", id);
try {
queue.markSent(id);
idsToMarkAsSent.remove(id);
} catch (Throwable e) {
logger.error("Couldn't mark mail as sent", e);
}
continue;
}
Email email;
try {
email = queue.loadEmail(id);
} catch (MailParseException e) {
logger.error("Mail with id " + id + " is corrupted, marking as failed", e);
markFailed(id, e);
continue;
} catch (Throwable e) {
logger.error("Unexpected error loading mail with id " + id + ", skipping", e);
continue;
}
if (email != null) {
boolean sent = false;
try {
logger.info("Sending email with id {}", id);
send(email);
sent = true;
} catch (EmailException e) {
Throwable cause = e.getCause();
if (cause instanceof ParseException || cause instanceof IllegalWriteException || cause instanceof MethodNotSupportedException) {
markFailed(id, cause);
} else if (cause instanceof MessagingException) {
if (e.getCause() instanceof SendFailedException && e.getCause().getCause() instanceof SMTPAddressFailedException) {
logger.warn("Mail not sent due to known server error, marking as failed");
markFailed(id, e);
} else {
logger.warn("Mail not sent due to known server error, NOT marking as failed", e);
serverErrors++;
}
} else {
markFailed(id, e);
}
} catch (Throwable e) {
markFailed(id, e);
}
if (sent)
try {
logger.info("Email with id {} sent, marking as sent ", id);
queue.markSent(id);
} catch (Throwable e) {
logger.error("Couldn't mark mail as sent", e);
idsToMarkAsSent.add(id);
}
}
}
return serverErrors;
}
Aggregations