Example usage for javax.mail SendFailedException getLocalizedMessage

List of usage examples for javax.mail SendFailedException getLocalizedMessage

Introduction

In this page you can find the example usage for javax.mail SendFailedException getLocalizedMessage.

Prototype

public String getLocalizedMessage() 

Source Link

Document

Creates a localized description of this throwable.

Usage

From source file:com.youxifan.utils.EMail.java

/**
 *   Send Mail direct/* w ww  . j a  v a 2s  . c  o m*/
 *   @return OK or error message
 */
public String send() {
    log.info("(" + m_smtpHost + ") " + m_from + " -> " + m_to);
    m_sentMsg = null;
    //
    if (!isValid(true)) {
        m_sentMsg = "Invalid Data";
        return m_sentMsg;
    }
    //
    Properties props = System.getProperties();
    props.put("mail.store.protocol", "smtp");
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.host", m_smtpHost);
    //   Bit-Florin David
    props.put("mail.smtp.port", String.valueOf(m_smtpPort));
    //   TLS settings
    if (m_isSmtpTLS) {
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.socketFactory.port", String.valueOf(m_smtpPort));
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
    }

    //
    Session session = null;
    try {
        if (m_auth != null) //   createAuthenticator was called
            props.put("mail.smtp.auth", "true");
        //         if (m_smtpHost.equalsIgnoreCase("smtp.gmail.com")) {
        //            // TODO: make it configurable
        //            // Enable gmail port and ttls - Hardcoded
        //            props.put("mail.smtp.port", "587");
        //            props.put("mail.smtp.starttls.enable", "true");
        //         }

        session = Session.getInstance(props, m_auth);
    } catch (SecurityException se) {
        log.warn("Auth=" + m_auth + " - " + se.toString());
        m_sentMsg = se.toString();
        return se.toString();
    } catch (Exception e) {
        log.warn("Auth=" + m_auth, e);
        m_sentMsg = e.toString();
        return e.toString();
    }

    try {
        //   m_msg = new MimeMessage(session);
        m_msg = new SMTPMessage(session);
        //   Addresses
        m_msg.setFrom(m_from);
        InternetAddress[] rec = getTos();
        if (rec.length == 1)
            m_msg.setRecipient(Message.RecipientType.TO, rec[0]);
        else
            m_msg.setRecipients(Message.RecipientType.TO, rec);
        rec = getCcs();
        if (rec != null && rec.length > 0)
            m_msg.setRecipients(Message.RecipientType.CC, rec);
        rec = getBccs();
        if (rec != null && rec.length > 0)
            m_msg.setRecipients(Message.RecipientType.BCC, rec);
        if (m_replyTo != null)
            m_msg.setReplyTo(new Address[] { m_replyTo });
        //
        m_msg.setSentDate(new java.util.Date());
        m_msg.setHeader("Comments", "Becit Mail");
        m_msg.setHeader("Comments", "Becit ERP Mail");
        //   m_msg.setDescription("Description");
        //   SMTP specifics
        m_msg.setAllow8bitMIME(true);
        //   Send notification on Failure & Success - no way to set envid in Java yet
        //   m_msg.setNotifyOptions (SMTPMessage.NOTIFY_FAILURE | SMTPMessage.NOTIFY_SUCCESS);
        //   Bounce only header
        m_msg.setReturnOption(SMTPMessage.RETURN_HDRS);
        //   m_msg.setHeader("X-Mailer", "msgsend");
        //
        setContent();
        m_msg.saveChanges();
        //   log.fine("message =" + m_msg);
        //
        //   Transport.send(msg);
        Transport t = session.getTransport("smtp");
        //   log.fine("transport=" + t);
        t.connect();
        //   t.connect(m_smtpHost, user, password);
        //   log.fine("transport connected");
        Transport.send(m_msg);
        //   t.sendMessage(msg, msg.getAllRecipients());
        log.info("Success - MessageID=" + m_msg.getMessageID());
    } catch (MessagingException me) {
        Exception ex = me;
        StringBuffer sb = new StringBuffer("(ME)");
        boolean printed = false;
        do {
            if (ex instanceof SendFailedException) {
                SendFailedException sfex = (SendFailedException) ex;
                Address[] invalid = sfex.getInvalidAddresses();
                if (!printed) {
                    if (invalid != null && invalid.length > 0) {
                        sb.append(" - Invalid:");
                        for (int i = 0; i < invalid.length; i++)
                            sb.append(" ").append(invalid[i]);

                    }
                    Address[] validUnsent = sfex.getValidUnsentAddresses();
                    if (validUnsent != null && validUnsent.length > 0) {
                        sb.append(" - ValidUnsent:");
                        for (int i = 0; i < validUnsent.length; i++)
                            sb.append(" ").append(validUnsent[i]);
                    }
                    Address[] validSent = sfex.getValidSentAddresses();
                    if (validSent != null && validSent.length > 0) {
                        sb.append(" - ValidSent:");
                        for (int i = 0; i < validSent.length; i++)
                            sb.append(" ").append(validSent[i]);
                    }
                    printed = true;
                }
                if (sfex.getNextException() == null)
                    sb.append(" ").append(sfex.getLocalizedMessage());
            } else if (ex instanceof AuthenticationFailedException) {
                sb.append(" - Invalid Username/Password - " + m_auth);
            } else //   other MessagingException 
            {
                String msg = ex.getLocalizedMessage();
                if (msg == null)
                    sb.append(": ").append(ex.toString());
                else {
                    if (msg.indexOf("Could not connect to SMTP host:") != -1) {
                        int index = msg.indexOf('\n');
                        if (index != -1)
                            msg = msg.substring(0, index);
                        String cc = "??";

                        msg += " - AD_Client_ID=" + cc;
                    }
                    String className = ex.getClass().getName();
                    if (className.indexOf("MessagingException") != -1)
                        sb.append(": ").append(msg);
                    else
                        sb.append(" ").append(className).append(": ").append(msg);
                }
            }
            //   Next Exception
            if (ex instanceof MessagingException)
                ex = ((MessagingException) ex).getNextException();
            else
                ex = null;
        } while (ex != null); //   error loop
        m_sentMsg = sb.toString();
        return sb.toString();
    } catch (Exception e) {
        log.warn("", e);
        m_sentMsg = e.getLocalizedMessage();
        return e.getLocalizedMessage();
    }
    //
    m_sentMsg = SENT_OK;
    return m_sentMsg;
}

From source file:org.jasig.mygps.business.StudentIntakeRequestManager.java

public void processStudentIntakeRequest(AppointmentTO obj, UUID personId) throws ObjectNotFoundException {
    Person student = personDao.get(personId);

    Task studentIntakeTask = createIntakeTask(student);

    try {//from  ww w.jav  a  2 s . com

        taskService.create(studentIntakeTask);

        SubjectAndBody studentIntakeMessage = messageTemplateService
                .createStudentIntakeTaskMessage(studentIntakeTask);
        String ccString = buildCCString(obj.getIntakeEmail(), student.getSecondaryEmailAddress());
        messageService.createMessage(student, ccString, studentIntakeMessage);

        clearIntakeData(student);

        personDao.save(student);

    } catch (SendFailedException e) {
        LOGGER.error(e.getLocalizedMessage());
    } catch (ValidationException e) {
        LOGGER.error(e.getLocalizedMessage());
    }

}