Search in sources :

Example 26 with EmailException

use of org.apache.commons.mail.EmailException in project AuthMeReloaded by AuthMe.

the class TestEmailSender method sendTestEmail.

private boolean sendTestEmail(String email) {
    HtmlEmail htmlEmail;
    try {
        htmlEmail = sendMailSsl.initializeMail(email);
    } catch (EmailException e) {
        ConsoleLogger.logException("Failed to create email for sample email:", e);
        return false;
    }
    htmlEmail.setSubject("AuthMe test email");
    String message = "Hello there!<br />This is a sample email sent to you from a Minecraft server (" + server.getName() + ") via /authme debug mail. If you're seeing this, sending emails should be fine.";
    return sendMailSsl.sendEmail(message, htmlEmail);
}
Also used : HtmlEmail(org.apache.commons.mail.HtmlEmail) EmailException(org.apache.commons.mail.EmailException)

Example 27 with EmailException

use of org.apache.commons.mail.EmailException in project sling by apache.

the class SimpleMailService method sendMail.

private MailResult sendMail(final String message, final String recipient, final Map data, final MailBuilder mailBuilder) {
    try {
        final Email email = mailBuilder.build(message, recipient, data);
        final String messageId = email.send();
        logger.info("mail '{}' sent", messageId);
        final byte[] bytes = MailUtil.toByteArray(email);
        return new MailResult(bytes);
    } catch (EmailException | MessagingException | IOException e) {
        throw new CompletionException(e);
    }
}
Also used : MailResult(org.apache.sling.commons.messaging.mail.MailResult) Email(org.apache.commons.mail.Email) MessagingException(javax.mail.MessagingException) CompletionException(java.util.concurrent.CompletionException) EmailException(org.apache.commons.mail.EmailException) IOException(java.io.IOException)

Example 28 with EmailException

use of org.apache.commons.mail.EmailException in project dal by ctripcorp.

the class GenTaskResource method taskApproveOperationForEmail.

@GET
@Path("taskApproveOperationForEmail")
@Produces(MediaType.APPLICATION_JSON)
public String taskApproveOperationForEmail(@Context HttpServletRequest request, @QueryParam("taskId") int taskId, @QueryParam("taskType") String taskType, @QueryParam("approveFlag") int approveFlag, @QueryParam("approveMsg") String approveMsg) {
    String userNo = RequestUtil.getUserNo(request);
    LoginUser user = SpringBeanGetter.getDaoOfLoginUser().getUserByNo(userNo);
    if (user == null) {
        return "please login fisrt.";
    }
    ApproveTask task = haveApprovePermision(user.getId(), taskId, taskType);
    if (task == null) {
        return "Dao have been approved.";
    }
    List<GenTaskByTableViewSp> tableViewSpTasks = new ArrayList<>();
    List<GenTaskBySqlBuilder> autoTasks = new ArrayList<>();
    List<GenTaskByFreeSql> sqlTasks = new ArrayList<>();
    if ("table_view_sp".equalsIgnoreCase(taskType)) {
        SpringBeanGetter.getDaoByTableViewSp().updateTask(taskId, approveFlag, approveMsg);
        SpringBeanGetter.getApproveTaskDao().deleteApproveTaskByTaskIdAndType(taskId, taskType);
        tableViewSpTasks.add(SpringBeanGetter.getDaoByTableViewSp().getTasksByTaskId(taskId));
    } else if ("auto".equalsIgnoreCase(taskType)) {
        SpringBeanGetter.getDaoBySqlBuilder().updateTask(taskId, approveFlag, approveMsg);
        SpringBeanGetter.getApproveTaskDao().deleteApproveTaskByTaskIdAndType(taskId, taskType);
        autoTasks.add(SpringBeanGetter.getDaoBySqlBuilder().getTasksByTaskId(taskId));
    } else if ("sql".equalsIgnoreCase(taskType)) {
        SpringBeanGetter.getDaoByFreeSql().updateTask(taskId, approveFlag, approveMsg);
        SpringBeanGetter.getApproveTaskDao().deleteApproveTaskByTaskIdAndType(taskId, taskType);
        sqlTasks.add(SpringBeanGetter.getDaoByFreeSql().getTasksByTaskId(taskId));
    }
    java.util.Collections.sort(tableViewSpTasks);
    java.util.Collections.sort(autoTasks);
    java.util.Collections.sort(sqlTasks);
    LoginUser noticeUsr = SpringBeanGetter.getDaoOfLoginUser().getUserById(task.getCreate_user_id());
    VelocityContext context = GenUtils.buildDefaultVelocityContext();
    context.put("standardDao", tableViewSpTasks);
    context.put("autoDao", autoTasks);
    context.put("sqlDao", sqlTasks);
    String msg = "你好," + noticeUsr.getUserName() + ":<br/>&nbsp;&nbsp;你提交的DAO已审批,审批";
    if (approveFlag == 2) {
        msg += "通过。";
    } else {
        msg += "未通过。";
    }
    if (approveMsg != null) {
        msg += "<br/>&nbsp;&nbsp;审批意见:" + approveMsg;
    }
    context.put("msg", msg);
    String mailMsg = GenUtils.mergeVelocityContext(context, "templates/approval/approveResult.tpl");
    HtmlEmail email = new HtmlEmail();
    email.setHostName(Configuration.get("email_host_name"));
    email.setAuthentication(Configuration.get("email_user_name"), Configuration.get("email_password"));
    try {
        email.addTo(noticeUsr.getUserEmail());
        email.setFrom(user.getUserEmail(), user.getUserName());
        email.setSubject("Codegen DAO 审批结果通知");
        email.setHtmlMsg(mailMsg);
        email.send();
    } catch (EmailException e) {
        e.printStackTrace();
    }
    return "Success Approved.";
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) HtmlEmail(org.apache.commons.mail.HtmlEmail) EmailException(org.apache.commons.mail.EmailException)

