版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、电子邮件发件实验,电子邮件流程,用户A,用户B,服务器A,服务器B,1、用户A通过邮件客户端发送邮件到服务器A 2、服务器A将邮件发送到服务器B 3、用户B接受服务器B上的邮件,用户A邮件发送过程,用户A客户端首先和服务器A建立TCP连接 确认之后,用户A和服务器A之间采用SMTP协议发送邮件内容 邮件内容传输完毕后,发送结束,邮件客户端JAVA程序,该程序分为4部分,分别为mailclient、envelope、message、smtpconnection Mail cilent为客户端主程序,包括使用界面、按键的定义,整个的发送流程中类的创建 message为发送邮件的内容部分,包含有发件
2、人、收件人等内容 envelope为用于smtp协议的信息传递,包含发送接收信息以及message信息 smtpconnection为发件过程中和smtp连接的建立以及关闭,发送过程中使用的指令,HELO 250 MAIL FROM 250 RCPT TO 250 DATA 354 QUIT 221,Mail Client,import java.io.*; import .*; import java.awt.*; import java.awt.event.*; public class MailClient extends Frame private Button btSend = ne
3、w 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
4、(, 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);,private Label messageLabel = new Label(Message:); private TextArea messageText = new TextArea(10, 4
5、0); /* * 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
6、(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, BorderL
7、ayout.CENTER); toPanel.add(toLabel, BorderLayout.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.CE
8、NTER); 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.addAc
9、tionListener(new SendListener(); btClear.addActionListener(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.CENTE
10、R); 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
11、local mailserver */ if (serverField.getText().equals() System.out.println(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 t
12、he message */ Message mailMessage = new Message(fromField.getText(), toField.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; Envelo
13、pe 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; ,try SMTPConnection connection = new SMTPConnection(envelope); connection.send(envelope); connection.cl
14、ose(); 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); fromFiel
15、d.setText(); toField.setText(); subjectField.setText();,messageText.setText(); /* Quit */ class QuitListener implements ActionListener public void actionPerformed(ActionEvent e) System.exit(0); ,Message,import java.util.*; import java.text.*; public class Message public String Headers; public String
16、 Body; private String From; private String To; /* To make it look nicer */ private static final String CRLF = 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
17、*/,From = from.trim(); To = to.trim(); Headers = From: + From + CRLF; Headers += To: + To + CRLF; Headers += Subject: + 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); St
18、ring dateString = format.format(new Date(); Headers += Date: + dateString + CRLF; Body = text; /* Two functions to access the sender and recipient. */ public String getFrom() return From; public String getTo() return To; ,/*检查信息的有效性,发送者和接受者的地址 * contains only one -sign. */ public boolean isValid() i
19、nt 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() Sys
20、tem.out.println(Sender address is invalid.); return false; ,/*检查信息的有效性,发送者和接受者的地址 * 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(Sender address is invalid.); return false; if
21、 (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; ,if (toat != To.lastIndexOf() System.out.println(Recipient address is invalid.); return false; return t
22、rue; /* For printing the message. */ public String toString() String res; res = Headers + CRLF; res += Body; return res; ,Envelope,import java.io.*; import .*; import java.util.*; public class Envelope public String Sender; /* SMTP-recipient, or contents of To-header. */ public String Recipient; /*
23、Target MX-host */ public String DestHost; public InetAddress DestAddr; /* The actual message. */ public Message Message; /* Create the envelope. */ public Envelope(Message message, String localServer) throws UnknownHostException ,/* Get sender and recipient. */ Sender = message.getFrom(); Recipient
24、= message.getTo(); Message = escapeMessage(message); /* Take the name of the local mailserver and map it into an Inet Address */ DestHost = localServer; try DestAddr = InetAddress.getByName(DestHost); catch (UnknownHostException e) System.out.println(Unknown Host: + DestHost); System.out.println(e);
25、 throw e; return; ,private Message escapeMessage(Message message) String escapeBody = ; String token; StringTokenizer parser = new StringTokenizer(message.Body, n, true); while (parser.hasMoreTokens() token = parser.nextToken(); if (token.startsWith(.) token = . + token; escapeBody += token; message
26、.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-host: + DestHost + , address: + DestAddr + n; res += Message: + n; res += Message.toString(); return res; ,SMT
27、PConnection,import .*; import java.io.*; import java.util.*; public class SMTPConnection private Socket connection; /* Streams for reading and writing the socket */ private BufferedReader fromServer; private DataOutputStream toServer; private static final int SMTP_PORT = 25; private static final Str
28、ing 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. Initialize * SMTP connection. */ public SMTPConnection(Envelope envelope) throws IOException connecti
29、on = new Socket(envelope.DestAddr, SMTP_PORT); fromServer = new BufferedReader(new InputStreamReader(connection.getInputStream(); toServer = new DataOutputStream(connection.getOutputStream(); /* Read a line from server and check that the reply code is 220. If not, throw an IOException. */ String rep
30、ly = 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. */ String localhost = envelope.DestHost; try sendCommand(HELO + localhost, 250); catch (IO
31、Exception 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 checking for errors, * just throw them to the caller. */ public void send(Envelope
32、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.Sender + CRLF, 250); sendCommand(RCPT TO: + envelope.Recipient + CRLF, 250); sen
33、dCommand(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 /* 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
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 芯诗珀莉护理考核测试卷及答案
- 2026电容器考试题库及答案
- 2026电气符号考试题及答案解析
- 2026电镜分析笔试题目及答案
- 市政工程质量监督工作方案
- 室内装饰装修专项施工方案
- 2026年外贸样品管理员考试题库及答案
- 室外管网回填砂砾铺设规范
- 中小学教师体育教育试题及答案
- 抗高血压药物考试试卷(附答案)
- 信用卡部培训大纲
- 代建公司代建管理制度
- T/CSWSL 007-2019饲料原料酵母水解物
- 滚针美容治疗技术解析
- 儿童自闭症康复中心项目商业计划书
- 莱州市月季产业发展规划(2018-2022年)
- 2025黑龙江七台河辰能生物质发电有限公司招聘笔试参考题库附带答案详解
- 《毛泽东思想和中国特色社会主义理论体系概论》附有答案
- 兰州市文职辅警招聘考试真题
- 田英章毛笔楷书2500字(简体版)
- 柴油机发电作业岗位职业危害告知卡
评论
0/150
提交评论