Search in sources :

Example 1 with FeatureNotAvailable

use of io.kamax.mxisd.exception.FeatureNotAvailable in project mxisd by kamax-io.

the class EmailSendGridNotificationHandler method send.

private void send(String recipient, Email email) {
    if (StringUtils.isBlank(cfg.getIdentity().getFrom())) {
        throw new FeatureNotAvailable("3PID Email identity: sender address is empty - " + "You must set a value for notifications to work");
    }
    try {
        email.addTo(recipient);
        email.setFrom(cfg.getIdentity().getFrom());
        email.setFromName(cfg.getIdentity().getName());
        Response response = sendgrid.send(email);
        if (response.getStatus()) {
            log.info("Successfully sent email to {} using SendGrid", recipient);
        } else {
            throw new RuntimeException("Error sending via SendGrid to " + recipient + ": " + response.getMessage());
        }
    } catch (SendGridException e) {
        throw new RuntimeException("Unable to send e-mail invite via SendGrid to " + recipient, e);
    }
}
Also used : FeatureNotAvailable(io.kamax.mxisd.exception.FeatureNotAvailable) Response(com.sendgrid.SendGrid.Response) SendGridException(com.sendgrid.SendGridException)

Example 2 with FeatureNotAvailable

use of io.kamax.mxisd.exception.FeatureNotAvailable in project mxisd by kamax-io.

the class EmailSmtpConnector method send.

@Override
public void send(String senderAddress, String senderName, String recipient, String content) {
    if (StringUtils.isBlank(senderAddress)) {
        throw new FeatureNotAvailable("3PID Email identity: sender address is empty - " + "You must set a value for notifications to work");
    }
    if (StringUtils.isBlank(content)) {
        throw new InternalServerError("Notification content is empty");
    }
    try {
        InternetAddress sender = new InternetAddress(senderAddress, senderName);
        MimeMessage msg = new MimeMessage(session, IOUtils.toInputStream(content, StandardCharsets.UTF_8));
        // FIXME set version
        msg.setHeader("X-Mailer", "mxisd");
        msg.setSentDate(new Date());
        msg.setFrom(sender);
        msg.setRecipients(Message.RecipientType.TO, recipient);
        msg.saveChanges();
        log.info("Sending invite to {} via SMTP using {}:{}", recipient, cfg.getHost(), cfg.getPort());
        SMTPTransport transport = (SMTPTransport) session.getTransport("smtp");
        transport.setStartTLS(cfg.getTls() > 0);
        transport.setRequireStartTLS(cfg.getTls() > 1);
        log.info("Connecting to {}:{}", cfg.getHost(), cfg.getPort());
        transport.connect(cfg.getHost(), cfg.getPort(), cfg.getLogin(), cfg.getPassword());
        try {
            transport.sendMessage(msg, InternetAddress.parse(recipient));
            log.info("Invite to {} was sent", recipient);
        } finally {
            transport.close();
        }
    } catch (UnsupportedEncodingException | MessagingException e) {
        throw new RuntimeException("Unable to send e-mail invite to " + recipient, e);
    }
}
Also used : FeatureNotAvailable(io.kamax.mxisd.exception.FeatureNotAvailable) InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InternalServerError(io.kamax.mxisd.exception.InternalServerError) Date(java.util.Date) SMTPTransport(com.sun.mail.smtp.SMTPTransport)

Aggregations

FeatureNotAvailable (io.kamax.mxisd.exception.FeatureNotAvailable)2 Response (com.sendgrid.SendGrid.Response)1 SendGridException (com.sendgrid.SendGridException)1 SMTPTransport (com.sun.mail.smtp.SMTPTransport)1 InternalServerError (io.kamax.mxisd.exception.InternalServerError)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Date (java.util.Date)1 MessagingException (javax.mail.MessagingException)1 InternetAddress (javax.mail.internet.InternetAddress)1 MimeMessage (javax.mail.internet.MimeMessage)1