郵件客戶機(jī)分析_第1頁
郵件客戶機(jī)分析_第2頁
郵件客戶機(jī)分析_第3頁
郵件客戶機(jī)分析_第4頁
郵件客戶機(jī)分析_第5頁
已閱讀5頁,還剩36頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、電子郵件流程電子郵件流程用戶A用戶B服務(wù)器A服務(wù)器B1、用戶A通過郵件客戶端發(fā)送郵件到服務(wù)器A2、服務(wù)器A將郵件發(fā)送到服務(wù)器B3、用戶B接受服務(wù)器B上的郵件用戶A郵件發(fā)送過程 用戶A客戶端首先和服務(wù)器A建立TCP連接 確認(rèn)之后,用戶A和服務(wù)器A之間采用SMTP協(xié)議發(fā)送郵件內(nèi)容 郵件內(nèi)容傳輸完畢后,發(fā)送結(jié)束郵件客戶端JAVA程序該程序分為4部分,分別為mailclient、envelope、message、smtpconnectionMail cilent為客戶端主程序,包括使用界面、按鍵的定義,整個的發(fā)送流程中類的創(chuàng)建message為發(fā)送郵件的內(nèi)容部分,包含有發(fā)件人、收件人等內(nèi)容envelop

2、e為用于smtp協(xié)議的信息傳遞,包含發(fā)送接收信息以及message信息smtpconnection為發(fā)件過程中和smtp連接的建立以及關(guān)閉發(fā)送過程中使用的指令HELO 250MAIL FROM 250RCPT TO 250DATA 354QUIT 221Mail Clientimport java.io.*;import javax.*;import java.awt.*;import java.awt.event.*;public class MailClient extends Frame private Button btSend = new Button(Send);private B

3、utton 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 =

4、 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 MailClient wind

5、ow 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 =

6、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

7、.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 GridLayout(0,

8、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

9、 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 public v

10、oid 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(Nee

11、d name of local mailserver!);return;/* 確認(rèn)發(fā)送者和接收者的郵件地址正確 */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(

12、),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(); catch (UnknownH

13、ostException 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;System.out.pri

14、ntln(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 QuitListener

15、 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 CRLF = rn;/* Cr

16、eate 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 += Subject: + subje

17、ct.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 access the sende

18、r and recipient. */public String getFrom() return From;public String getTo() return To;/*檢查信息的有效性,發(fā)送者和接受者的地址 * contains only one x-sign. */public boolean isValid() int fromat = From.indexOf();int toat = To.indexOf();if (fromat 1 | (From.length() - fromat) = 1) System.out.println(Sender address is in

19、valid.);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;/*檢查信息的有效性,發(fā)送者和接受者的地址 * contains only one x-sign. */public boolean isValid() int fro

20、mat = 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() System.out.prin

21、tln(Sender address is invalid.);return false;if (toat != To.lastIndexOf(x) 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.*;import javax.

22、*;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. */public Envelope(Mess

23、age 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 DestAddr = InetAdd

24、ress.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, true);while (parse

25、r.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: + Recipient +n;res += MX-ho

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

27、;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 associated streams. Initi

28、alize * 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();/* Read a line f

29、rom 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 handshake command. */

30、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 correct order. No c

31、hecking 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 FROM: + envelope.S

32、ender + 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() isConnected = false;try /

33、*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. */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 IOException(Server re

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論