- 作者:zhaozj
- 发表时间:2020-12-23 10:35
- 来源:未知
//原理:可以将邮件看成有多个部分组成的,如正文是一个部分,附件也是一个部分,所以用BodyPart来设置邮件的格式 message.addRecipient(Message.RecipientType.TO,new InternetAddress(mail_to)); BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText("Pardon Ideas"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); messageBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource("./20020423/test.zip");//此处设置邮件的附件 messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName("test.zip"); multipart.addBodyPart(messageBodyPart); message.setContent(multipart); Transport.send(message); System.out.println("send ok!"); } catch(Exception ex) { System.out.println("faild"+ex); } } //此段代码用来发送带有附件的邮件 void jButton6_actionPerformed(ActionEvent e) { try { Properties props = System.getProperties(); Authenticator auth = new Email_Autherticator(); props.put("mail.smtp.host",host); props.put("mail.smtp.auth","true"); Session session = Session.getDefaultInstance(props,auth); //建立session Store store = session.getStore("pop3"); store.connect(host,username,password); //After connecting to the Store,you can get a Folder,which must be opened before you can read messages from it: Folder folder = store.getFolder("INBOX"); folder.open(Folder.READ_WRITE); Message message[] = folder.getMessages(); for (int i=0,n=message.length;i { //获得邮件的部分信息,如头部信息,送信人的署名,送信人的邮件地址 String out_from_person = ((InternetAddress)message[i].getFrom()[0]).getPersonal(); String out_from_address = ((InternetAddress)message[i].getFrom()[0]).getAddress(); System.out.println("From:"+out_from_person+"/t"); System.out.println("Address:"+out_from_address+"/t"); String out_subject = message[i].getSubject(); System.out.println("Subject:"+out_subject+"/t"); //以下代码用来获取邮件的主体信息 Part messagePart = message[i]; Object out_content = messagePart.getContent(); if (out_content instanceof Multipart) { messagePart = ((Multipart)out_content).getBodyPart(0); System.out.println("[ Multipart Message ]"); } String out_content_type = messagePart.getContentType(); System.out.println("CONTENT:"+out_content_type); if (out_content_type.startsWith("text/plain") | | out_content_type.startsWith("text/html")) { InputStream ipstm = messagePart.getInputStream(); BufferedReader bufreader = new BufferedReader(new InputStreamReader(ipstm)); String thisLine = bufreader.readLine(); while (thisLine != null) { System.out.println("thisLine: "+thisLine); thisLine = bufreader.readLine(); } } //获取附件 Multipart mp = (Multipart)message[i].getContent(); for (int j=0,m=mp.getCount();j { System.out.println("***"+m+"***"); Part part = mp.getBodyPart(j); String disposition = part.getDisposition(); if ((disposition != null) && ((disposition.equals(part.ATTACHMENT)) | | (disposition.equals(part.INLINE)))) { //以下代码将获得的附件保存到当前目录下,以part.getFileName()为文件名,也既是附件的名称。 File filename = new File(part.getFileName()); for (int k=0;filename.exists();k++) { filename = new File(part.getFileName()+k); } FileOutputStream myFileoutputstream = new FileOutputStream(filename); int chunk = part.getSize();//获得附件的大小,不一定很准确。 byte [] buffer = new byte[chunk]; InputStream instream = part.getInputStream(); instream.read(buffer,0,chunk); myFileoutputstream.write(buffer,0,chunk); instream.close(); myFileoutputstream.close(); } } System.out.println("------------------------------------------------------------"); message[i].setFlag(Flags.Flag.DELETED,true); } folder.close(true); store.close();//关闭 } catch(Exception ej2) { System.out.println(ej2); } }