Search in sources :

Example 1 with MailException

use of org.springframework.mail.MailException in project OpenClinica by OpenClinica.

the class QueryServiceImpl method sendEmail.

private void sendEmail(String to, String subject, String body, Boolean htmlEmail) throws Exception {
    try {
        JavaMailSenderImpl mailSender = (JavaMailSenderImpl) appContext.getBean("mailSender");
        Properties javaMailProperties = mailSender.getJavaMailProperties();
        if (null != javaMailProperties) {
            if (javaMailProperties.get("mail.smtp.localhost") == null || ((String) javaMailProperties.get("mail.smtp.localhost")).equalsIgnoreCase("")) {
                javaMailProperties.put("mail.smtp.localhost", "localhost");
            }
        }
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, htmlEmail);
        helper.setFrom(EmailEngine.getAdminEmail());
        helper.setTo(processMultipleImailAddresses(to.trim()));
        helper.setSubject(subject);
        helper.setText(body, true);
        mailSender.send(mimeMessage);
        logger.debug("Email sent successfully on {}", new Date());
    } catch (MailException me) {
        logger.error("Email could not be sent on {} due to: {}", new Date(), me.getMessage());
    }
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) MimeMessage(javax.mail.internet.MimeMessage) MailException(org.springframework.mail.MailException) Properties(java.util.Properties) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) Date(java.util.Date)

Example 2 with MailException

use of org.springframework.mail.MailException in project webofneeds by researchstudio-sat.

the class WonMailSender method sendTextMessage.

public void sendTextMessage(String toEmail, String subject, String text) {
    SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage);
    msg.setSubject(subject);
    msg.setTo(toEmail);
    msg.setText(text);
    try {
        mailSender.send(msg);
    } catch (MailException ex) {
        logger.warn(ex.getMessage());
    }
}
Also used : SimpleMailMessage(org.springframework.mail.SimpleMailMessage) MailException(org.springframework.mail.MailException)

Example 3 with MailException

use of org.springframework.mail.MailException in project coprhd-controller by CoprHD.

the class MailHelper method sendMailMessage.

public void sendMailMessage(String to, String subject, String html) {
    try {
        JavaMailSender mailSender = getMailSender();
        if (mailSender != null) {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, false, "utf-8");
            helper.setText(html, true);
            String[] addresses = StringUtils.split(to, ",");
            for (String address : addresses) {
                address = StringUtils.trimToNull(address);
                if (address != null) {
                    helper.addTo(address);
                }
            }
            helper.setSubject(subject);
            mailSender.send(mimeMessage);
        } else {
            log.warn("Unable to send notification email.  Email settings not configured.");
        }
    } catch (MailException | MessagingException ex) {
        String message = String.format("Failed to notify user by email");
        log.error(message, ex);
        throw APIException.internalServerErrors.genericApisvcError(message, ex);
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) MailException(org.springframework.mail.MailException) JavaMailSender(org.springframework.mail.javamail.JavaMailSender) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 4 with MailException

use of org.springframework.mail.MailException in project ocvn by devgateway.

the class SendEmailService method sendEmailResetPassword.

/**
     * Send a reset password email. This is UNSAFE because passwords are sent in clear text.
     * Nevertheless some customers will ask for these emails to be sent, so ...
     * @param person
     * @param newPassword
     */
public void sendEmailResetPassword(final Person person, final String newPassword) {
    SimpleMailMessage msg = new SimpleMailMessage();
    msg.setTo(person.getEmail());
    msg.setFrom("support@developmentgateway.org");
    msg.setSubject("Recover your password");
    msg.setText("Dear " + person.getFirstName() + " " + person.getLastName() + ",\n\n" + "These are your new login credentials for E-Procurement Toolkit.\n\n" + "Username: " + person.getUsername() + "\n" + "Password: " + newPassword + "\n\n" + "At login, you will be prompted to change your password to one of your choice.\n\n" + "Thank you,\n" + "DG Team");
    try {
        javaMailSenderImpl.send(msg);
    } catch (MailException e) {
        e.printStackTrace();
    }
}
Also used : SimpleMailMessage(org.springframework.mail.SimpleMailMessage) MailException(org.springframework.mail.MailException)

Example 5 with MailException

use of org.springframework.mail.MailException in project judge by zjnu-acm.

the class ResetPasswordController method doPost.

@PostMapping("/resetPassword")
public void doPost(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "action", required = false) String action, @RequestParam(value = "verify", required = false) String verify, @RequestParam(value = "username", required = false) String username, Locale locale) throws IOException {
    response.setContentType("text/javascript;charset=UTF-8");
    PrintWriter out = response.getWriter();
    HttpSession session = request.getSession(false);
    String word = null;
    if (session != null) {
        word = (String) session.getAttribute("word");
        session.removeAttribute("word");
    }
    if (word == null || !word.equalsIgnoreCase(verify)) {
        out.print("alert('验证码错误');");
        return;
    }
    User user = userMapper.findOne(username);
    if (user == null) {
        out.print("alert('用户不存在');");
        return;
    }
    String email = user.getEmail();
    if (email == null || !email.toLowerCase().matches(ValueCheck.EMAIL_PATTERN)) {
        out.print("alert('该用户未设置邮箱或邮箱格式不对,如需重置密码,请联系管理员!');");
        return;
    }
    try {
        String vc = resetPasswordService.getOrCreate(user.getId());
        String url = getPath(request, "/resetPassword.html?vc=", vc + "&u=", user.getId());
        String title = systemService.getResetPasswordTitle();
        Map<String, Object> map = ImmutableMap.of("url", url, "title", Objects.toString(title, ""));
        String content = templateEngine.process("users/password", new Context(locale, map));
        emailService.send(email, title, content);
    } catch (MailException | MessagingException ex) {
        log.error("fail to send email", ex);
        out.print("alert('邮件发送失败,请稍后再试')");
        return;
    }
    out.print("alert('已经将邮件发送到" + user.getEmail() + ",请点击链接重设密码');");
}
Also used : Context(org.thymeleaf.context.Context) User(cn.edu.zjnu.acm.judge.domain.User) MessagingException(javax.mail.MessagingException) HttpSession(javax.servlet.http.HttpSession) MailException(org.springframework.mail.MailException) PrintWriter(java.io.PrintWriter) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Aggregations

MailException (org.springframework.mail.MailException)45 MimeMessage (javax.mail.internet.MimeMessage)18 SimpleMailMessage (org.springframework.mail.SimpleMailMessage)17 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)14 MessagingException (javax.mail.MessagingException)10 Date (java.util.Date)8 Properties (java.util.Properties)3 InternetAddress (javax.mail.internet.InternetAddress)3 OpenClinicaSystemException (org.akaza.openclinica.exception.OpenClinicaSystemException)3 JavaMailSender (org.springframework.mail.javamail.JavaMailSender)3 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)2 AuthenticationFailedException (jakarta.mail.AuthenticationFailedException)2 MessagingException (jakarta.mail.MessagingException)2 NoSuchProviderException (jakarta.mail.NoSuchProviderException)2 MimeMessage (jakarta.mail.internet.MimeMessage)2 PrintWriter (java.io.PrintWriter)2 Matcher (java.util.regex.Matcher)2 DataSource (javax.activation.DataSource)2 FileDataSource (javax.activation.FileDataSource)2 MolgenisDataException (org.molgenis.data.MolgenisDataException)2