Example usage for javax.mail MessagingException toString

List of usage examples for javax.mail MessagingException toString

Introduction

In this page you can find the example usage for javax.mail MessagingException toString.

Prototype

@Override
public synchronized String toString() 

Source Link

Document

Override toString method to provide information on nested exceptions.

Usage

From source file:MessageViewer.java

protected Component getBodyComponent() {
    //------------
    // now get a content viewer for the main type...
    //------------
    try {// w ww . ja v a  2s. co  m
        DataHandler dh = displayed.getDataHandler();
        CommandInfo ci = dh.getCommand("view");
        if (ci == null) {
            throw new MessagingException("view command failed on: " + displayed.getContentType());
        }

        Object bean = dh.getBean(ci);
        if (bean instanceof Component) {
            return (Component) bean;
        } else {
            throw new MessagingException("bean is not a component " + bean.getClass().toString());
        }
    } catch (MessagingException me) {
        return new Label(me.toString());
    }
}

From source file:edu.ku.brc.helpers.EMailHelper.java

/**
 * Send an email. Also sends it as a gmail if applicable, and does password checking.
 * @param host host of SMTP server//from  w  w  w .j  a  v  a  2 s. c o  m
 * @param uName username of email account
 * @param pWord password of email account
 * @param fromEMailAddr the email address of who the email is coming from typically this is the same as the user's email
 * @param toEMailAddr the email addr of who this is going to
 * @param subject the Textual subject line of the email
 * @param bodyText the body text of the email (plain text???)
 * @param fileAttachment and optional file to be attached to the email
 * @return true if the msg was sent, false if not
 */
public static boolean sendMsgAsGMail(final String host, final String uName, final String pWord,
        final String fromEMailAddr, final String toEMailAddr, final String subject, final String bodyText,
        final String mimeType, @SuppressWarnings("unused") final String port,
        @SuppressWarnings("unused") final String security, final File fileAttachment) {
    String userName = uName;
    String password = pWord;
    Boolean fail = false;

    ArrayList<String> userAndPass = new ArrayList<String>();

    Properties props = System.getProperties();

    props.put("mail.smtp.host", host); //$NON-NLS-1$
    props.put("mail.smtp.auth", "true"); //$NON-NLS-1$ //$NON-NLS-2$
    props.put("mail.smtp.port", "587"); //$NON-NLS-1$ //$NON-NLS-2$
    props.put("mail.smtp.starttls.enable", "true"); //$NON-NLS-1$ //$NON-NLS-2$

    boolean usingSSL = false;
    if (usingSSL) {
        props.put("mail.smtps.port", "587"); //$NON-NLS-1$ //$NON-NLS-2$
        props.put("mail.smtp.starttls.enable", "true"); //$NON-NLS-1$ //$NON-NLS-2$

    }

    Session session = Session.getInstance(props, null);

    session.setDebug(instance.isDebugging);
    if (instance.isDebugging) {
        log.debug("Host:     " + host); //$NON-NLS-1$
        log.debug("UserName: " + userName); //$NON-NLS-1$
        log.debug("Password: " + password); //$NON-NLS-1$
        log.debug("From:     " + fromEMailAddr); //$NON-NLS-1$
        log.debug("To:       " + toEMailAddr); //$NON-NLS-1$
        log.debug("Subject:  " + subject); //$NON-NLS-1$
    }

    try {
        // create a message
        MimeMessage msg = new MimeMessage(session);

        msg.setFrom(new InternetAddress(fromEMailAddr));
        if (toEMailAddr.indexOf(",") > -1) //$NON-NLS-1$
        {
            StringTokenizer st = new StringTokenizer(toEMailAddr, ","); //$NON-NLS-1$
            InternetAddress[] address = new InternetAddress[st.countTokens()];
            int i = 0;
            while (st.hasMoreTokens()) {
                String toStr = st.nextToken().trim();
                address[i++] = new InternetAddress(toStr);
            }
            msg.setRecipients(Message.RecipientType.TO, address);
        } else {
            InternetAddress[] address = { new InternetAddress(toEMailAddr) };
            msg.setRecipients(Message.RecipientType.TO, address);
        }
        msg.setSubject(subject);

        //msg.setContent( aBodyText , "text/html;charset=\"iso-8859-1\"");

        // create the second message part
        if (fileAttachment != null) {
            // create and fill the first message part
            MimeBodyPart mbp1 = new MimeBodyPart();
            mbp1.setContent(bodyText, mimeType);//"text/html;charset=\"iso-8859-1\"");
            //mbp1.setContent(bodyText, "text/html;charset=\"iso-8859-1\"");

            MimeBodyPart mbp2 = new MimeBodyPart();

            // attach the file to the message
            FileDataSource fds = new FileDataSource(fileAttachment);
            mbp2.setDataHandler(new DataHandler(fds));
            mbp2.setFileName(fds.getName());

            // create the Multipart and add its parts to it
            Multipart mp = new MimeMultipart();
            mp.addBodyPart(mbp1);
            mp.addBodyPart(mbp2);

            // add the Multipart to the message
            msg.setContent(mp);

        } else {
            // add the Multipart to the message
            msg.setContent(bodyText, mimeType);
        }

        // set the Date: header
        msg.setSentDate(new Date());

        // send the message
        int cnt = 0;
        do {
            cnt++;
            SMTPTransport t = (SMTPTransport) session.getTransport("smtp"); //$NON-NLS-1$
            try {
                t.connect(host, userName, password);

                t.sendMessage(msg, msg.getAllRecipients());

                fail = false;
            } catch (MessagingException mex) {
                edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
                edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(EMailHelper.class, mex);
                instance.lastErrorMsg = mex.toString();

                Exception ex = null;
                if ((ex = mex.getNextException()) != null) {
                    ex.printStackTrace();
                    instance.lastErrorMsg = instance.lastErrorMsg + ", " + ex.toString(); //$NON-NLS-1$
                }

                //wrong username or password, get new one
                if (mex.toString().equals("javax.mail.AuthenticationFailedException")) //$NON-NLS-1$
                {
                    fail = true;
                    userAndPass = askForUserAndPassword((Frame) UIRegistry.getTopWindow());

                    if (userAndPass == null) {//the user is done
                        return false;
                    }
                    // else
                    //try again
                    userName = userAndPass.get(0);
                    password = userAndPass.get(1);
                }
            } finally {

                log.debug("Response: " + t.getLastServerResponse()); //$NON-NLS-1$
                t.close();
            }
        } while (fail && cnt < 6);

    } catch (MessagingException mex) {
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(EMailHelper.class, mex);
        instance.lastErrorMsg = mex.toString();

        //mex.printStackTrace();
        Exception ex = null;
        if ((ex = mex.getNextException()) != null) {
            ex.printStackTrace();
            instance.lastErrorMsg = instance.lastErrorMsg + ", " + ex.toString(); //$NON-NLS-1$
        }
        return false;

    } catch (Exception ex) {
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(EMailHelper.class, ex);
        ex.printStackTrace();
    }

    if (fail) {
        return false;
    } //else
    return true;
}

