在 Java 中打印变量值

发布于 2024-10-19 19:24:44 字数 1344 浏览 7 评论 0原文

我有一个名为 message 的新 MimeMessage 对象,我想找出它传递到我的外发邮件服务器的内容。然而我不知道如何在 Java 中打印出这样的变量。这是一些代码:

private String sendConfirmationEmail (String to, String from, String subject, String body, boolean CCSender) {
      try
      {
  //            String smtpHost = Properties.smtp;
          String smtpHost = Properties.smtp;      
          String fromAddress = from;
          String toAddress = to;

          Properties properties = System.getProperties();
          properties.put("mail.smtp.host", smtpHost);

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

          MimeMessage message = new MimeMessage(session);

          message.setFrom(new InternetAddress(fromAddress));
          message.setRecipient(Message.RecipientType.TO,
                  new InternetAddress(toAddress));

          if (CCSender) {
            message.setRecipient(Message.RecipientType.CC,
                    new InternetAddress(from));
          }
          message.setSubject(subject);
          message.setText(body);

          System.out.println(message); <=== I want this to work!

          message.saveChanges();
          Transport.send(message);
          return "1:success";
      }
      catch(Exception e)
      {
          return "0:failure "+e.toString();
      }
  }

任何帮助将不胜感激。

谢谢。

I've got an object which is a new MimeMessage called message and I want to find out what it's passing to my outgoing mail server. I however have no idea how one gets a variable like this printed out in Java. Here's some code:

private String sendConfirmationEmail (String to, String from, String subject, String body, boolean CCSender) {
      try
      {
  //            String smtpHost = Properties.smtp;
          String smtpHost = Properties.smtp;      
          String fromAddress = from;
          String toAddress = to;

          Properties properties = System.getProperties();
          properties.put("mail.smtp.host", smtpHost);

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

          MimeMessage message = new MimeMessage(session);

          message.setFrom(new InternetAddress(fromAddress));
          message.setRecipient(Message.RecipientType.TO,
                  new InternetAddress(toAddress));

          if (CCSender) {
            message.setRecipient(Message.RecipientType.CC,
                    new InternetAddress(from));
          }
          message.setSubject(subject);
          message.setText(body);

          System.out.println(message); <=== I want this to work!

          message.saveChanges();
          Transport.send(message);
          return "1:success";
      }
      catch(Exception e)
      {
          return "0:failure "+e.toString();
      }
  }

Any help would be much appreciated.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

书间行客 2024-10-26 19:24:44

如果它是你可以控制的类,你可以重写它的 toString() 方法

如果你无法控制它,你必须编写一个类似的方法

String valueOf(Message message) {
    StringBuilder sb = new StringBuilder();
    sb.append(message.someValue());
    sb.append(message.someOtherValue());
    return sb.toString();
}

并在调试消息中使用 valueOf

对于 MimeMessage,我依赖它其内容的 toString 实现

String valueOf(Message message) {
    return message.getContent().toString();
}

If it's a class you have control of, you override it's toString() method

If you have no control over it, you'll have to write a method like

String valueOf(Message message) {
    StringBuilder sb = new StringBuilder();
    sb.append(message.someValue());
    sb.append(message.someOtherValue());
    return sb.toString();
}

and use valueOf in your debug message

For MimeMessage, I'd rely on it having a toString implementation for its content

String valueOf(Message message) {
    return message.getContent().toString();
}
独行侠 2024-10-26 19:24:44

对于MimeMessage,您可以调用message.writeTo(System.out);。您可能需要在调用 message.saveChanges() 之后执行此操作以确保一致性。

For MimeMessage, you can call message.writeTo(System.out);. You will probably want to do this after calling message.saveChanges() to ensure consistency.

寂寞美少年 2024-10-26 19:24:44

您可以创建一个实现基接口的装饰器类,并记录/打印包装类的 toString() (或基接口具有的任何其他方法)返回的所有内容。

You could create a decorator class implementing the base interface, and logging/printing everything returned by the wrapped class' toString() (or whatever other methods the base interface has).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文