List of usage examples for javax.mail.internet MimeUtility decodeWord
public static String decodeWord(String eword) throws ParseException, UnsupportedEncodingException
From source file:es.ucm.fdi.dalgs.mailbox.service.MailBoxService.java
/** * Returns new messages and fetches details for each message. *//*from w w w .j a v a 2 s . c o m*/ @Transactional(readOnly = false) public ResultClass<Boolean> downloadEmails() { ResultClass<Boolean> result = new ResultClass<>(); Properties properties = getServerProperties(protocol, host, port); Session session = Session.getDefaultInstance(properties); try { // connects to the message store Store store = session.getStore(protocol); store.connect(userName, password); // opens the inbox folder Folder folderInbox = store.getFolder("INBOX"); folderInbox.open(Folder.READ_ONLY); // fetches new messages from server Message[] messages = folderInbox.getMessages(); for (int i = 0; i < messages.length; i++) { Message msg = messages[i]; String[] idHeaders = msg.getHeader("MESSAGE-ID"); if (idHeaders != null && idHeaders.length > 0) { MessageBox exists = repositoryMailBox.getMessageBox(idHeaders[0]); if (exists == null) { MessageBox messageBox = new MessageBox(); messageBox.setSubject(msg.getSubject()); messageBox.setCode(idHeaders[0]); Address[] fromAddresses = msg.getFrom(); String from = InternetAddress.toString(fromAddresses); if (from.startsWith("=?")) { from = MimeUtility.decodeWord(from); } messageBox.setFrom(from); String to = InternetAddress.toString(msg.getRecipients(RecipientType.TO)); if (to.startsWith("=?")) { to = MimeUtility.decodeWord(to); } messageBox.setTo(to); String[] replyHeaders = msg.getHeader("References"); if (replyHeaders != null && replyHeaders.length > 0) { StringTokenizer tokens = new StringTokenizer(replyHeaders[0]); MessageBox parent = repositoryMailBox.getMessageBox(tokens.nextToken()); if (parent != null) parent.addReply(messageBox); } result = parseSubjectAndCreate(messageBox, msg); } } } folderInbox.close(false); store.close(); return result; } catch (NoSuchProviderException ex) { logger.error("No provider for protocol: " + protocol); ex.printStackTrace(); } catch (MessagingException ex) { logger.error("Could not connect to the message store"); ex.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } result.setSingleElement(false); return result; }
From source file:com.cubusmail.mail.util.MessageUtils.java
/** * @param data//from w w w . ja v a 2 s. c om * @return */ public static String decodeText(String data) { int start = 0, i; StringBuffer sb = new StringBuffer(); while ((i = data.indexOf("=?", start)) >= 0) { sb.append(data.substring(start, i)); int end = data.indexOf("?=", i); if (end < 0) { break; } String s = data.substring(i, end + 2); try { sb.append(MimeUtility.decodeWord(s)); } catch (Exception e) { sb.append(s); } start = end + 2; } if (start == 0) return data; if (start < data.length()) sb.append(data.substring(start)); return sb.toString(); }
From source file:com.duroty.utils.mail.MessageUtilities.java
/** * A 'safe' version of JavaMail getFileName() which doesn't throw * exceptions. Encoded filenames are also decoded if necessary. Why * doesn't JAVA Mail do this?/*from w ww . j av a 2 s . c om*/ * * @param part The part to interogate * * @return File name of the part, or null if missing or invalid * * @see javax.mail.Part */ public static String getFileName(Part part) { if (part == null) { return null; } String xname = null; try { xname = part.getFileName(); } catch (MessagingException xex) { } // decode the file name if necessary if ((xname != null) && xname.startsWith("=?")) { try { xname = MimeUtility.decodeWord(xname); } catch (Exception xex) { } } return xname; }