Example 29 with EmailException

use of org.apache.commons.mail.EmailException in project dhis2-core by dhis2.

the class EmailMessageSender method sendMessage.

@Override
public OutboundMessageResponse sendMessage(String subject, String text, Set<String> recipients) {
    EmailConfiguration emailConfig = getEmailConfiguration();
    String errorMessage = "No recipient found";
    OutboundMessageResponse status = new OutboundMessageResponse();
    if (emailConfig.getHostName() == null) {
        status.setOk(false);
        status.setResponseObject(EmailResponse.NOT_CONFIGURED);
        return status;
    }
    try {
        HtmlEmail email = getHtmlEmail(emailConfig.getHostName(), emailConfig.getPort(), emailConfig.getUsername(), emailConfig.getPassword(), emailConfig.isTls(), emailConfig.getFrom());
        email.setSubject(customizeTitle(DEFAULT_SUBJECT_PREFIX) + subject);
        email.setTextMsg(text);
        boolean hasRecipients = false;
        for (String recipient : recipients) {
            if (isEmailValid(recipient)) {
                email.addBcc(recipient);
                hasRecipients = true;
                log.info("Sending email to : " + recipient + " to host: " + emailConfig.getHostName() + ":" + emailConfig.getPort());
            } else {
                log.error(recipient + " is not a valid email");
                errorMessage = "No valid email address found";
            }
        }
        if (hasRecipients) {
            email.send();
            log.info("Email sent using host: " + emailConfig.getHostName() + ":" + emailConfig.getPort() + " with TLS: " + emailConfig.isTls());
            return new OutboundMessageResponse("Email sent", EmailResponse.SENT, true);
        } else {
            status = new OutboundMessageResponse(errorMessage, EmailResponse.ABORTED, false);
        }
    } catch (EmailException ex) {
        log.warn("Error while sending email: " + ex.getMessage() + ", " + DebugUtils.getStackTrace(ex));
        status = new OutboundMessageResponse("Email not sent: " + ex.getMessage(), EmailResponse.FAILED, false);
    } catch (RuntimeException ex) {
        log.warn("Error while sending email: " + ex.getMessage() + ", " + DebugUtils.getStackTrace(ex));
        status = new OutboundMessageResponse("Email not sent: " + ex.getMessage(), EmailResponse.FAILED, false);
    }
    return status;
}
Also used : EmailConfiguration(org.hisp.dhis.email.EmailConfiguration) HtmlEmail(org.apache.commons.mail.HtmlEmail) EmailException(org.apache.commons.mail.EmailException) OutboundMessageResponse(org.hisp.dhis.outboundmessage.OutboundMessageResponse)

Aggregations

EmailException (org.apache.commons.mail.EmailException)29 HtmlEmail (org.apache.commons.mail.HtmlEmail)18 Email (org.apache.commons.mail.Email)7 MultiPartEmail (org.apache.commons.mail.MultiPartEmail)7 EmailAttachment (org.apache.commons.mail.EmailAttachment)5 ExecutionException (java.util.concurrent.ExecutionException)4 SimpleEmail (org.apache.commons.mail.SimpleEmail)4 RenderResult (cn.bran.japid.template.RenderResult)3 IOException (java.io.IOException)3 MalformedURLException (java.net.MalformedURLException)3 URL (java.net.URL)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 InternetAddress (javax.mail.internet.InternetAddress)3 ImageHtmlEmail (org.apache.commons.mail.ImageHtmlEmail)3 VelocityContext (org.apache.velocity.VelocityContext)3 MailException (cn.bran.play.exceptions.MailException)2 Status (com.ctrip.platform.dal.daogen.domain.Status)2