Example usage for javax.mail MessagingException printStackTrace

List of usage examples for javax.mail MessagingException printStackTrace

Introduction

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

Prototype

public void printStackTrace(PrintStream s) 

Source Link

Document

Prints this throwable and its backtrace to the specified print stream.

Usage

From source file:SendMime.java

/** Do the work: send the mail to the SMTP server. */
public void doSend() throws IOException, MessagingException {

    // Create the Session object
    session = Session.getDefaultInstance(null, null);
    session.setDebug(true); // Verbose!

    try {//from  ww  w  . j a v  a 2 s.c om
        // create a message
        mesg = new MimeMessage(session);

        // From Address - this should come from a Properties...
        mesg.setFrom(new InternetAddress("nobody@host.domain"));

        // TO Address
        InternetAddress toAddress = new InternetAddress(message_recip);
        mesg.addRecipient(Message.RecipientType.TO, toAddress);

        // CC Address
        InternetAddress ccAddress = new InternetAddress(message_cc);
        mesg.addRecipient(Message.RecipientType.CC, ccAddress);

        // The Subject
        mesg.setSubject(message_subject);

        // Now the message body.
        Multipart mp = new MimeMultipart();

        BodyPart textPart = new MimeBodyPart();
        textPart.setText(message_body); // sets type to "text/plain"

        BodyPart pixPart = new MimeBodyPart();
        pixPart.setContent(html_data, "text/html");

        // Collect the Parts into the MultiPart
        mp.addBodyPart(textPart);
        mp.addBodyPart(pixPart);

        // Put the MultiPart into the Message
        mesg.setContent(mp);

        // Finally, send the message!
        Transport.send(mesg);

    } catch (MessagingException ex) {
        System.err.println(ex);
        ex.printStackTrace(System.err);
    }
}

From source file:org.jenkinsci.plugins.send_mail_builder.SendMailBuilder.java

@Override
public boolean perform(final AbstractBuild<?, ?> build, final Launcher launcher, final BuildListener listener)
        throws IOException, InterruptedException {
    final EnvVars env = build.getEnvironment(listener);
    final String charset = Mailer.descriptor().getCharset();
    final MimeMessage msg = new MimeMessage(Mailer.descriptor().createSession());
    try {//  w w  w  .jav  a2  s.  c om
        msg.setFrom(Mailer.StringToAddress(JenkinsLocationConfiguration.get().getAdminAddress(), charset));
        msg.setContent("", "text/plain");
        msg.setSentDate(new Date());
        String actualReplyTo = env.expand(replyTo);
        if (StringUtils.isBlank(actualReplyTo)) {
            actualReplyTo = Mailer.descriptor().getReplyToAddress();
        }
        if (StringUtils.isNotBlank(actualReplyTo)) {
            msg.setReplyTo(new Address[] { Mailer.StringToAddress(actualReplyTo, charset) });
        }
        msg.setRecipients(RecipientType.TO, toInternetAddresses(listener, env.expand(tos), charset));
        msg.setRecipients(RecipientType.CC, toInternetAddresses(listener, env.expand(ccs), charset));
        msg.setRecipients(RecipientType.BCC, toInternetAddresses(listener, env.expand(bccs), charset));
        msg.setSubject(env.expand(subject), charset);
        msg.setText(env.expand(text), charset);
        Transport.send(msg);
    } catch (final MessagingException e) {
        listener.getLogger().println(Messages.SendMail_FailedToSend());
        e.printStackTrace(listener.error(e.getMessage()));
        return false;
    }
    listener.getLogger().println(Messages.SendMail_SentSuccessfully());
    return true;
}

From source file:hudson.tasks.MailSender.java

