邮件客户机分析要点课件

上传人:大米 文档编号:570183377 上传时间:2024-08-02 格式:PPT 页数:41 大小:2MB
返回 下载 相关 举报
邮件客户机分析要点课件_第1页
第1页 / 共41页
邮件客户机分析要点课件_第2页
第2页 / 共41页
邮件客户机分析要点课件_第3页
第3页 / 共41页
邮件客户机分析要点课件_第4页
第4页 / 共41页
邮件客户机分析要点课件_第5页
第5页 / 共41页
点击查看更多>>
资源描述

《邮件客户机分析要点课件》由会员分享,可在线阅读,更多相关《邮件客户机分析要点课件(41页珍藏版)》请在金锄头文库上搜索。

1、电子邮件发件实验电子邮件发件实验电子邮件流程电子邮件流程用户A用户B服务器A服务器B1、用户A通过邮件客户端发送邮件到服务器A2、服务器A将邮件发送到服务器B3、用户B接受服务器B上的邮件用户A邮件发送过程用户A客户端首先和服务器A建立TCP连接确认之后,用户A和服务器A之间采用SMTP协议发送邮件内容邮件内容传输完毕后,发送结束邮件客户端JAVA程序该程序分为4部分,分别为mailclient、envelope、message、smtpconnectionMailcilent为客户端主程序,包括使用界面、按键的定义,整个的发送流程中类的创建message为发送邮件的内容部分,包含有发件人、收

2、件人等内容envelope为用于smtp协议的信息传递,包含发送接收信息以及message信息smtpconnection为发件过程中和smtp连接的建立以及关闭发送过程中使用的指令HELO 250MAIL FROM 250RCPT TO 250DATA 354QUIT 221Mail Clientimport java.io.*;import .*;import java.awt.*;import java.awt.event.*;public class MailClient extends Frame private Button btSend = new Button(Send);pr

3、ivate 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 to

4、Label = new Label(To:);private TextField toField = new TextField(, 40);private Label subjectLabel = new Label(Subject:);private TextField subjectField = new TextField(, 40);private Label messageLabel = new Label(Message:);private TextArea messageText = new TextArea(10, 40); /* * Create a new MailCli

5、ent 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 subject

6、Panel = 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, Bord

7、erLayout.WEST);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 GridLa

8、yout(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.addActionList

9、ener(new ClearListener();btQuit.addActionListener(new QuitListener();buttonPanel.add(btSend);buttonPanel.add(btClear);buttonPanel.add(btQuit);/* Add, pack, and show */add(fieldPanel, BorderLayout.NORTH);add(messagePanel, BorderLayout.CENTER);add(buttonPanel, BorderLayout.SOUTH);pack();show(); static

10、 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.p

11、rintln(Need name of local mailserver!);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(), toFiel

12、d.getText(),subjectField.getText(),messageText.getText(); /* 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(); catc

13、h (UnknownHostException e) /* If there is an error, do not go further */System.out.println(Unknown host!);return;try SMTPConnection connection = new SMTPConnection(envelope);connection.send(envelope);connection.close(); catch (IOException error) System.out.println(Sending failed: + error);return;Sys

14、tem.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();messageText.setText(); /* Quit */class

15、QuitListener implements ActionListener public void actionPerformed(ActionEvent e) System.exit(0);Messageimport 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 C

16、RLF = rn; /* Create the message object by inserting the required headers from RFC 822 (From, To, Date). */public Message(String from, String to, String subject, String text) /* Remove whitespace */From = from.trim();To = to.trim();Headers = From: + From + CRLF;Headers += To: + To + CRLF;Headers += S

