借网上一位仁兄的代码:
import javax.swing.*;
import java.awt.*;
import java.net.Socket;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.io.IOException;
import java.util.StringTokenizer;
import java.awt.event.*;/**
* <p>Title: Java Core 2 Learning</p>
* <p>Description: </p>
* <p>Copyright: ERIC Ltd. Co Copyright (c) 2004</p>
* <p>Company: </p>
* @author ERIC
* @version 1.0
*/public class MailTest
extends JFrame {
GridBagLayout gridBagLayout1 = new GridBagLayout();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JTextField from = new JTextField();
JTextField to = new JTextField();
JTextField smtpServer = new JTextField();
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea message = new JTextArea();
JScrollPane jScrollPane2 = new JScrollPane();
JButton sendButton = new JButton();
JTextArea communication = new JTextArea();
String user;
String pwd;private PrintWriter out;
private BufferedReader in;
JLabel labelSubject = new JLabel();
JTextField subject = new JTextField();
public MailTest() {
try {
jbInit();
sendButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Thread() {
public void run() {
sendMail();
}
}.start();
}
}
);
//user = new String(javacore2.Base64.encode("your user name".getBytes()));
//pwd = new String(javacore2.Base64.encode("your password".getBytes()));
user = "ZXJpYy1oeHk=";
pwd = "RVJJQ0NBUk9MMDczMQ==";
}
catch (Exception ex) {
ex.printStackTrace();
}
}private void sendMail() {
try {
Socket s = new Socket(smtpServer.getText(), 25); //smtp port is 25
out = new PrintWriter(s.getOutputStream());
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
String hostName = InetAddress.getLocalHost().getHostName();
receive();
send("HELO " + hostName);
receive();
send("AUTH LOGIN");
receive();
send(user);
receive();
send(pwd);
receive();
send("MAIL FROM: <" + from.getText() + ">");
receive();
send("RCPT TO: <" + to.getText() + ">");
receive();
send("DATA");
receive();
send("SUBJECT:"+subject.getText());
//receive();
StringTokenizer t = new StringTokenizer(
message.getText(),
"\n");
while (t.hasMoreTokens()) {
send(t.nextToken());
}
send("\r\n.");
receive();
s.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}

解决方案 »

  1.   

    private void receive() throws IOException {
    String line = in.readLine();
    if (line != null) {
    communication.append(line + "\n");
    }
    }private void send(String s) throws IOException {
    communication.append(s + "\n");
    out.print(s);
    out.print("\r\n"); //SMTP must end every line with \r\n
    out.flush();
    }void jbInit() throws Exception {
    jLabel1.setRequestFocusEnabled(true);
    jLabel1.setText("From:");
    this.getContentPane().setLayout(gridBagLayout1);
    jLabel2.setText("To:");
    jLabel3.setText("SMTP Server:");
    from.setMinimumSize(new Dimension(20, 22));
    from.setPreferredSize(new Dimension(20, 22));
    from.setText("[email protected]");
    to.setText("[email protected]");
    smtpServer.setText("smtp.163.com");
    sendButton.setText("Send");
    communication.setText("");
    labelSubject.setText("Subject:");
    subject.setText("jTextField1");
    subject.setText("hello");
    this.getContentPane().add(jLabel1,
    new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
    ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    this.getContentPane().add(jLabel2,
    new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
    ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    this.getContentPane().add(jLabel3,
    new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0
    ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    this.getContentPane().add(from,
    new GridBagConstraints(1, 0, 1, 1, 100.0, 0.0
    ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    this.getContentPane().add(to, new GridBagConstraints(1, 1, 1, 1, 100.0, 0.0
    ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    this.getContentPane().add(smtpServer,
    new GridBagConstraints(1, 3, 1, 1, 100.0, 0.0
    ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    this.getContentPane().add(jScrollPane1,
    new GridBagConstraints(0, 4, 2, 1, 0.0, 100.0
    ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    this.getContentPane().add(jScrollPane2,
    new GridBagConstraints(0, 5, 2, 1, 0.0, 100.0
    ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    this.getContentPane().add(sendButton,
    new GridBagConstraints(0, 6, 2, 1, 0.0, 0.0
    ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
    this.getContentPane().add(labelSubject, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
    ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    jScrollPane1.getViewport().add(message, null);
    jScrollPane2.getViewport().add(communication, null);
    this.getContentPane().add(subject, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
    ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    }public static void main(String[] args) {
    MailTest mailTest = new MailTest();
    mailTest.setLocation(100, 100);
    mailTest.setSize(300, 300);
    mailTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mailTest.show();
    }
    }
    出自http://community.csdn.net/Expert/TopicView3.asp?id=3465074
    但是163不行,换个服务器可以