📄 sendmail.java
字号:
import java.io.*;
import java.awt.*;
import java.net.*;
class sendmail extends Frame
{
PrintStream ps=null;
DataInputStream dis=null;
String strmailfrom,strmailto,strsubject,strbody;
String strbodyend="\r\n. \r\n";
TextArea taletter,tastatus;
TextField tfmailfrom,tfmailto,tfsubject;
Label Ibmailfrom,Ibmailto,Ibsubject,Ibletter,Ibstatus;
Button btini,btsend;
Socket smtp=null;
public void send(String str) throws IOException
{
ps.println(str);
ps.flush();
tastatus.appendText("Java send:"+str+"\n");
}
public void receive() throws IOException
{
String readstr=dis.readLine();
tastatus.appendText("Java receive:"+readstr+"\n");
}
public static void main(String args[])
{
sendmail sm=new sendmail();
sm.show();
}
public sendmail()
{
setLayout(new FlowLayout());
Ibmailfrom=new Label("从地址:");
tfmailfrom=new TextField(" ",50);
Ibmailto=new Label("发送到:");
tfmailto=new TextField(" ",50);
Ibsubject=new Label("标题:");
tfsubject=new TextField(50);
Ibletter=new Label("信件内容:");
Ibstatus=new Label("发送邮件状态:");
add(Ibmailfrom);
add(tfmailfrom);
add(Ibmailto);
add(tfmailto);
btsend=new Button("SEND!");
btini=new Button("Initial");
tastatus=new TextArea(8,50);
tastatus.setEditable(false);
taletter=new TextArea(10,50);
add(Ibstatus);
add(tastatus);
add(btsend);
add(btini);
add(Ibsubject);
add(tfsubject);
setTitle("发电子邮件");
resize(600,600);
}
public boolean action(Event evt,Object obj)
{
if(evt.target==btsend)
{
tastatus.appendText("Now Start to send.\n");
try{
String loc=InetAddress.getLocalHost().getHostName();
send("HELO"+loc);
receive();
send("MAIL FROM:"+strmailfrom);
receive();
send("RECP TO:"+strmailto);
receive();
send("DATA");
receive();
send("SUBJECT"+strsubject);
receive();
send(strbody+strbodyend);
receive();
smtp.close();
}catch(IOException e){tastatus.appendText("Error sending!\n");}
tastatus.appendText("Send Complete!\n");
}
else if(evt.target==btini)
{
tastatus.appendText("Now Start to connet.\n");
try{
smtp=new Socket("center.njtu.edu.cn",25);
OutputStream os=smtp.getOutputStream();
ps=new PrintStream(os);
InputStream is=smtp.getInputStream();
dis=new DataInputStream(is);
tastatus.appendText("Connet complete!\n");
}catch(IOException e){tastatus.appendText("Error sending!\n");}
strmailfrom=tfmailfrom.getText();
strmailto=tfmailto.getText();
strsubject=tfsubject.getText();
strbody=taletter.getText();
}
return super.action(evt,obj);
}
public boolean handleEvent(Event e)
{
if(e.id==Event.WINDOW_DESTROY){System.exit(0);}
return super.handleEvent(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -