您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > Java——简单邮件发送客户端实现
这个程序主要就是实现一个简单的邮件客户端,在知道邮箱服务器pop3地址的情况下可以实现随意填写发信人地址给指定用户发邮件。代码如下:importjava.awt.*;importjava.awt.event.*;importjava.io.*;importjava.net.*;importjavax.swing.*;importjavax.swing.border.BevelBorder;publicclassMailclientextendsJFrame{publicMailclient(){JPanelp=newJPanel();//createtheGUIJPanelp1=newJPanel();JPanelp2=newJPanel();JPanelp3=newJPanel();JPanelp4=newJPanel();JPanelp5=newJPanel();JButtonSend=newJButton(Send);//threebuttonJButtonClear=newJButton(Clear);JButtonQuit=newJButton(Quit);Stringdefult=Youcanchangetheinformationorusethedefult,andyoucangettheruninformationfromJavaConsole.;//createthedefaultmailtextfinalJTextAreamessages=newJTextArea(defult,22,40);messages.setLineWrap(true);//autolinefeedmessages.setBorder(newBevelBorder(BevelBorder.LOWERED,Color.gray,Color.gray));p1.add(newJLabel(From:));finalJTextFieldjtf1=newJTextField(android_757@yahoo.com.cn,38);p1.add(jtf1);p2.add(newJLabel(To:));finalJTextFieldjtf2=newJTextField(android_757@qq.com,40);p2.add(jtf2);p3.add(newJLabel(Subject:));finalJTextFieldjtf3=newJTextField(TestMailclient,37);p3.add(jtf3);p5.add(newJLabel(MailServerName:));finalJTextFieldjtf4=newJTextField(mx3.qq.com,32);p5.add(jtf4);p4.add(Send);Send.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){SocketclientSocket;OutputStreamoutToServer;//sendmessengerstomailserverBufferedReaderinFromServer;//receivemessengersfrommailserverStringmailFrom=jtf1.getText();//gettheinformationthatuserinputStringmailTo=jtf2.getText();StringmailSubject=jtf3.getText();StringmailSeverName=jtf4.getText();StringgetFromServer=null;try{clientSocket=newSocket(mailSeverName,25);//createconnectwithmailServeronport25inFromServer=newBufferedReader(newInputStreamReader(clientSocket.getInputStream()));outToServer=clientSocket.getOutputStream();PrintWritersockout=newPrintWriter(outToServer,true);getFromServer=inFromServer.readLine();System.out.println(getFromServer);if(getFromServer.startsWith(220)){//sendmessengersasSTMPrules(bookpage78)sockout.println(HELOSariel);//andoutputallinformationonconsoleSystem.out.println(HELOSariel);getFromServer=inFromServer.readLine();System.out.println(getFromServer);if(getFromServer.startsWith(250)){sockout.println(MAILFROM:++mailFrom+);System.out.println(MAILFROM:++mailFrom+);getFromServer=inFromServer.readLine();System.out.println(getFromServer);if(getFromServer.startsWith(250)){sockout.println(RCPTTO:++mailTo+);System.out.println(RCPTTO:++mailTo+);getFromServer=inFromServer.readLine();System.out.println(getFromServer);if(getFromServer.startsWith(250)){sockout.println(DATA);System.out.println(DATA);getFromServer=inFromServer.readLine();System.out.println(getFromServer);if(getFromServer.startsWith(354)){sockout.println(From:+mailFrom);sockout.println(To:+mailTo);sockout.println(Subject:+mailSubject);sockout.println();System.out.println(From:+mailFrom);System.out.println(To:+mailTo);System.out.println(Subject:+mailSubject);System.out.println();Stringstr=messages.getText();StringReadersr=newStringReader(str);BufferedReaderbr=newBufferedReader(sr);Stringline=;while(true){try{//writethemaintexttoserverline=br.readLine();if(line==null)break;sockout.println(line);System.out.println(line);}catch(IOExceptione1){e1.printStackTrace();}}sockout.println(.);//endmaintextwith.System.out.println(.);getFromServer=inFromServer.readLine();System.out.println(getFromServer);if(getFromServer.startsWith(250)){sockout.println(QUIT);System.out.println(QUIT);getFromServer=inFromServer.readLine();System.out.println(getFromServer);if(getFromServer.startsWith(221)){System.out.println('\n'+Successfullysend!!!);}}}}}}}}catch(UnknownHostExceptione2){e2.printStackTrace();}catch(IOExceptione2){e2.printStackTrace();}}});p4.add(Clear);//thebuttonCleardeleteallinformationinJTextAreaClear.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){messages.setText();}});p4.add(Quit);Quit.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){System.exit(0);}});p.add(p1);p.add(p2);p.add(p3);p.add(p5);p.add(messages);p.add(p4);p.setBorder(newBevelBorder(BevelBorder.RAISED));add(p);}publicstaticvoidmain(String[]args){JFramemailclient=newMailclient();mailclient.setTitle(JavaMailclient);mailclient.setLocationRelativeTo(null);mailclient.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);mailclient.setSize(500,640);mailclient.setVisible(true);}};
本文标题:Java——简单邮件发送客户端实现
链接地址:https://www.777doc.com/doc-4700931 .html