日期:2014-05-20 浏览次数:20822 次
// Define messageMessage message = new MimeMessage(session);message.setFrom(new InternetAddress(from));message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));message.setSubject("Hello JavaMail Attachment");// Create the message part BodyPart messageBodyPart = new MimeBodyPart();// Fill the messagemessageBodyPart.setText("Pardon Ideas");Multipart multipart = new MimeMultipart();multipart.addBodyPart(messageBodyPart);// Part two is attachmentmessageBodyPart = new MimeBodyPart();DataSource source = new FileDataSource(filename);messageBodyPart.setDataHandler(new DataHandler(source));messageBodyPart.setFileName(filename);multipart.addBodyPart(messageBodyPart);// Put parts in messagemessage.setContent(multipart);// Send the messageTransport.send(message);
------解决方案--------------------
没做过,但是感觉可以自己解析。打开邮件文件,可以看到基本的邮件结构,其中附件肯定是以BASE64编码放在后面的,把它读出来解析,按照邮件中的文件名存储就可以了。
------解决方案--------------------
学习
------解决方案--------------------
我编过一个从邮箱里获取邮件附件的程序,供楼主参考吧
import javax.mail.Authenticator; import javax.mail.Flags; import javax.mail.Folder; import javax.mail.Message; import javax.mail.Multipart; import javax.mail.Part; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.URLName; ...... public static Folder getMailFolder(String serverString, String username, String password) throws Exception { Folder folder = null; URLName server = new URLName(serverString); session = Session.getInstance(new Properties(), new MailAuthenticator(username, password)); folder = session.getFolder(server); System.out.println("\n\n\nFolder retrieved for " + serverString + ", with " + folder.getMessageCount() + " messages"); return folder; } public static void getEmailWithSubjectContaining(String emailUrl, String username, String password, String regex, String filepath, boolean addPrefix, boolean delete) throws Exception { Folder folder = getMailFolder(emailUrl, username, password); if (folder == null) return; folder.open(Folder.READ_WRITE); Message[] msgs = folder.getMessages(); System.out.println("Totally there are " + msgs.length); for (int i = 0; i < msgs.length; i++) { String pathToSave = filepath; String subject = msgs[i].getSubject(); System.out.println("Email subject: " + subject); if (!subject.matches(regex)) { System.out.println("Email subject doesn't match regex: " + regex + ", this email is ignored."); continue; } Part p = (Part) msgs[i]; if (addPrefix) { pathToSave = pathToSave + "/" + getFilenamePrefix(subject); } dumpAttachment(p, pathToSave); msgs[i].setFlag(Flags.Flag.DELETED, delete); //得到邮件后删除 } folder.expunge(); //清除邮箱里DELETE的邮件 folder.close(false); } public static void dumpAttachment(Part part, String filepath) throws Exception{ if (part.isMimeType("text/plain")) { System.out.println("This is plain text"); } else if (part.isMimeType("multipart/*")) { System.out.println("This is a Multipart, trying to dig into the attachment now"); Multipart mp = (Multipart) part.getContent(); int count = mp.getCount(); for (int i = 0; i < count; i++) dumpAttachment(mp.getBodyPart(i), filepath); } else if (part.isMimeType("message/rfc822")) { System.out.println("This is a Nested Message"); dumpAttachment((Part) part.getContent(), filepath); } else { Object o = part.getContent(); String attachmentFilename = "no_name"; if (part.getFileName() != null && !part.getFileName().equals("")) { attachmentFilename = part.getFileName(); } if (o instanceof String) { System.out.println("This is a string"); } else if (o instanceof InputStream) { System.out.println("This is just an input stream"); InputStream is = (InputStream) o; FileOutputStream fos = new FileOutputStream(filepath + formatFileExtension(attachmentFilename)); int c; int count = 0; System.out.print("Saving attachment: " + formatFileExtension(attachmentFilename)); while ((c = is.read()) != -1) { fos.write(c); if (count % 1000000 == 0) { System.out.print("\n"); } else if (count % 5000 == 0) { System.out.print("."); } count ++; } System.out.println("\nAttachment " + (format.format((double)count/1024)) + " Kilobytes saved as '" + filepath + formatFileExtension(attachmentFilename) + "'\n"); } else { System.out.println("This is an unknown type"); System.out.println(o.toString()); } } } ......