程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> 這是個用APPLET發送E-MAIL的源代碼

這是個用APPLET發送E-MAIL的源代碼

編輯:JAVA編程入門知識

  都要用到JBuilder裡的一個組件,

  你可以去JBuilder目錄下拷,

  或自己用JDK的組件改一下

  NO 1:這個程序調用Foxmail發送信件

  

/*這個程序要用到Jbuilder的幾個類*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import borland.jbcl.layout.*;
import borland.jbcl.control.*;
import java.net.*;
public class EmailApplet extends Applet{
Button button;
TextField textField;
public EmailApplet(){}
public void init(){
try{
jbInit();}
catch(Exception e){
e.printStackTrace();}
}//end of init()
public void jbInit()throws Exception{
setLayout(new BorderLayout());
button=new Button("sendMail");
textField=new TextField();
add("North",textField);
add("South",button);
}//end of jbInit()
public boolean action(Event e,Object arg){
if(arg.equals("sendMail")){
try{
/*注意哦,這裡是主要代碼!!*/
String mailto=textField.getText();
URL url=new URL(mailto);
AppletContext ac=this.getAppletContext();
ac.showDocument(url);}
catch(Exception e2){
System.out.println(e2.toString());}
}//end of if
else{
return super.action(e,arg);}
return true;
}//end of action()
}//end of EmailApplet
NO 2:這個程序直接發送信件:
import borland.jbcl.layout.*;
import borland.jdbl.*;
import java.awt.*;
import java.applet.Applet;
import java.util.StringTokenizer;
import java.net.Socket;
import java.io.PrintStream;
import java.lang.Exception;
import java.awt.event.ActionEvent;
public class MailApplet extends Applet{
boolean isStandalone=false;
PaneLayout panelLayout1=new PaneLayout();
panel panel1=new Panel();
Label label1=new Label();
XYLayout xYLayout1=new XYLayout();
Label Label2=new Label();
TextField textField1=new TextField();
TextField textField2=new TextField();
Label lable3=new Label();
TextArea textArea1=new TextArea();
Button button1=new Button();
Button button2=new Button();
private int SMTP_PORT=25;
Frame frame=new Frame();
Color bgcolor=null;
String send="";
String get="";
public MailApplet(){}
public void init(){
try{
int c1=Integer.parseInt(getParameter("color1"));
int c2=Integer.parseInt(getParameter("color2"));
int c3=Integer.parseInt(getParameter("color3"));
bgcolor=new Color(c1,c2,c3);
SMTP_PORT=Integer.parseInt(getParameter("port"));
send=getParameter("send");
get=getParameter("get");
jbInit();}
catch(Exception e){
e.printStackTrace();}
}//end of init()
/*Component initialization*/
private void jbInit()throws Exception{
this.setSize(new Dimension(332,280));
panel1.setBackground(bgcolor);
label1.setAlignment(2);
label1.setText("收信人地址:");
label2.setAlignment(2);
label2.setText("寄信人地址:");
label3.setAlignment(2);
label3.setText("內容:");
button1.setLabel("發 送");
button1.addActionListener(new MailApplet_button1_actionAdapter(this));
button2.setLabel("清 除");
button2.addActionListener(new MailApplet_button2_actionAdapter(this));
panel1.setLayout(xYLayout);
this.setLayout(panelLayout1);
thsi.add(panel1,new PaneConstraints("panel1","panel1",PaneConstraints.ROOT,1.0f));
panel1.add(label1,new XYConstraints(10,5,80,20));
panel1.add(label2,new XYConstraints(10,30,80,20));
panel1.add(textField1,new XYConstraints(95,5,200,20));
panel1.add(textField2,new XYConstraints(95,30,200,20));
panel1.add(label3,new XYConstraints(10,60,80,20));
panel1.add(textArea1,new XYConstraints(10,80,300,150));
panel1.add(button1,new XYConstraints(50,240,80,25));
panel1.add(button2,new XYConstraints(160,240,80,25));
textField1.setText(get);
textField2.setText(send);
}//end of jbInit()
public String getAppletInfo(){
return "Applet Information";}
public String[][] getParameteInfo(){
return null;}
void sendMail(){
String sender=textField2.getText();
String geter=textField1.getText();
String memo=textArea1.getText();
StringTokenizer st=new StringTokenizer(sender,"@");
int count=st.countTokens();
if(count!=2){
MessageDialog mdlg=new MessageDialog(frame,"郵件地址錯誤","你的發信人地址錯誤,請確認後重新發送!!",1);
mdlg.show();
textField2.selectAll();
return;}
String senderName=st.nextToken();
String senderHost=st.nextToken();
st=new StringTokenizer(geter,"@");
count=st.countTokens();
if(count!=2){
MessageDialog mdlg=new MessageDialog(frame,"郵件地址錯誤","你的收信人地址錯誤,請確認後重新發送!!",1);
mdlg.show();
textField1.selectAll();
return;}
String geterName=st.nextToken();
String geterHost=st.nextToken();
try{
Socket s=new Socket(geterHost,SMTP_PORT);
PrintStream out=new PrintStream(s.getOutputStream());
out.println("HELLO:"+senderHost);
/*郵件服務器不認證讀者所輸SMTP是否正確*/
out.println("MAIL FORM:"+senderName);
out.println("RCPT TO:"+geterName);
out.println("DATA");
out.println(memo);
out.println(".\nQUIT");
MessageDialog mdlg=new MessageDialog(frame,"郵件地址正確","恭喜,您的郵件已經成功發送",1);
mdlg.show();}
catch(Exception e){
System.out.println(e.toString());
MessageDialog mdlg=new MessageDialog(frame,"郵件發送失敗",e.toString(),1);//"郵件發送出錯,請確認合重新發送!",1);
mdlg.show();
return;}
}//end of sendMail()
void button1_actionPerformed(ActionEvent e){
sendMail();
textArea1.setText("");}
void button2_actionPerformed(ActionEvent e){
textField1.setText("");
textFIeld2.setText("");
textArea1.setText("");}
}//end of MailApplet
class MailApplet_button1_actionAdapter implements java.awt.event.ActionListener{
MailApplet adaptee;
MailApplet_button1_actionAdapter(MailApplet adaptee){
this.adaptee=adaptee;}
public void actionPerformed(ActionEvent e){
adaptee.button1_actionPerformed(e);}
}//end of this
class MailApplet_button2_actionAdapter implements java.awt.event.ActionListener{
MailApplet adaptee;
MailApplet_button2_actionAdapter(MailApplet adaptee){
this.adaptee=adaptee;}
public void actionPerformed(ActionEvent e){
adaptee.button2_actionPerformed(e);}
}//end of this

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved