在 Java 中打印变量值
我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果它是你可以控制的类,你可以重写它的 toString() 方法
如果你无法控制它,你必须编写一个类似的方法
并在调试消息中使用 valueOf
对于 MimeMessage,我依赖它其内容的 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
and use valueOf in your debug message
For MimeMessage, I'd rely on it having a toString implementation for its content
对于
MimeMessage
,您可以调用message.writeTo(System.out);
。您可能需要在调用message.saveChanges()
之后执行此操作以确保一致性。For
MimeMessage
, you can callmessage.writeTo(System.out);
. You will probably want to do this after callingmessage.saveChanges()
to ensure consistency.您可以创建一个实现基接口的装饰器类,并记录/打印包装类的 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).