我正在本地计算机上运行smtp4dev(http://smtp4dev.codeplex.com/)并尝试编写一个使用SMTP发送邮件的简单Java程序。
但是当我运行它时,没有收到电子邮件。 smtp4dev录制了一个会话,但没有检索任何内容。不明白为什么,因为当我使用telnet执行完全相同的操作时,它可以正常工作,但不适用于Java。
我也试图打印到终端服务器回复的内容,但它只给了我第一个“220 localhost smtp4dev ready”,然后再没有了。
这是我的客户:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Main {
public static void main(String [] args) {
String hostName = "localhost";
int portNumber = 25;
String userInput = "";
String mailFrom = "sender@server.com";
String mailTo = "receiver@server.com";
String subject = "Important matters";
String data = "Finish your mission in 24h, sir. Or else...";
try {
Socket emailSocket = new Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(
emailSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(emailSocket.getInputStream()));
System.out.println(in.readLine());
out.append("HELO" + hostName);
out.append("MAIL FROM: <" + mailFrom + ">");
System.out.println(in.readLine());
out.append("RCPT TO: <" + mailTo + ">");
System.out.println(in.readLine());
out.append("DATA");
System.out.println(in.readLine());
out.append(data);
out.append(".");
System.out.println(in.readLine());
out.append("QUIT");
System.out.println(in.readLine());
}
catch (Exception e) {}
}
}