Search in sources :

Example 26 with MailException

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

the class ImportRunService method createAndSendStatusMail.

private void createAndSendStatusMail(ImportRun importRun) {
    try {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setTo(userService.getUser(importRun.getUsername()).getEmail());
        mailMessage.setSubject(createMailTitle(importRun));
        mailMessage.setText(createEnglishMailText(importRun, ZoneId.systemDefault()));
        mailSender.send(mailMessage);
    } catch (MailException mce) {
        LOG.error("Could not send import status mail", mce);
        throw new MolgenisDataException("An error occurred. Please contact the administrator.");
    }
}
Also used : MolgenisDataException(org.molgenis.data.MolgenisDataException) SimpleMailMessage(org.springframework.mail.SimpleMailMessage) MailException(org.springframework.mail.MailException)

Example 27 with MailException

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

the class AccountServiceImpl method createUser.

@Override
@RunAsSystem
@Transactional
public void createUser(User user, String baseActivationUri) throws UsernameAlreadyExistsException, EmailAlreadyExistsException {
    // Check if username already exists
    if (userService.getUser(user.getUsername()) != null) {
        throw new UsernameAlreadyExistsException("Username '" + user.getUsername() + "' already exists.");
    }
    // Check if email already exists
    if (userService.getUserByEmail(user.getEmail()) != null) {
        throw new EmailAlreadyExistsException("Email '" + user.getEmail() + "' is already registered.");
    }
    // collect activation info
    String activationCode = idGenerator.generateId(SECURE_RANDOM);
    List<String> activationEmailAddresses;
    if (authenticationSettings.getSignUpModeration()) {
        activationEmailAddresses = userService.getSuEmailAddresses();
        if (activationEmailAddresses == null || activationEmailAddresses.isEmpty())
            throw new MolgenisDataException("Administrator account is missing required email address");
    } else {
        String activationEmailAddress = user.getEmail();
        if (activationEmailAddress == null || activationEmailAddress.isEmpty())
            throw new MolgenisDataException("User '" + user.getUsername() + "' is missing required email address");
        activationEmailAddresses = asList(activationEmailAddress);
    }
    // create user
    user.setActivationCode(activationCode);
    user.setActive(false);
    dataService.add(USER, user);
    LOG.debug("created user " + user.getUsername());
    // add user to group
    Group group = dataService.query(GROUP, Group.class).eq(NAME, ALL_USER_GROUP).findOne();
    GroupMember groupMember = null;
    if (group != null) {
        groupMember = groupMemberFactory.create();
        groupMember.setGroup(group);
        groupMember.setUser(user);
        dataService.add(GROUP_MEMBER, groupMember);
    }
    // send activation email
    URI activationUri = URI.create(baseActivationUri + '/' + activationCode);
    try {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setTo(activationEmailAddresses.toArray(new String[] {}));
        mailMessage.setSubject("User registration for " + appSettings.getTitle());
        mailMessage.setText(createActivationEmailText(user, activationUri));
        mailSender.send(mailMessage);
    } catch (MailException mce) {
        LOG.error("Could not send signup mail", mce);
        if (groupMember != null) {
            dataService.delete(GROUP_MEMBER, groupMember);
        }
        dataService.delete(USER, user);
        throw new MolgenisUserException("An error occurred. Please contact the administrator. You are not signed up!");
    }
    LOG.debug("send activation email for user " + user.getUsername() + " to " + StringUtils.join(activationEmailAddresses, ','));
}
Also used : Group(org.molgenis.data.security.auth.Group) GroupMember(org.molgenis.data.security.auth.GroupMember) MolgenisDataException(org.molgenis.data.MolgenisDataException) SimpleMailMessage(org.springframework.mail.SimpleMailMessage) MolgenisUserException(org.molgenis.security.user.MolgenisUserException) MailException(org.springframework.mail.MailException) URI(java.net.URI) RunAsSystem(org.molgenis.security.core.runas.RunAsSystem) Transactional(org.springframework.transaction.annotation.Transactional)

Example 28 with MailException

use of org.springframework.mail.MailException in project vaadin-jsf-integration by alejandro-du.

the class PasswordHint method execute.

public String execute() {
    getFacesContext().getViewRoot().setViewId("/passwordHint.xhtml");
    // ensure that the username has been sent
    if (username == null || "".equals(username)) {
        log.warn("Username not specified, notifying user that it's a required field.");
        addError("errors.required", getText("user.username"));
        return null;
    } else if (username.endsWith(".xhtml")) {
        username = username.substring(0, username.indexOf(".xhtml"));
    }
    if (log.isDebugEnabled()) {
        log.debug("Processing Password Hint...");
    }
    // look up the user's information
    try {
        User user = userManager.getUserByUsername(username);
        StringBuilder msg = new StringBuilder();
        msg.append("Your password hint is: ").append(user.getPasswordHint());
        msg.append("\n\nLogin at: ").append(RequestUtil.getAppURL(getRequest()));
        message.setTo(user.getEmail());
        String subject = '[' + getText("webapp.name") + "] " + getText("user.passwordHint");
        message.setSubject(subject);
        message.setText(msg.toString());
        mailEngine.send(message);
        addMessage("login.passwordHint.sent", new Object[] { username, user.getEmail() });
    } catch (UsernameNotFoundException e) {
        log.warn(e.getMessage());
        // If exception is expected do not rethrow
        addError("login.passwordHint.error", username);
    } catch (MailException me) {
        addError(me.getCause().getLocalizedMessage());
    }
    return "success";
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) User(org.appfuse.model.User) MailException(org.springframework.mail.MailException)

Example 29 with MailException

use of org.springframework.mail.MailException in project microservices by pwillhan.

the class MailService method sendMailAlert.

@ServiceActivator(inputChannel = "notifyChannel")
public void sendMailAlert(String body) {
    String[] parts = body.split("\\|");
    String email = parts[0];
    String realbody = parts[1];
    logger.info("MailService : " + body);
    SimpleMailMessage msg = new SimpleMailMessage();
    msg.setTo(email);
    msg.setFrom(env.getProperty("com.cassandrawebtrader.mail.from"));
    msg.setSubject(env.getProperty("com.cassandrawebtrader.mail.subject"));
    msg.setText(realbody);
    try {
        mailSender.send(msg);
        logger.info(msg.toString());
    } catch (MailException e) {
        logger.error(e.getMessage());
    }
}
Also used : SimpleMailMessage(org.springframework.mail.SimpleMailMessage) MailException(org.springframework.mail.MailException) ServiceActivator(org.springframework.integration.annotation.ServiceActivator)

Example 30 with MailException

use of org.springframework.mail.MailException in project oc-explorer by devgateway.

the class SendEmailService method sendEmail.

public void sendEmail(final String subject, final String text, final String to) {
    SimpleMailMessage msg = new SimpleMailMessage();
    msg.setTo(to);
    msg.setFrom("support@developmentgateway.org");
    msg.setSubject(subject);
    msg.setText(text);
    try {
        LOGGER.info("Sending email " + msg);
        javaMailSenderImpl.send(msg);
    } catch (MailException e) {
        e.printStackTrace();
    }
}
Also used : SimpleMailMessage(org.springframework.mail.SimpleMailMessage) MailException(org.springframework.mail.MailException)

Aggregations

MailException (org.springframework.mail.MailException)47 MimeMessage (javax.mail.internet.MimeMessage)18 SimpleMailMessage (org.springframework.mail.SimpleMailMessage)17 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)15 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 URI (java.net.URI)2 Matcher (java.util.regex.Matcher)2 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)2 MolgenisDataException (org.molgenis.data.MolgenisDataException)2