邮件客户机分析_第1页
邮件客户机分析_第2页
邮件客户机分析_第3页
邮件客户机分析_第4页
邮件客户机分析_第5页
已阅读5页,还剩36页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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

2、e为用于smtp协议的信息传递,包含发送接收信息以及message信息smtpconnection为发件过程中和smtp连接的建立以及关闭发送过程中使用的指令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;/* 确认发送者和接收者的邮件地址正确 */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;/*检查信息的有效性,发送者和接受者的地址 * 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;/*检查信息的有效性,发送者和接受者的地址 * 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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论