java:收发邮件代码 看看吧~

vae819723280 2010-10-16 01:00:33
这是代码:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;
import java.io.*;
import javax.swing.*;

public class MailTest {
public static void main(String[] args) {
Frame frame = new MailTestFrame();
frame.show();
}
}

class MailTestFrame extends Frame implements ActionListener {
private BufferedReader in;
private PrintWriter out;
private TextField from;
private TextField to;
private TextField smtpServer;
private TextArea message;
private TextArea response;

public MailTestFrame() {
setTitle("MailTest");
setSize(300, 300);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

getContentPane().setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
// 当容器比组件要大时,只调整垂直方向组件的大小
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 0;
gbc.weighty = 0;

gbc.weightx = 0;
add(new Label("发送地址:"), gbc, 0, 0, 1, 1);
gbc.weightx = 100;
from = new TextField(20);
add(from, gbc, 1, 0, 1, 1);

gbc.weightx = 0;
add(new Label("收件地址:"), gbc, 0, 1, 1, 1);
gbc.weightx = 100;
to = new TextField(20);
add(to, gbc, 1, 1, 1, 1);

gbc.weightx = 0;
add(new Label("SMTP服务器:"), gbc, 0, 2, 1, 1);
gbc.weightx = 100;
smtpServer = new TextField(20);
add(smtpServer, gbc, 1, 2, 1, 1);

gbc.fill = GridBagConstraints.BOTH;
gbc.weighty = 100;
message = new TextArea();
add(new ScrollPane(), gbc, 0, 3, 2, 1);

response = new TextArea();
add(new ScrollPane(), gbc, 0, 4, 2, 1);

gbc.weighty = 0;
Button sendButton = new Button("发送");
sendButton.addActionListener(this);
Panel buttonPanel = new Panel();
buttonPanel.add(sendButton);
add(buttonPanel, gbc, 0, 5, 2, 1);
}

private Container getContentPane() {
// TODO Auto-generated method stub
return null;
}

private void add(Component c, GridBagConstraints gbc, int x, int y, int w,
int h) {
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = w;
gbc.gridheight = h;
getContentPane().add(c, gbc);
}

public void actionPerformed(ActionEvent evt) {
// 异步机制,使得当所有的awt事件发生后才进行以下工作
SwingUtilities.invokeLater(new Runnable() {
public void run() {
sendMail();
}
});
}

public void sendMail() {
try {
Socket s = new Socket(smtpServer.getText(), 25);
out = new PrintWriter(s.getOutputStream());
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
String hostName = InetAddress.getLocalHost().getHostName();

send(null);
// 传送本机域名
send("HELO " + hostName);
// 传送发信者信箱名称
send("MAIL FROM: " + from.getText());
// 传送收信者信箱名称
send("RCPT TO: " + to.getText());
// 发送信箱数据,包括信头和信体
send("DATA");
out.println(message.getText());
// 发送结束标志
send(".");
s.close();
} catch (IOException exception) {
response.append("Error: " + exception);
}
}

public void send(String s) throws IOException {
if (s != null) {
response.append(s + "\n");
out.println(s);
out.flush();
}
String line;
if ((line = in.readLine()) != null)
response.append(line + "\n");
}
}




错误在
public void actionPerformed(ActionEvent evt) {
// 异步机制,使得当所有的awt事件发生后才进行以下工作
SwingUtilities.invokeLater(new Runnable() {
public void run() {
sendMail();
}
});
}


提示是:Multiple markers at this line
- Access restriction: The method invokeLater(Runnable) from the type SwingUtilities is not accessible due to restriction on
required library C:\Program Files\Java\jdk1.7.0\jre\lib\rt.jar
- Access restriction: The type SwingUtilities is not accessible due to restriction on required library C:\Program Files\Java
\jdk1.7.0\jre\lib\rt.jar


请问是rt.jar的问题,还是代码问题?帮忙解答下啊~~
...全文
138 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
vae819723280 2010-10-18
  • 打赏
  • 举报
回复
我想问smtp服务器是啥?运行出来了~呵呵~谢谢~
vae819723280 2010-10-18
  • 打赏
  • 举报
回复
DATA
503 bad sequence of commands
.
502 Error: command not implemented
sendMail() done


这是啥意思? command not implemented
masterz 2010-10-17
  • 打赏
  • 举报
回复
你的代码在jdk 1.6下有点问题。用下面的在jdk6下面跑一下试试。
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;
import java.io.*;
import javax.swing.*;

public class MailTest {
public static void main(String[] args) {
Frame frame = new MailTestFrame();
frame.show();
}
}

class MailTestFrame extends Frame implements ActionListener {
private BufferedReader in;
private PrintWriter out;
private TextField from;
private TextField to;
private TextField smtpServer;
private TextArea message;
private TextArea response;
public MailTestFrame() {
setTitle("MailTest");
setSize(300, 300);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
// 当容器比组件要大时,只调整垂直方向组件的大小
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 0;
gbc.weighty = 0;

gbc.weightx = 0;
add(new Label("发送地址:"), gbc, 0, 0, 1, 1);
gbc.weightx = 100;
from = new TextField(20);
add(from, gbc, 1, 0, 1, 1);

gbc.weightx = 0;
add(new Label("收件地址:"), gbc, 0, 1, 1, 1);
gbc.weightx = 100;
to = new TextField(20);
add(to, gbc, 1, 1, 1, 1);

gbc.weightx = 0;
add(new Label("SMTP服务器:"), gbc, 0, 2, 1, 1);
gbc.weightx = 100;
smtpServer = new TextField(20);
add(smtpServer, gbc, 1, 2, 1, 1);

gbc.fill = GridBagConstraints.BOTH;
gbc.weighty = 100;
message = new TextArea();
add(message, gbc, 0, 3, 2, 1);

response = new TextArea();
add(response, gbc, 0, 4, 2, 1);

gbc.weighty = 0;
Button sendButton = new Button("发送");
sendButton.addActionListener(this);
Panel buttonPanel = new Panel();
buttonPanel.add(sendButton);
add(buttonPanel, gbc, 0, 5, 2, 1);
}

private void add(Component c, GridBagConstraints gbc, int x, int y, int w,
int h) {
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = w;
gbc.gridheight = h;
add(c, gbc);
}

public void actionPerformed(ActionEvent evt) {
// 异步机制,使得当所有的awt事件发生后才进行以下工作
// java.awt.EventQueue.invokeLater(new Runnable() {
// public void run() {
// sendMail();
// }
// });


SwingUtilities.invokeLater(new Runnable() {
public void run() {
sendMail();
}
});
}

public void sendMail() {
println("sendMail() start");
try {
Socket s = new Socket(smtpServer.getText(), 25);
out = new PrintWriter(s.getOutputStream());
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
String hostName = InetAddress.getLocalHost().getHostName();

send(null);
// 传送本机域名
send("HELO " + hostName);
// 传送发信者信箱名称
send("MAIL FROM: " + from.getText());
// 传送收信者信箱名称
send("RCPT TO: " + to.getText());
// 发送信箱数据,包括信头和信体
send("DATA");
out.println(message.getText());
// 发送结束标志
send(".");
s.close();
println("sendMail() done");
} catch (IOException exception) {
response.append("Error: " + exception);
}
}

public void send(String s) throws IOException {
if (s != null) {
response.append(s + "\n");
out.println(s);
out.flush();
}
String line;
if ((line = in.readLine()) != null)
response.append(line + "\n");
}
private void println(String s)
{
response.append(s + "\n");
}
}

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