Debug sending email - java-mail-1.4.4

Asked

Viewed 72 times

1

I have following code:

private static boolean envioTest(final String descricao, final String msg, final String to)throws MessagingException{
    final Properties props = new Properties();
    props.put("mail.smtp.host", "64.233.186.108");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");

    final Session session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(user,ps);
        }
    });

    try {
        final ByteArrayOutputStream os = new ByteArrayOutputStream();
        final PrintStream ps = new PrintStream(os);

        session.setDebug(true);
        session.setDebugOut(ps);
        final Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(user));
        message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
        message.setSubject(descricao);
        message.setText(msg);
        System.out.println("Enviando....");
        Transport.send(message);

        ps.println(); // Deveria imprimir?
        return true;
    } catch (MessagingException e) {
        throw e;
    }finally {
        System.out.println("\n");
    }
}

I would like to know how to get it to print the shipping information???

I don’t really know what to do with the PrintStream.

I tried a ps.println(); but he displays nothing.

Thank you!

  • Checks whether System.setOut(ps) is printed on the console.

  • No... and still does not display the System.out.println() following

  • Blz. I’ll see if I can find something and let you know.

  • Shipping information? You can specify, what shipping information?

  • When Session.setDebug(true); it records some logs on Printstream. I can’t print what he records on this Printstream

  • I didn’t get it right, you want to print the contents of the ps?

  • exactly.....

  • That one println() will just put a line separation on the current line, finishing the line, to print the content do os.toString(), nay?

Show 3 more comments

1 answer

1


Reflecting on the comments, I reached the following conclusion:

He needs a PrintStream right?

And why create one? If I already own the System.out?

So instead of creating a new one, I use what I already have:

try {
        session.setDebug(true);
        session.setDebugOut(System.out);

      final Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(user));
        message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
        message.setSubject(descricao);
        message.setText(msg);
        System.out.println("Enviando....");
        Transport.send(message);

        return true;
    } catch (MessagingException e) {
        throw e;
    }finally {
        System.out.println("\n");
    }

And so it worked perfectly!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.