From source file:com.redhat.rhn.common.messaging.SmtpMail.java

/** {@inheritDoc} */
public void setHeader(String name, String value) {
    try {/*w w w  .  ja v  a  2  s. c  o m*/
        message.setHeader(name, value);
    } catch (MessagingException me) {
        String msg = "MessagingException while trying to send email: " + me.toString();
        log.warn(msg);
        throw new JavaMailException(msg, me);
    }
}

From source file:com.redhat.rhn.common.messaging.SmtpMail.java

/** {@inheritDoc} */
public void setSubject(String subIn) {
    try {// www .  ja  v  a2s . c o m
        message.setSubject(subIn);
    } catch (MessagingException me) {
        String msg = "MessagingException while trying to send email: " + me.toString();
        log.warn(msg);
        throw new JavaMailException(msg, me);
    }
}

From source file:com.redhat.rhn.common.messaging.SmtpMail.java

/** {@inheritDoc} */
public void setBody(String textIn) {
    try {/*from   w w w . java  2  s .co m*/
        message.setText(textIn);
    } catch (MessagingException me) {
        String msg = "MessagingException while trying to send email: " + me.toString();
        log.warn(msg);
        throw new JavaMailException(msg, me);
    }
}

From source file:com.redhat.rhn.common.messaging.SmtpMail.java

/** {@inheritDoc} */
public void send() {

    try {//from w ww .j  a  v  a  2s.  c  o  m
        Address[] addrs = message.getRecipients(RecipientType.TO);
        if (addrs == null || addrs.length == 0) {
            log.warn("Aborting mail message " + message.getSubject() + ": No recipients");
            return;
        }
        Transport.send(message);
    } catch (MessagingException me) {
        String msg = "MessagingException while trying to send email: " + me.toString();
        log.warn(msg);
        throw new JavaMailException(msg, me);
    }
}

From source file:se.vgregion.userfeedback.impl.FeedbackReportServiceImpl.java

private int reportByEmail(UserFeedback report) {
    try {//from ww  w .  j a v  a 2 s . c  o m
        sendReportByEmail(report, FEEDBACK_REPORT_EMAIL_SUBJECT);
        LOGGER.info("Successfully reported by email");
    } catch (MessagingException me) {

        System.out.println("Failed report by email" + me.toString());
        return -1;
    }
    return 0;
}

From source file:com.redhat.rhn.common.messaging.SmtpMail.java

/**
 * Private helper method to do the heavy lifting of setting the recipients field for
 * a message//from  w w  w . j  av a  2s .c o m
 * @param type The javax.mail.Message.RecipientType (To, CC, or BCC) for the recipients
 * @param recipIn A string array of email addresses
 */
private void setRecipients(RecipientType type, String[] recipIn) {
    log.debug("setRecipients called.");
    Address[] recAddr = null;
    try {
        List tmp = new LinkedList();
        for (int i = 0; i < recipIn.length; i++) {
            InternetAddress addr = new InternetAddress(recipIn[i]);
            log.debug("checking: " + addr.getAddress());
            if (verifyAddress(addr)) {
                log.debug("Address verified.  Adding: " + addr.getAddress());
                tmp.add(addr);
            }
        }
        recAddr = new Address[tmp.size()];
        tmp.toArray(recAddr);
        message.setRecipients(type, recAddr);
    } catch (MessagingException me) {
        String msg = "MessagingException while trying to send email: " + me.toString();
        log.warn(msg);
        throw new JavaMailException(msg, me);
    }
}

From source file:it.unimi.di.big.mg4j.document.JavamailDocumentCollection.java

public void close() throws IOException {
    super.close();
    try {/*from w  w  w  . j  a  va  2 s. c  o m*/
        folder.close(false);
        store.close();
    } catch (MessagingException e) {
        throw new IOException(e.toString());
    }
}

From source file:it.unimi.di.big.mg4j.document.JavamailDocumentCollection.java

public InputStream stream(final long index) throws IOException {
    ensureDocumentIndex(index);//from w w w . j a va2s  . c  o m
    try {
        // Can you believe that? Javamail numbers messages from 1...
        return folder.getMessage((int) (index + 1)).getInputStream();
    } catch (MessagingException e) {
        throw new IOException(e.toString());
    }
}