这是java核心技术socket篇的一段代码
可是我发送邮件却收不到,哪位测试一下看是什么问题package mail;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.io.*;
import java.net.*;
/**
 * 发送邮件的格式
 * HELO 发送邮件的主机
 * MAIL FROM:<发送人的邮件>
 * RCPT TO:<收件人的邮件>
 * DATA
 * 正文内容
 * @author Tomoya
 *
 */
@SuppressWarnings("serial")
public class MailFrame extends JFrame { private JTextField to;
private JTextField from;
private JTextField smtpServer;
private JTextArea sendMsg;
private JTextArea comm;
private Scanner in;
private PrintWriter out;
private StringBuilder sb;
public MailFrame() {
int textlen = 15;
JPanel p = new JPanel();
p.setLayout(new GridBagLayout());
GridBagConstraints con = new GridBagConstraints();
con.insets = new Insets(5,5,5,5);

con.gridx = 0;
con.gridy = 0;
p.add(new JLabel("收   件   人:"), con);

con.gridx++;
to = new JTextField(textlen);
p.add(to, con);

con.gridx = 0;
con.gridy++;
p.add(new JLabel("发   送   人:"), con);

con.gridx++;
from = new JTextField(textlen);
p.add(from, con);

con.gridx = 0;
con.gridy++;
p.add(new JLabel("SMTP服务:"), con);

con.gridx++;
smtpServer = new JTextField(textlen);
p.add(smtpServer, con);

con.gridx = 0;
con.gridy++;
p.add(new JLabel("发送消息:"), con);

con.gridy++;
con.gridwidth = 2;
sendMsg = new JTextArea(5, 20);
p.add(new JScrollPane(sendMsg, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), con);

con.gridy++;
con.gridwidth = 1;
JButton send = new JButton("发送");
send.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
comm.setText("");
sendMail();
}
});
p.add(send, con);

con.gridy++;
p.add(new JLabel("收到消息:"), con);

con.gridy++;
con.gridwidth = 2;
comm = new JTextArea(5, 20);
p.add(new JScrollPane(comm, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), con);

add(p, BorderLayout.CENTER);
pack();
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void sendMail() {
try {
Socket so = new Socket(smtpServer.getText(), 25);
in = new Scanner(so.getInputStream());
out = new PrintWriter(so.getOutputStream());
String host = InetAddress.getLocalHost().getHostName();
sb = new StringBuilder();
//receive();
send("HELO" + host);
//receive();
send("MAIL FROM:<" + from.getText() + ">");
//receive();
send("RCPT TO:<" + to.getText() + ">");
//receive();
send("DATA");
//receive();
send(sendMsg.getText());
//receive();
send(".");
out.println(sb.toString());
out.flush();
so.close();
JOptionPane.showMessageDialog(this, "发送成功!");
} catch(Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "发送失败!");
}
}

public void send(String msg) throws IOException {
sb.append(msg);
sb.append("\n\r");
//comm.append(msg);
//comm.append("\n");
//out.print(msg.replaceAll("\n", "\n\r"));
//out.print("\n\r");
//out.flush();
}

public void receive() throws IOException {
if(in.hasNextLine()) {
comm.append(in.nextLine());
comm.append("\n");
}
}

public static void main(String[] args) {
new MailFrame();
}
}

解决方案 »

  1.   

    没时间看了,简单说思路,首先应该确定你的连接有没有问题,试着用打印语句调试一下连接的部分。其次应该确认你真的有发出去,看看IO的部分。最后在看看接收端的连接和IO部分,一般都能找到问题。
      

  2.   

    连接是没有问题的
    把receive的注释去掉的话,是可以收到一些回溃信息的
      

  3.   

    建议你用telnet直接连接邮件服务器,把整个命令过程记录下来,验证通过了再用程序实现。
    或者直接用javamail来做。
      

  4.   

    smtp里面是auth命令
    服务器会返回提示输入账户,输入后,服务器会再次提示输入密码,输入后,再执行其他发送邮件的命令。
    注意,服务器提示的输入账户和密码的信息是base64编码的。