我一直在尝试启动并运行此SMTP客户端。客户端到达服务器并能够发送HELO消息。我也能得到一个成功的确认。但是在第一个命令之后,所有其他命令都会得到一个确认,说该命令没有被识别。这非常奇怪,我无法继续我的计划。请帮助.. :(
这是我的程序..你可以在eclipse和swing UI l comeup中编译它
enter code here
import java.awt.*;
import java.io.*;
import java.net.Socket;
import javax.swing.*;
public class Mail extends javax.swing.JFrame {
JLabel userLabel = new JLabel("USER");
JLabel passLabel = new JLabel("PASS");
JLabel fromLabel = new JLabel("FROM");
JLabel toLabel = new JLabel("TO");
JLabel subjectLabel = new JLabel("SUBJECT");
JLabel bodyLabel = new JLabel("BODY");
JLabel serverLabel=new JLabel("SERVER");
JButton sendButton = new JButton("Send");
JButton closeButton = new JButton("Close");
JTextField userText= new JTextField();
JPasswordField passText= new JPasswordField();
JTextField fromText= new JTextField();
JTextField toText= new JTextField();
JTextField subjectText= new JTextField();
JTextField bodyText = new JTextField();
JTextField serverText = new JTextField("smtp.nus.edu.sg");
JList testList= new JList() ;
JTextArea test= new JTextArea() ;
DefaultListModel testArea= new DefaultListModel();
BufferedReader input ;
PrintWriter output ;
public Mail(){
setTitle("User Agent");
this.setSize(1400,1400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(null);
userLabel.setBounds(12, 12, 70, 12);
passLabel.setBounds(12, 48, 70, 12);
fromLabel.setBounds(12, 84, 70, 12);
toLabel.setBounds(12, 120, 70, 12);
subjectLabel.setBounds(12, 156, 70, 12);
bodyLabel.setBounds(12, 192, 70, 12);
serverLabel.setBounds(12, 228, 70, 12);
sendButton.setBounds(12, 264, 70, 22);
closeButton.setBounds(130, 264, 70, 22);
//testArea.setBounds(130, 294, 170, 122);
userText.setBounds(60,12,200,22);
passText.setBounds(60,48,200,22);
fromText.setBounds(60,84,200,22);
toText.setBounds(60,120,200,22);
subjectText.setBounds(60,156,200,22);
bodyText.setBounds(60,192,200,22);
serverText.setBounds(60,228,200,22);
testList.setBounds(130, 294, 970, 922);
test.setBounds(400, 294, 970, 922);
getContentPane().add(userLabel);
getContentPane().add(passLabel);
getContentPane().add(fromLabel);
getContentPane().add(toLabel);
getContentPane().add(subjectLabel);
getContentPane().add(bodyLabel);
getContentPane().add(serverLabel);
getContentPane().add(sendButton);
getContentPane().add(closeButton);
getContentPane().add(userText);
getContentPane().add(passText);
getContentPane().add(fromText);
getContentPane().add(toText);
getContentPane().add(subjectText);
getContentPane().add(bodyText);
getContentPane().add(serverText);
getContentPane().add(testList);
// getContentPane().add(test);
testList.setModel(testArea);
testArea.addElement("this is a test message");
/*thehandler handler = new thehandler();
sendButton.addActionListener(handler);
*/
theHandler handler = new theHandler();
closeButton.addActionListener(handler);
sendButton.addActionListener(handler);
}
class theHandler implements java.awt.event.ActionListener{
public void actionPerformed(java.awt.event.ActionEvent event){
Object happen = event.getSource();
if (happen == closeButton)
closeButton_actionPerformed(event);
else if(happen == sendButton)
sendButton_actionPerformed(event);
}
}
/*void Send_actionPerformed(java.awt.event.ActionEvent event) {
System.exit(0);
}*/
void closeButton_actionPerformed(java.awt.event.ActionEvent event) {
System.exit(0);
//testText.setText("vikas");
}
void sendButton_actionPerformed(java.awt.event.ActionEvent event) {
try{
Socket sock = new Socket(serverText.getText(), 25);
//testArea.addElement(sock);
output = new PrintWriter(sock.getOutputStream(),true);
input = new BufferedReader(new InputStreamReader(sock.getInputStream()));
//output.flush();
pass(null);
pass("HELO\r\n");
pass("AUTH LOGIN\r\n");
//pass("AUTH LOGIN\r\n");
//pass(null);
//pass("TlVTU1RVXHUwOTA2OTc1");
//pass("TlVTU1RVXHUwOTA2OTc1");
}
catch(Exception e){
testArea.addElement("Error");
}
}
//testText.setText("vikas");
void pass(String s)throws java.io.IOException{
if(s!=null){
output.println(s);
System.out.println(s);
testArea.addElement("Me");
testArea.addElement(s);
}
String inline = input.readLine();
if(inline!= null)
testArea.addElement("Him");
testArea.addElement(inline);
// test.setText(inline);
}
public static void main(String args[]) {
Mail theMail = new Mail();
theMail.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
答案 0 :(得分:2)
由于您要发送HELO
而不是EHLO
,服务器可能无法启用对ESMTP命令AUTH
的支持。
答案 1 :(得分:0)
SMTP的RFC 5321要求HELO应包含FQDN(带域的主机名),具体取决于您使用的SMTP服务器可能因此而拒绝通信。此外,不赞成使用HELO来支持EHLO命令。
您是否考虑过使用Java Mail或Commons-email而不是自己进行所有协议处理?