public boolean execute(AbstractBuild<?, ?> build, BuildListener listener) throws InterruptedException {
    try {// ww w.  j  ava2s  .  c  o  m
        MimeMessage mail = getMail(build, listener);
        if (mail != null) {
            // if the previous e-mail was sent for a success, this new e-mail
            // is not a follow up
            AbstractBuild<?, ?> pb = build.getPreviousBuild();
            if (pb != null && pb.getResult() == Result.SUCCESS) {
                mail.removeHeader("In-Reply-To");
                mail.removeHeader("References");
            }

            Address[] allRecipients = mail.getAllRecipients();
            if (allRecipients != null) {
                StringBuilder buf = new StringBuilder("Sending e-mails to:");
                for (Address a : allRecipients)
                    buf.append(' ').append(a);
                listener.getLogger().println(buf);
                Transport.send(mail);

                build.addAction(new MailMessageIdAction(mail.getMessageID()));
            } else {
                listener.getLogger().println(Messages.MailSender_ListEmpty());
            }
        }
    } catch (MessagingException e) {
        e.printStackTrace(listener.error(e.getMessage()));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace(listener.error(e.getMessage()));
    } finally {
        CHECKPOINT.report();
    }

    return true;
}

From source file:edu.harvard.iq.dataverse.MailServiceBean.java

public void sendMail(String host, String from, String to, String subject, String messageText) {
    Properties props = System.getProperties();
    props.put("mail.smtp.host", host);
    Session session = Session.getDefaultInstance(props, null);

    try {/*from  w ww. j a  v a 2  s  . c o  m*/
        MimeMessage msg = new MimeMessage(session);
        String[] recipientStrings = to.split(",");
        InternetAddress[] recipients = new InternetAddress[recipientStrings.length];
        try {
            msg.setFrom(new InternetAddress(from, charset));
            for (int i = 0; i < recipients.length; i++) {
                recipients[i] = new InternetAddress(recipientStrings[i], "", charset);
            }
        } catch (UnsupportedEncodingException ex) {
            logger.severe(ex.getMessage());
        }
        msg.setRecipients(Message.RecipientType.TO, recipients);
        msg.setSubject(subject, charset);
        msg.setText(messageText, charset);
        Transport.send(msg, recipients);
    } catch (AddressException ae) {
        ae.printStackTrace(System.out);
    } catch (MessagingException me) {
        me.printStackTrace(System.out);
    }
}

From source file:edu.harvard.iq.dataverse.MailServiceBean.java

public void sendMail(String from, String to, String subject, String messageText,
        Map<Object, Object> extraHeaders) {
    try {/*w  w  w .  j a v  a  2 s  .c  om*/
        MimeMessage msg = new MimeMessage(session);
        if (from.matches(EMAIL_PATTERN)) {
            msg.setFrom(new InternetAddress(from));
        } else {
            // set fake from address; instead, add it as part of the message
            //msg.setFrom(new InternetAddress("invalid.email.address@mailinator.com"));
            msg.setFrom(getSystemAddress());
            messageText = "From: " + from + "\n\n" + messageText;
        }
        msg.setSentDate(new Date());
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
        msg.setSubject(subject, charset);
        msg.setText(messageText, charset);

        if (extraHeaders != null) {
            for (Object key : extraHeaders.keySet()) {
                String headerName = key.toString();
                String headerValue = extraHeaders.get(key).toString();

                msg.addHeader(headerName, headerValue);
            }
        }

        Transport.send(msg);
    } catch (AddressException ae) {
        ae.printStackTrace(System.out);
    } catch (MessagingException me) {
        me.printStackTrace(System.out);
    }
}

From source file:edu.harvard.iq.dataverse.MailServiceBean.java

