Example usage for org.springframework.mail MailException contains

List of usage examples for org.springframework.mail MailException contains

Introduction

In this page you can find the example usage for org.springframework.mail MailException contains.

Prototype

public boolean contains(@Nullable Class<?> exType) 

Source Link

Document

Check whether this exception contains an exception of the given type: either it is of the given class itself or it contains a nested cause of the given type.

Usage

From source file:edu.txstate.dmlab.clusteringwiki.web.RegisterController.java

@RequestMapping("register.*")
public String getRegisterPage(HttpServletRequest request, HttpServletResponse response, Model model) {

    String action = request.getParameter("applAction");
    if (action != null && action.equals("register") && isAjaxRequest(request)) {
        //requesting registration
        String email = request.getParameter("email");
        String password = request.getParameter("password");
        String firstName = request.getParameter("firstname");
        String lastName = request.getParameter("lastname");

        if (email == null || password == null) {
            sendOutput(response, "{\"error\":\"Invalid registration request received.\"}");
            return null;
        }// ww  w  .j a  va2s  . c o  m

        email = email.toLowerCase();

        IUserDao dao = applicationUser.getUserDao();

        User user = dao.selectUserByEmail(email);
        if (user != null) {
            sendOutput(response,
                    "{\"error\":\"An account is already registered with this "
                            + "email. Please use the forgot password feature to retrieve your credentials "
                            + "or choose an alternate email address.\"}");
            return null;
        }

        //Create a thread safe "sandbox" of the mailMessage
        SimpleMailMessage msg = new SimpleMailMessage(mailMessage);
        msg.setSubject("ClusteringWiki account created");
        msg.setTo(email);
        msg.setText("Dear " + firstName + ", \n\n"
                + "A ClusteringWiki account has been created for this email address.  Log in to ClusteringWiki to "
                + "start editing search result clusters.  Right-click on nodes to access available "
                + "editing operations.  Your edits will improve search for you as well as others "
                + "quering similar things.  \n\n " + "\n\nThank you,\n\nClusteringWiki Admin");
        try {
            mailSender.send(msg);
        } catch (MailException ex) {
            if (ex.contains(com.sun.mail.smtp.SMTPAddressFailedException.class)) {
                sendOutput(response,
                        "{\"error\":\"Invalid email address.  Please specify a valid email address.\"}");
            } else if (!ex.contains(com.sun.mail.smtp.SMTPSendFailedException.class)) {
                //ignore not being able to send this message out.
                sendOutput(response, "{\"error\":\"Email message could not be sent: <br><br>"
                        + StringEscapeUtils.escapeJavaScript(ex.getMessage().replace("\n", "<br>")) + "\"}");
            }
            return null;
        }

        user = new User();
        user.setEmail(email);
        user.setPassword(password);
        user.setFirstName(firstName);
        user.setLastName(lastName);
        try {
            dao.saveUser(user);
        } catch (Exception e) {
            sendOutput(response, "{\"error\":\"Registration failed: " + e.getMessage() + ".\"}");
            return null;
        }

        applicationUser.setEmail(email);
        applicationUser.setPassword(password);
        try {
            applicationUser.logIn();
        } catch (Exception e) {
            sendOutput(response, "{\"error\":\"Login error: " + e.getMessage() + "\"}");
            return null;
        }

        sendOutput(response, "{\"success\":true}");
        return null;
    }

    return "register";
}

From source file:edu.txstate.dmlab.clusteringwiki.web.LoginController.java

@RequestMapping("reminder.*")
public void sendReminder(HttpServletRequest request, HttpServletResponse response, Model model) {
    // send an email with link to allow changing password
    String action = request.getParameter("applAction");
    if (action != null && action.equals("sendReminder") && isAjaxRequest(request)) {
        //requesting registration
        String email = request.getParameter("email");

        if (email == null) {
            sendOutput(response, "{\"error\":\"Invalid reminder request received.\"}");
            return;
        }// w  w w .  j  ava 2s . c  o m

        email = email.toLowerCase();

        IUserDao dao = applicationUser.getUserDao();

        User user = dao.selectUserByEmail(email);
        if (user == null) {
            sendOutput(response, "{\"error\":\"Invalid email.  Please try again.\"}");
            return;
        }

        //create password request link
        CredentialsRequest cred = new CredentialsRequest();
        cred.setEmail(email);
        String link = request.getRequestURL().toString();
        link = link.replace("reminder.html", "changePassword.html?key=" + cred.getKey());

        //Create a thread safe "sandbox" of the mailMessage
        SimpleMailMessage msg = new SimpleMailMessage(mailMessage);
        msg.setTo(email);
        msg.setText("Dear " + user.getFirstName() + ", \n\n"
                + "We have received a forgot password request at ClusteringWiki for the account "
                + "associated with this email address.  If you did not initiate this request, please "
                + "ignore this email message.  Otherwise, copy and paste the link below in your "
                + "browser to complete your password reset.  Please note this forgot pasword request "
                + "will expire in one hour. \n\n " + link + "\n\nThank you,\n\nClusterWiki Admin");
        try {
            mailSender.send(msg);
        } catch (MailException ex) {
            if (ex.contains(com.sun.mail.smtp.SMTPAddressFailedException.class)) {
                sendOutput(response,
                        "{\"error\":\"The email address is no longer valid.  Please contact an administrator or create a new account.\"}");
            } else if (ex.contains(com.sun.mail.smtp.SMTPSendFailedException.class)) {
                //ignore not being able to send this message out.
                sendOutput(response,
                        "{\"error\":\"Email message could not be sent.  Please try again later.\"}");
            } else
                sendOutput(response, "{\"error\":\"Email message could not be sent: <br><br>"
                        + StringEscapeUtils.escapeJavaScript(ex.getMessage().replace("\n", "<br>")) + "\"}");
            return;
        }

        //make valid and save credentials request
        cred.setValid(1);
        try {
            credentialsRequestDao.saveCredentialsRequest(cred);
        } catch (Exception e) {
            sendOutput(response, "{\"error\":\"Credential request could not be saved.  Please try again.\"}");
            return;
        }

        sendOutput(response, "{\"success\":true}");
        return;
    }
}