简体   繁体   English

使用javax.mail时获取服务器输出

[英]Get server output while using javax.mail

I need to implement POP3, IMAP and SMTP servers availability and identity checking by connecting to them, authorizing and comparing expected server output with received server output while doing previously mentioned operations. 我需要实现POP3,IMAP和SMTP服务器的可用性和身份检查,方法是将它们连接到它们,并在执行上述操作时授权并比较预期的服务器输出与接收到的服务器输出。

I'm using Java, so not to reinvent the wheel, I'm also using javax.mail implementation. 我正在使用Java,所以不要重蹈覆辙,我也在使用javax.mail实现。 Everything works fine, except that there's no way to get recorded server output. 一切工作正常,除了无法获取记录的服务器输出。 The only available source is debug output: 唯一可用的来源是调试输出:

Properties props = new Properties();
props.setProperty("mail.pop3.debug", "true");
Session session = Session.getInstance(props, null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
session.setDebug(true);
session.setDebugOut(new PrintStream(out));
Store store = new POP3Store(session, getUrl());
store.connect();
store.close();
out.toString(); // < the only available communication output

The only way I'm able to get server output is by parsing debug output (searching for lines beginning with "S: " and reading strings until line beginning with "C: " or end of stream is found). 我能够获取服务器输出的唯一方法是解析调试输出(搜索以“ S:”开头的行并读取字符串,直到找到以“ C:”开头的行或流的末尾)。

Sample POP3 communication debug output looks like this: 样本POP3通信调试输出如下所示:

DEBUG POP3: connecting to host "pop.gmail.com", port 995, isSSL true
S: +OK Gpop ready for requests from *** ***
C: CAPA
S: +OK Capability list follows
USER
RESP-CODES
EXPIRE 0
LOGIN-DELAY 300
X-GOOGLE-VERHOEVEN
UIDL
.
C: USER ***
S: +OK send PASS
C: PASS ***
S: +OK Welcome.
C: QUIT
S: +OK Farewell.

The only other alternative I see here is to use custom SocketFactory (set "mail.*.socketFactory" property), which would produce my initially set Socket, from which I'd be able to get server input stream. 我在这里看到的唯一其他选择是使用自定义SocketFactory(设置为“ mail。*。socketFactory”属性),这将生成我最初设置的Socket,从中可以获取服务器输入流。

I believe there's a better solution for this. 我相信有一个更好的解决方案。 Do you see any? 你看到了吗?

Thanks in advance. 提前致谢。

I'd just connect via a socket and supply the exact test case. 我只是通过一个插座连接并提供确切的测试用例。 I wouldn't use javamail for this particular application. 我不会将Javamail用于此特定应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM