Search in sources :

Example 1 with Email

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);
    }
}
Also used : Email(com.manydesigns.mail.queue.model.Email) QueueException(com.manydesigns.mail.queue.QueueException) Recipient(com.manydesigns.mail.queue.model.Recipient)

Example 2 with Email

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);
    }
}
Also used : Email(com.manydesigns.mail.queue.model.Email) JAXBException(javax.xml.bind.JAXBException) Unmarshaller(javax.xml.bind.Unmarshaller) File(java.io.File) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException)

Example 3 with Email

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)");
}
Also used : org.apache.commons.mail(org.apache.commons.mail) Email(com.manydesigns.mail.queue.model.Email) Attachment(com.manydesigns.mail.queue.model.Attachment) Recipient(com.manydesigns.mail.queue.model.Recipient) FileDataSource(javax.activation.FileDataSource) File(java.io.File)

Example 4 with 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;
}
Also used : SendFailedException(javax.mail.SendFailedException) Email(com.manydesigns.mail.queue.model.Email) MessagingException(javax.mail.MessagingException) SMTPAddressFailedException(com.sun.mail.smtp.SMTPAddressFailedException) IllegalWriteException(javax.mail.IllegalWriteException) MethodNotSupportedException(javax.mail.MethodNotSupportedException) MailParseException(com.manydesigns.mail.queue.MailParseException) ParseException(javax.mail.internet.ParseException) MailParseException(com.manydesigns.mail.queue.MailParseException)

Aggregations

Email (com.manydesigns.mail.queue.model.Email)4 Recipient (com.manydesigns.mail.queue.model.Recipient)2 File (java.io.File)2 MailParseException (com.manydesigns.mail.queue.MailParseException)1 QueueException (com.manydesigns.mail.queue.QueueException)1 Attachment (com.manydesigns.mail.queue.model.Attachment)1 SMTPAddressFailedException (com.sun.mail.smtp.SMTPAddressFailedException)1 IOException (java.io.IOException)1 FileDataSource (javax.activation.FileDataSource)1 IllegalWriteException (javax.mail.IllegalWriteException)1 MessagingException (javax.mail.MessagingException)1 MethodNotSupportedException (javax.mail.MethodNotSupportedException)1 SendFailedException (javax.mail.SendFailedException)1 ParseException (javax.mail.internet.ParseException)1 JAXBException (javax.xml.bind.JAXBException)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 org.apache.commons.mail (org.apache.commons.mail)1