17、ubject: + subject.trim() + CRLF;/* A close approximation of the required format. Unfortunately only GMT. */SimpleDateFormat format = new SimpleDateFormat(EEE, dd MMM yyyy HH:mm:ss GMT);String dateString = format.format(new Date();Headers += Date: + dateString + CRLF;Body = text; /* Two functions to

18、access the sender and recipient. */public String getFrom() return From; public String getTo() return To; /*检查信息的有效性,发送者和接受者的地址 * contains only one -sign. */public boolean isValid() int fromat = From.indexOf();int toat = To.indexOf();if (fromat 1 | (From.length() - fromat) = 1) System.out.println(Sen

19、der address is invalid.);return false;if (toat 1 | (To.length() - toat) = 1) System.out.println(Recipient address is invalid.);return false;if (fromat != From.lastIndexOf() System.out.println(Sender address is invalid.);return false;/*检查信息的有效性,发送者和接受者的地址 * contains only one -sign. */public boolean i

20、sValid() int fromat = From.indexOf();int toat = To.indexOf();if (fromat 1 | (From.length() - fromat) = 1) System.out.println(Sender address is invalid.);return false;if (toat 1 | (To.length() - toat) = 1) System.out.println(Recipient address is invalid.);return false;if (fromat != From.lastIndexOf()

21、 System.out.println(Sender address is invalid.);return false;if (toat != To.lastIndexOf() System.out.println(Recipient address is invalid.);return false;return true; /* For printing the message. */ public String toString() String res;res = Headers + CRLF;res += Body;return res;Envelopeimport java.io

22、.*;import .*;import java.util.*;public class Envelope public String Sender; /* SMTP-recipient, or contents of To-header. */public String Recipient; /* Target MX-host */public String DestHost;public InetAddress DestAddr; /* The actual message. */public Message Message; /* Create the envelope. */publi

23、c Envelope(Message message, String localServer) throws UnknownHostException /* Get sender and recipient. */Sender = message.getFrom();Recipient = message.getTo();Message = escapeMessage(message);/* Take the name of the local mailserver and map it into an Inet Address */DestHost = localServer;try Des

24、tAddr = InetAddress.getByName(DestHost); catch (UnknownHostException e) System.out.println(Unknown Host: + DestHost);System.out.println(e);throw e;return; private Message escapeMessage(Message message) String escapeBody = ;String token;StringTokenizer parser = new StringTokenizer(message.Body, n, tr

25、ue);while (parser.hasMoreTokens() token = parser.nextToken();if (token.startsWith(.) token = . + token;escapeBody += token;message.Body = escapeBody;return message; /* For printing the envelope. Only for debug. */public String toString() String res = Sender: + Sender + n;res += Recipient: + Recipien

26、t +n;res += MX-host: + DestHost + , address: + DestAddr + n;res += Message: + n;res += Message.toString();return res;SMTPConnectionimport .*;import java.io.*;import java.util.*;public class SMTPConnection private Socket connection; /* Streams for reading and writing the socket */private BufferedRead

27、er fromServer;private DataOutputStream toServer; private static final int SMTP_PORT = 25;private static final String CRLF = rn; /* Are we connected? Used in close() to determine what to do. */private boolean isConnected = false; /* Create an SMTPConnection object. Create the socket and the associate

28、d streams. Initialize * SMTP connection. */public SMTPConnection(Envelope envelope) throws IOException connection = new Socket(envelope.DestAddr, SMTP_PORT);fromServer = new BufferedReader(new InputStreamReader(connection.getInputStream();toServer = new DataOutputStream(connection.getOutputStream();

29、/* Read a line from server and check that the reply code is 220. If not, throw an IOException. */String reply = fromServer.readLine();if (reply.startsWith(220) else throw new IOException(Server reply + reply);/* SMTP handshake. We need the name of the local machine. * Send the appropriate SMTP hands

30、hake command. */String localhost = envelope.DestHost;try sendCommand(HELO + localhost, 250); catch (IOException error) System.out.println(error);System.exit(1); catch (Exception e) System.out.println(e);System.exit(1);isConnected = true; /* Send the message. Write the correct SMTP-commands in the co

31、rrect order. No checking for errors, * just throw them to the caller. */public void send(Envelope envelope) throws IOException /* Send all the necessary commands to send a message. Call sendCommand() to do the dirty * work. Do _not_ catch the exception thrown from sendCommand(). */sendCommand(MAIL F

32、ROM: + envelope.Sender + CRLF, 250);sendCommand(RCPT TO: + envelope.Recipient + CRLF, 250);sendCommand(DATA + CRLF + envelope.Message.Headers + envelope.Message.Body + CRLF + . + CRLF, 354); /* Close the connection. First, terminate on SMTP level, then close the socket. */public void close() isConne

33、cted = false;try /*sendCommand(QUIT, 221);*/sendCommand(QUIT, 250);connection.close(); catch (IOException e) System.out.println(Unable to close connection: + e);isConnected = true; /* Send an SMTP command to the server. Check that the reply code is what is is supposed to be * according to RFC 821. *

34、/private void sendCommand(String command, int rc) throws IOException String reply;toServer.writeBytes(command);toServer.writeBytes(CRLF);System.out.println(Me: + command + n);reply = fromServer.readLine();System.out.println(Server: + reply + n);if(!reply.startsWith(String.valueOf(rc) throw new IOExc

35、eption(Server reply: + reply); /*if(rc != parseReply(reply) throw new IOException(Server reply + reply);*/*if (command.equalsIgnoreCase(HELO) & rc != 250) | (command.equalsIgnoreCase(MAIL FROM) & rc != 250) | (command.equalsIgnoreCase(RCPT TO) & rc != 250)| (command.equalsIgnoreCase(DATA) & rc != 35

36、4)| (command.equalsIgnoreCase(QUIT) & rc != 221) throw new IOException(Server replys + rc);*/ /* Parse the reply line from the server. Returns the reply code. */private int parseReply(String reply) return Integer.parseInt(reply); /* Destructor. Closes the connection if something bad happens. */prote

37、cted void finalize() throws Throwable if (isConnected) close();super.finalize();Eclipse程序运行过程程序运行过程ESMTPESMTP,英文全称是“Extended SMTP”,顾名思义,扩展SMTP就是对标准SMTP协议进行的扩展本地本地SMTP服务器的搭建服务器的搭建采用一个smtp服务器软件,可以搭建本地smtp服务器,完成发件过程SSLSSL(Secure Sockets Layer 安全套接层),及其继任者传输层安全(Transport Layer Security,TLS)是为网络通信提供安全及数据完整性的一种安全协议。TLS与SSL在传输层对网络连接进行加密。SSL协议提供的服务主要有: 1)认证用户和服务器,确保数据发送到正确的客户机和服务器; 2)加密数据以防止数据中途被窃取; 3)维护数据的完整性,确保数据在传输过程中不被改变。 HttpsnHTTPS(Secure Hypertext Transfer Protocol)安全超文本传输协议 nhttps是以安全为目标的HTTP通道,简单讲是HTTP的安全版。即HTTP下加入SSL层,https的安全基础是SSL

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 工作计划

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号