资源描述
,*,*,*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,*,*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,*,*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,*,*,电子邮件发件实验,电子邮件发件实验,1,电子邮件流程,用户A,用户B,服务器A,服务器B,1、用户A通过邮件客户端发送邮件到服务器A,2、服务器A将邮件发送到服务器B,3、用户B接受服务器B上的邮件,电子邮件流程用户A用户B服务器A服务器B1、用户A通过邮件客,2,用户A邮件发送过程,用户A客户端首先和服务器A建立,TCP连接,确认之后,用户A和服务器A之间,采用SMTP协议发送邮件内容,邮件内容传输完毕后,发送结束,用户A邮件发送过程用户A客户端首先和服务器A建立TCP连接,3,邮件客户端JAVA程序,该程序分为4部分,分别为mailclient、envelope、message、smtpconnection,Mail cilent为客户端主程序,包括,使用界面、按键的定义,,整个的发送流程中类的创建,message为发送邮件的,内容,部分,包含有,发件人,、,收件人,等内容,envelope为,用于smtp协议的信息传递,,包含发送接收信息以及message信息,smtpconnection为发件过程中和,smtp连接的建立以及关闭,邮件客户端JAVA程序该程序分为4部分,分别为mailcli,4,发送过程中使用的指令,HELO 250,MAIL FROM 250,RCPT TO 250,DATA 354,QUIT 221,发送过程中使用的指令HELO 250,5,Mail Client,import java.io.*;,import .*;,import java.awt.*;,import java.awt.event.*;,public class MailClient extends Frame,private Button btSend=new Button(Send);,private Button btClear=new Button(Clear);,private Button btQuit=new Button(Quit);,private Label serverLabel=new Label(Local mailserver:);,private TextField serverField=new TextField(,40);,private Label fromLabel=new Label(From:);,private TextField fromField=new TextField(,40);,private Label toLabel=new Label(To:);,private TextField toField=new TextField(,40);,private Label subjectLabel=new Label(Subject:);,private TextField subjectField=new TextField(,40);,Mail Clientimport java.io.*;,6,private Label messageLabel=new Label(Message:);,private TextArea messageText=new TextArea(10,40);,/*,*Create a new MailClient window with fields for entering all the relevant,*information(From,To,Subject,and message).,*/,public MailClient(),super(Java MailClient);,Panel serverPanel=new Panel(new BorderLayout();,Panel fromPanel=new Panel(new BorderLayout();,Panel toPanel=new Panel(new BorderLayout();,Panel subjectPanel=new Panel(new BorderLayout();,Panel messagePanel=new Panel(new BorderLayout();,serverPanel.add(serverLabel,BorderLayout.WEST);,serverPanel.add(serverField,BorderLayout.CENTER);,fromPanel.add(fromLabel,BorderLayout.WEST);,fromPanel.add(fromField,BorderLayout.CENTER);,toPanel.add(toLabel,BorderLayout.WEST);,private Label messageLabel=,7,toPanel.add(toField,BorderLayout.CENTER);,subjectPanel.add(subjectLabel,BorderLayout.WEST);,subjectPanel.add(subjectField,BorderLayout.CENTER);,messagePanel.add(messageLabel,BorderLayout.NORTH);,messagePanel.add(messageText,BorderLayout.CENTER);,Panel fieldPanel=new Panel(new GridLayout(0,1);,fieldPanel.add(serverPanel);,fieldPanel.add(fromPanel);,fieldPanel.add(toPanel);,fieldPanel.add(subjectPanel);,/*Create a panel for the buttons and listeners to the buttons.*/,Panel buttonPanel=new Panel(new GridLayout(1,0);,btSend.addActionListener(new SendListener();,btClear.addActionListener(new ClearListener();,btQuit.addActionListener(new QuitListener();,buttonPanel.add(btSend);,buttonPanel.add(btClear);,buttonPanel.add(btQuit);,toPanel.add(toField,BorderL,8,/*Add,pack,and show*/,add(fieldPanel,BorderLayout.NORTH);,add(messagePanel,BorderLayout.CENTER);,add(buttonPanel,BorderLayout.SOUTH);,pack();,show();,static public void main(String argv),new MailClient();,/*Handler for the Send-button.*/,class SendListener implements ActionListener,public void actionPerformed(ActionEvent event),System.out.println(Sending mail);,/*Check that we have the local mailserver*/,if(serverField.getText().equals(),System.out.println(Need name of local mailserver!);,9,return;,/*,确认发送者和接收者的邮件地址正确,*/,if(fromField.getText().equals(),System.out.println(Need sender!);,return;,if(toField.getText().equals(),System.out.println(,Need recipient!,);,return;,/*Create the message*/,Message mailMessage=new Message(fromField.getText(),toField.getText(),subjectField.getText(),messageText.getText();,return;,10,/*Check that the message is valid,i.e.,sender and recipient addresss look ok.*/,if(!mailMessage.isValid(),System.out.println(Mail is not valid!);,return;,Envelope envelope;,try,envelope=new Envelope(mailMessage,serverField.getText();,catch(UnknownHostException e),/*If there is an error,do not go further*/,System.out.println(Unknown host!);,return;,/*Check that the message i,11,try,SMTPConnection connection=new SMTPConnection(envelope);,connection.send(envelope);,connection.close();,catch(IOException error),System.out.println(Sending failed:+error);,return;,System.out.println(Mail send successfully!);,/*Clear the fields on the GUI.*/,class ClearListener implements ActionListener,public void actionPerformed(ActionEvent e),System.out.println(Clearing fields);,fromField.setText();,toField.setText();,subjectField.setText();,try,12,messageText.setText();,/*Quit*/,class QuitListener implements ActionListener,public void actionPerformed(ActionEvent e),System.exit(0);,messageText.setText();,13,Message,import java.util.*;,import java.text.*;,public class Message,public String Headers;,public String Body;,private String From;,private String To;,/*To make it look nicer*/,private static final String CRL
展开阅读全文