Java Web adv CH9 Java mail Destination:Let us know how to use java to send a email (一) 在发送之前需要需要具备的东西有javamail1_4_4版本的组件 如果需要在局网或者广域网中搭建一个环境,可以使用WinMail软件来作为服务器(很好很强大)! 首先需要了解E-Mail中的几个协议: SMTP(Simple Mail Transfer Protocol) 简单邮件传输协议,用于邮件的传输 POP3(Post Office Protocal Version 3) 邮局协议第三版,用户邮件的接受 IMAP(Internet Message Access Protocol) 因特网消息访问协议,用于传输和接受但是普及率较低 JAF(JavaBeans Activation Framework) JavaBeans激活框架 (二) 在这里我来用代码演示一个比较完整的有邮件的发送(包含发送附件) //注:由于涉及到了文件的上传所以代码是写在servlet中的,还有不要忘记上传的库SmartUpload //还有就是javamail1_4_4中的包会与系统中的javax.mail包冲突 //解决方案:找到系统包(X:\Program Files\Java\Common\plugins\com.genuitec.eclipse.j2eedt.core_8.5.0.me201003231033\data\libraryset\EE_5\javaee.jar) //采用压缩包的方式打开并删除javax.mail包即可 <!--运行在servlet中的代码 try{ //get the instance of the Properties(java.util),and set the value Properties props=new Properties(); props.put("mail.smtp.auth","true");//only can be this //get the instance of the Session(javax.mail) Session session=Session.getInstance(props); //get the instance of the Message Message msg=new MimeMessage(); //get the instance of the SmartUpload SmartUpload su=new SmartUpload(); su.initialize(super.getServletConf,request,response); //get the Files and the File (Files and File both come from com.jspsmart.upload.*) Files files=su.getFiles(); File file=files.getFile(0); boolean contentAlreadyBeSetted=false; //to make a judgement if(!file.isMissing()){ //get the path of WebRoot String filePath=this.getServletContext.getRealpath("/")+System.currentTimeMillis()+file.getFileName(); //save the file file.saveAs(filePath); //get the MultiPart Multipart mp=new MimeMultipart(); //get Body Part BodyPart bp=new MimeBodyPart(); //get the FileDataSource //Becareful The File must be the java.io.File not the com.smartupload.file //It takes me a few of time to check it FileDataSource fds=new FileDataSource(new java.io.File(filePath)); //set file data source into the BodyPart bp.setDataHander(new DataHander(fds)); //set the name when it be downloaded bp.setFileName(file.getFileName()); //put BodyPart into the Multipart mp.addBodyPart(bp); //put the multipart into the message msg.setContent(mp); //remeber to make a mark the the content is already be setted! contentAlreadyBeSetted=true; } msg.setSubject("the title of the e-mail"); msg.setRecipient(Message.RecipientType.TO,new InternetAddress("the people who receive this e-mail")); msg.setFrom(new InternetAddress("the people who send this e-mail")); msg.setSentDate(new Date()); if(!contentAreadyBeSetted){ //Attention:There will be a mistake without judegement of the content set, //as a result of the coder who write setcontent() and setText() ,the content only will be text!!!! //remember above,that is a mistake that I had made!!! msg.setText("the text content of the e-mail"); } //get the Transport Transport trans=session.getTransport("smtp"); //test the trans is useable trans.connect("The Ip Address of the server","sender's name except @ and blow","password"); //begin to send it trans.sendMessage(msg,msg.getAllRecipients()); //and do not to forget to close it trans.close(); }catch(Exception e){} --> 以上就是怎样去发送一封邮件(包含附件),接下来就是将 怎样去接收邮件 <!--一下代码可以写在任意位置,这里是在mian函数中演示的 public static void main(String[] args){ //get instance of Properties Properties props = new Properties(); //set values props.put("mail.smtp.auth", "true"); //get instance of Session Session session = Session.getInstance(props); try { //The Store is used to store all the message Store store = session.getStore("pop3"); //connect server store.connect("localhost", "test", "j7student"); //get the Folder Folder fd = store.getFolder("INBOX"); //open it and the message only can be read fd.open(Folder.READ_ONLY); //now you get all the message do whatever you want! Message ms[] = fd.getMessages(); //do not colse it manul fd.close(false); //close the store store.close(); } catch (Exception e) {} } --> <!-- Author:Lovingshu's Forever Date:2011.09.22 Remark:Today I send ten thousand e-mails pass Through the server and the server is broken~ haha~and the computer of my classmates is shocked~haha ~It is funny humm~ -->