public boolean sendSystemEmail(String to, String subject, String messageText) {

    boolean sent = false;
    String rootDataverseName = dataverseService.findRootDataverse().getName();
    String body = messageText + BundleUtil.getStringFromBundle("notification.email.closing",
            Arrays.asList(BrandingUtil.getInstallationBrandName(rootDataverseName)));
    logger.fine("Sending email to " + to + ". Subject: <<<" + subject + ">>>. Body: " + body);
    try {//from   w w w  .  j a v  a2s  . c o  m
        MimeMessage msg = new MimeMessage(session);
        InternetAddress systemAddress = getSystemAddress();
        if (systemAddress != null) {
            msg.setFrom(systemAddress);
            msg.setSentDate(new Date());
            String[] recipientStrings = to.split(",");
            InternetAddress[] recipients = new InternetAddress[recipientStrings.length];
            for (int i = 0; i < recipients.length; i++) {
                try {
                    recipients[i] = new InternetAddress('"' + recipientStrings[i] + '"', "", charset);
                } catch (UnsupportedEncodingException ex) {
                    logger.severe(ex.getMessage());
                }
            }
            msg.setRecipients(Message.RecipientType.TO, recipients);
            msg.setSubject(subject, charset);
            msg.setText(body, charset);
            try {
                Transport.send(msg, recipients);
                sent = true;
            } catch (SMTPSendFailedException ssfe) {
                logger.warning("Failed to send mail to " + to + " (SMTPSendFailedException)");
            }
        } else {
            logger.fine("Skipping sending mail to " + to + ", because the \"no-reply\" address not set ("
                    + Key.SystemEmail + " setting).");
        }
    } catch (AddressException ae) {
        logger.warning("Failed to send mail to " + to);
        ae.printStackTrace(System.out);
    } catch (MessagingException me) {
        logger.warning("Failed to send mail to " + to);
        me.printStackTrace(System.out);
    }
    return sent;
}

From source file:org.masukomi.aspirin.core.RemoteDelivery.java

/**
 * Insert the method's description here. Creation date: (2/25/00 1:14:18 AM)
 * //from  w  ww  .  j a v  a 2 s  .c  om
 * @param mail
 *            org.apache.james.core.MailImpl
 * @param exception
 *            java.lang.Exception
 * @param boolean
 *            permanent
 * @return boolean Whether the message failed fully and can be deleted
 */
private boolean failMessage(QuedItem qi, MailAddress recepient, MessagingException ex, boolean permanent) {
    log.debug("entering failMessage(QuedItem qi, MessagingException ex, boolean permanent)");
    // weird printy bits inherited from JAMES
    MailImpl mail = (MailImpl) qi.getMail();
    StringWriter sout = new StringWriter();
    PrintWriter out = new PrintWriter(sout, true);
    if (permanent) {
        out.print("Permanent");
    } else {
        out.print("Temporary");
    }
    StringBuffer logBuffer = new StringBuffer(64).append(" exception delivering mail (").append(mail.getName())
            .append(": ");
    out.print(logBuffer.toString());
    ex.printStackTrace(out);
    if (log.isWarnEnabled()) {
        log.warn(sout.toString());
    }
    // //////////////
    // / It is important to note that deliver will pass us a mail with a
    // modified
    // / list of recepients non permanent ones will only have valid
    // recepients left
    // /
    if (!permanent) {
        if (!mail.getState().equals(Mail.ERROR)) {
            mail.setState(Mail.ERROR);
            mail.setErrorMessage("0");
            mail.setLastUpdated(new Date());
        }
        if (qi.retryable(recepient)) {
            logBuffer = new StringBuffer(128).append("Storing message ").append(mail.getName())
                    .append(" into que after ").append(qi.getNumAttempts()).append(" attempts");
            if (log.isDebugEnabled()) {
                log.debug(logBuffer.toString());
            }
            qi.retry(que, recepient);
            //mail.setErrorMessage(qi.getNumAttempts() + "");
            mail.setLastUpdated(new Date());
            return false;
        } else {
            logBuffer = new StringBuffer(128).append("Bouncing message ").append(mail.getName())
                    .append(" after ").append(qi.getNumAttempts()).append(" attempts");
            if (log.isDebugEnabled()) {
                log.debug(logBuffer.toString());
            }
            qi.failForRecipient(que, recepient);
        }
    } else {
        qi.failForRecipient(que, recepient);
    }
    try {
        Bouncer.bounce(que, mail, ex.toString(), Configuration.getInstance().getPostmaster());
    } catch (MessagingException me) {
        log.debug("failed to bounce");
        log.error(me);
    }
    return true;
}