我有一些代码无法成功执行,但我不明白为什么。
这个函数:
public void send(Envelope envelope) throws IOException
{
String CRLF = "\r\n";
sendCommand("MAIL FROM:"+ envelope.Sender + CRLF, 250);
//error occuring here. "Return Code:500 #5.5.1 command not recognized"
sendCommand("RCPT TO:" + envelope.Recipient + CRLF, 250);
sendCommand("DATA", 354);
toServer.print(envelope.Message.Headers + CRLF);
toServer.print(envelope.Message.Body + CRLF);
toServer.print("." +CRLF);
}
上面的代码调用了这个函数:
private void sendCommand(String command, int rc) throws IOException
{
/* Write command to server */
toServer.print(command + CRLF);
/*read reply from server. */
String line = fromServer.readLine();
System.err.println("Request: " + command);
System.err.println("Return Code:" + line);
/*
* Check that the server's reply code is the same as the parameter rc.
* If not, throw an IOException.
*/
if (!line.substring(0,3).equals(rc+""))
{
throw new IOException();
}
}
信息传输如下:
Socket connection = new Socket(envelope.DestAddr, SMTP_PORT);
fromServer = new BufferedReader(new InputStreamReader(connection.getInputStream()));
toServer = new PrintStream(connection.getOutputStream());
我使用相同的“From”和“To”。由于某种原因,RCPT TO: 命令遇到错误:
"Return Code:500 #5.5.1 command not recognized"
编辑:我确实通过远程登录手动尝试过它们
最佳答案
您将追加 \r\n
两次 - 一次在构建字符串时的 send()
中,一次在 print()
调用的 sendCommand()
中。
第二个\r\n
触发500 5.5.1 Unrecognized命令。
关于java - RCPT 至 : <rcpt@host> SMTP syntax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14668479/