List of usage examples for javax.mail.internet MimeUtility javaCharset
public static String javaCharset(String charset)
From source file:com.duroty.lucene.parser.ParserFactory.java
/** * DOCUMENT ME!// www . jav a2 s . c o m * * @param str DOCUMENT ME! * @param mimetype DOCUMENT ME! * * @return DOCUMENT ME! */ public String parse(String str, String mimetype, String charset) { if ((str == null) || str.trim().equals("")) { return null; } InputStream input = null; String className = (String) parseFactoryConfig.get(mimetype); //log.info("PARSE ATTACHMENT: " + className + " >> " + mimetype); if (className == null) { return null; } try { Class clazz = Class.forName(className); Parser parser = (Parser) clazz.newInstance(); parser.setCharset(MimeUtility.javaCharset(charset)); input = IOUtils.toInputStream(str); return parser.parse(input); } catch (ClassNotFoundException e) { } catch (InstantiationException e) { } catch (IllegalAccessException e) { } catch (ParserException e) { } catch (Exception e) { } finally { IOUtils.closeQuietly(input); } return null; }
From source file:mitm.common.mail.MimeUtils.java
/** * Parses the contentType and returns the Java Charset found in the content type. Null if charset * parameter is not found./*from w w w .j a va 2 s .co m*/ */ public static String getCharsetFromContentType(String contentType) throws MimeTypeParseException { MimeType mimeType = new MimeType(contentType); String charset = mimeType.getParameter("charset"); return StringUtils.isNotEmpty(charset) ? MimeUtility.javaCharset(charset) : null; }
From source file:com.duroty.lucene.parser.ParserFactory.java
/** * DOCUMENT ME!/* w ww . j a v a2s . c o m*/ * * @param in DOCUMENT ME! * @param mimetype DOCUMENT ME! * * @return DOCUMENT ME! */ public String parse(InputStream input, String mimetype, String charset) { if (input == null) { return null; } String className = (String) parseFactoryConfig.get(mimetype); //log.info("PARSE ATTACHMENT: " + className + " >> " + mimetype); if (className == null) { return null; } try { Class clazz = Class.forName(className); Parser parser = (Parser) clazz.newInstance(); parser.setCharset(MimeUtility.javaCharset(charset)); return parser.parse(input); } catch (ClassNotFoundException e) { } catch (InstantiationException e) { } catch (IllegalAccessException e) { } catch (ParserException e) { } catch (Exception e) { } finally { IOUtils.closeQuietly(input); } return null; }
From source file:com.duroty.application.chat.manager.ChatManager.java
/** * Creates a new ChatManager object./*from w w w .j a v a 2s. com*/ * * @param chat DOCUMENT ME! * @throws ClassNotFoundException * @throws IllegalAccessException * @throws InstantiationException */ public ChatManager(HashMap chat) throws ClassNotFoundException, InstantiationException, IllegalAccessException { super(); if (charset == null) { charset = MimeUtility.javaCharset(Charset.defaultCharset().displayName()); } }
From source file:com.duroty.application.files.manager.StoreManager.java
/** * DOCUMENT ME!//from www . j a va2s .c o m * * @param hsession DOCUMENT ME! * @param session DOCUMENT ME! * @param repositoryName DOCUMENT ME! * @param identity DOCUMENT ME! * @param to DOCUMENT ME! * @param cc DOCUMENT ME! * @param bcc DOCUMENT ME! * @param subject DOCUMENT ME! * @param body DOCUMENT ME! * @param attachments DOCUMENT ME! * @param isHtml DOCUMENT ME! * @param charset DOCUMENT ME! * @param headers DOCUMENT ME! * @param priority DOCUMENT ME! * * @throws MailException DOCUMENT ME! */ public void send(org.hibernate.Session hsession, Session session, String repositoryName, Vector files, int label, String charset) throws FilesException { ByteArrayInputStream bais = null; FileOutputStream fos = null; try { if ((files == null) || (files.size() <= 0)) { return; } if (charset == null) { charset = MimeUtility.javaCharset(Charset.defaultCharset().displayName()); } Users user = getUser(hsession, repositoryName); Identity identity = getDefaultIdentity(hsession, user); InternetAddress _returnPath = new InternetAddress(identity.getIdeEmail(), identity.getIdeName()); InternetAddress _from = new InternetAddress(identity.getIdeEmail(), identity.getIdeName()); InternetAddress _replyTo = new InternetAddress(identity.getIdeReplyTo(), identity.getIdeName()); InternetAddress _to = new InternetAddress(identity.getIdeEmail(), identity.getIdeName()); for (int i = 0; i < files.size(); i++) { MultiPartEmail email = email = new MultiPartEmail(); email.setCharset(charset); if (_from != null) { email.setFrom(_from.getAddress(), _from.getPersonal()); } if (_returnPath != null) { email.addHeader("Return-Path", _returnPath.getAddress()); email.addHeader("Errors-To", _returnPath.getAddress()); email.addHeader("X-Errors-To", _returnPath.getAddress()); } if (_replyTo != null) { email.addReplyTo(_replyTo.getAddress(), _replyTo.getPersonal()); } if (_to != null) { email.addTo(_to.getAddress(), _to.getPersonal()); } MailPartObj obj = (MailPartObj) files.get(i); email.setSubject("Files-System " + obj.getName()); Date now = new Date(); email.setSentDate(now); File dir = new File(System.getProperty("user.home") + File.separator + "tmp"); if (!dir.exists()) { dir.mkdir(); } File file = new File(dir, obj.getName()); bais = new ByteArrayInputStream(obj.getAttachent()); fos = new FileOutputStream(file); IOUtils.copy(bais, fos); IOUtils.closeQuietly(bais); IOUtils.closeQuietly(fos); EmailAttachment attachment = new EmailAttachment(); attachment.setPath(file.getPath()); attachment.setDisposition(EmailAttachment.ATTACHMENT); attachment.setDescription("File Attachment: " + file.getName()); attachment.setName(file.getName()); email.attach(attachment); String mid = getId(); email.addHeader(RFC2822Headers.IN_REPLY_TO, "<" + mid + ".JavaMail.duroty@duroty" + ">"); email.addHeader(RFC2822Headers.REFERENCES, "<" + mid + ".JavaMail.duroty@duroty" + ">"); email.addHeader("X-DBox", "FILES"); email.addHeader("X-DRecent", "false"); //email.setMsg(body); email.setMailSession(session); email.buildMimeMessage(); MimeMessage mime = email.getMimeMessage(); int size = MessageUtilities.getMessageSize(mime); if (!controlQuota(hsession, user, size)) { throw new MailException("ErrorMessages.mail.quota.exceded"); } messageable.storeMessage(mid, mime, user); } } catch (FilesException e) { throw e; } catch (Exception e) { throw new FilesException(e); } catch (java.lang.OutOfMemoryError ex) { System.gc(); throw new FilesException(ex); } catch (Throwable e) { throw new FilesException(e); } finally { GeneralOperations.closeHibernateSession(hsession); IOUtils.closeQuietly(bais); IOUtils.closeQuietly(fos); } }
From source file:com.duroty.lucene.parser.ParserFactory.java
/** * DOCUMENT ME!/*from w w w. j a v a 2 s .co m*/ * * @param bytes DOCUMENT ME! * @param mimetype DOCUMENT ME! * * @return DOCUMENT ME! */ public String parse(byte[] bytes, String mimetype, String charset) { if ((bytes == null) || (bytes.length == 0)) { return null; } InputStream input = null; String className = (String) parseFactoryConfig.get(mimetype); //log.info("PARSE ATTACHMENT: " + className + " >> " + mimetype); if (className == null) { return null; } try { Class clazz = Class.forName(className); Parser parser = (Parser) clazz.newInstance(); parser.setCharset(MimeUtility.javaCharset(charset)); input = new ByteArrayInputStream(bytes); return parser.parse(input); } catch (ClassNotFoundException e) { } catch (InstantiationException e) { } catch (IllegalAccessException e) { } catch (ParserException e) { } catch (Exception e) { } finally { IOUtils.closeQuietly(input); } return null; }
From source file:com.duroty.application.mail.manager.SendManager.java
/** * DOCUMENT ME!// w ww. ja v a2 s .c om * * @param hsession DOCUMENT ME! * @param session DOCUMENT ME! * @param repositoryName DOCUMENT ME! * @param identity DOCUMENT ME! * @param to DOCUMENT ME! * @param cc DOCUMENT ME! * @param bcc DOCUMENT ME! * @param subject DOCUMENT ME! * @param body DOCUMENT ME! * @param attachments DOCUMENT ME! * @param isHtml DOCUMENT ME! * @param charset DOCUMENT ME! * @param headers DOCUMENT ME! * @param priority DOCUMENT ME! * * @throws MailException DOCUMENT ME! */ public void send(org.hibernate.Session hsession, Session session, String repositoryName, int ideIdint, String to, String cc, String bcc, String subject, String body, Vector attachments, boolean isHtml, String charset, InternetHeaders headers, String priority) throws MailException { try { if (charset == null) { charset = MimeUtility.javaCharset(Charset.defaultCharset().displayName()); } if ((body == null) || body.trim().equals("")) { body = " "; } Email email = null; if (isHtml) { email = new HtmlEmail(); } else { email = new MultiPartEmail(); } email.setCharset(charset); Users user = getUser(hsession, repositoryName); Identity identity = getIdentity(hsession, ideIdint, user); InternetAddress _returnPath = new InternetAddress(identity.getIdeEmail(), identity.getIdeName()); InternetAddress _from = new InternetAddress(identity.getIdeEmail(), identity.getIdeName()); InternetAddress _replyTo = new InternetAddress(identity.getIdeReplyTo(), identity.getIdeName()); InternetAddress[] _to = MessageUtilities.encodeAddresses(to, null); InternetAddress[] _cc = MessageUtilities.encodeAddresses(cc, null); InternetAddress[] _bcc = MessageUtilities.encodeAddresses(bcc, null); if (_from != null) { email.setFrom(_from.getAddress(), _from.getPersonal()); } if (_returnPath != null) { email.addHeader("Return-Path", _returnPath.getAddress()); email.addHeader("Errors-To", _returnPath.getAddress()); email.addHeader("X-Errors-To", _returnPath.getAddress()); } if (_replyTo != null) { email.addReplyTo(_replyTo.getAddress(), _replyTo.getPersonal()); } if ((_to != null) && (_to.length > 0)) { HashSet aux = new HashSet(_to.length); Collections.addAll(aux, _to); email.setTo(aux); } if ((_cc != null) && (_cc.length > 0)) { HashSet aux = new HashSet(_cc.length); Collections.addAll(aux, _cc); email.setCc(aux); } if ((_bcc != null) && (_bcc.length > 0)) { HashSet aux = new HashSet(_bcc.length); Collections.addAll(aux, _bcc); email.setBcc(aux); } email.setSubject(subject); Date now = new Date(); email.setSentDate(now); File dir = new File(System.getProperty("user.home") + File.separator + "tmp"); if (!dir.exists()) { dir.mkdir(); } if ((attachments != null) && (attachments.size() > 0)) { for (int i = 0; i < attachments.size(); i++) { ByteArrayInputStream bais = null; FileOutputStream fos = null; try { MailPartObj obj = (MailPartObj) attachments.get(i); File file = new File(dir, obj.getName()); bais = new ByteArrayInputStream(obj.getAttachent()); fos = new FileOutputStream(file); IOUtils.copy(bais, fos); EmailAttachment attachment = new EmailAttachment(); attachment.setPath(file.getPath()); attachment.setDisposition(EmailAttachment.ATTACHMENT); attachment.setDescription("File Attachment: " + file.getName()); attachment.setName(file.getName()); if (email instanceof MultiPartEmail) { ((MultiPartEmail) email).attach(attachment); } } catch (Exception ex) { } finally { IOUtils.closeQuietly(bais); IOUtils.closeQuietly(fos); } } } String mid = getId(); if (headers != null) { Header xheader; Enumeration xe = headers.getAllHeaders(); for (; xe.hasMoreElements();) { xheader = (Header) xe.nextElement(); if (xheader.getName().equals(RFC2822Headers.IN_REPLY_TO)) { email.addHeader(xheader.getName(), xheader.getValue()); } else if (xheader.getName().equals(RFC2822Headers.REFERENCES)) { email.addHeader(xheader.getName(), xheader.getValue()); } } } else { email.addHeader(RFC2822Headers.IN_REPLY_TO, "<" + mid + ".JavaMail.duroty@duroty" + ">"); email.addHeader(RFC2822Headers.REFERENCES, "<" + mid + ".JavaMail.duroty@duroty" + ">"); } if (priority != null) { if (priority.equals("high")) { email.addHeader("Importance", priority); email.addHeader("X-priority", "1"); } else if (priority.equals("low")) { email.addHeader("Importance", priority); email.addHeader("X-priority", "5"); } } if (email instanceof HtmlEmail) { ((HtmlEmail) email).setHtmlMsg(body); } else { email.setMsg(body); } email.setMailSession(session); email.buildMimeMessage(); MimeMessage mime = email.getMimeMessage(); int size = MessageUtilities.getMessageSize(mime); if (!controlQuota(hsession, user, size)) { throw new MailException("ErrorMessages.mail.quota.exceded"); } messageable.saveSentMessage(mid, mime, user); Thread thread = new Thread(new SendMessageThread(email)); thread.start(); } catch (MailException e) { throw e; } catch (Exception e) { throw new MailException(e); } catch (java.lang.OutOfMemoryError ex) { System.gc(); throw new MailException(ex); } catch (Throwable e) { throw new MailException(e); } finally { GeneralOperations.closeHibernateSession(hsession); } }
From source file:com.duroty.application.mail.ejb.SendBean.java
/** * Send message//from w ww . j a va 2s . c o m * * @ejb.interface-method view-type = "remote" * @ejb.permission * role-name = "mail" * * @throws EJBException Thrown if method fails due to system-level error. */ public void send(String mid, int identity, String to, String cc, String bcc, String subject, String body, Vector attachments, boolean isHtml, String charset, String priority) throws MailException { SessionFactory hfactory = null; Session session = null; try { hfactory = (SessionFactory) ctx.lookup(hibernateSessionFactory); session = (Session) ctx.lookup(smtpSessionFactory); String repositoryName = context.getCallerPrincipal().getName(); if (charset == null) { charset = MimeUtility.javaCharset(Charset.defaultCharset().displayName()); } InternetHeaders headers = null; if (!StringUtils.isBlank(mid)) { headers = manager.getHeaders(hfactory.openSession(), repositoryName, mid); } manager.send(hfactory.openSession(), session, repositoryName, identity, to, cc, bcc, subject, body, attachments, isHtml, charset, headers, priority); } catch (NamingException e) { throw new MailException(e); } catch (Exception e) { if (e instanceof MailException) { throw (MailException) e; } throw new MailException(e); } finally { } }
From source file:com.duroty.application.mail.ejb.SendBean.java
/** * Save draft message//from w w w.j a va 2s . c om * * @ejb.interface-method view-type = "remote" * @ejb.permission * role-name = "mail" * * @throws EJBException Thrown if method fails due to system-level error. */ public void saveDraft(String mid, int identity, String to, String cc, String bcc, String subject, String body, Vector attachments, boolean isHtml, String charset, String priority) throws MailException { SessionFactory hfactory = null; Session session = null; try { hfactory = (SessionFactory) ctx.lookup(hibernateSessionFactory); session = (Session) ctx.lookup(smtpSessionFactory); String repositoryName = context.getCallerPrincipal().getName(); if (charset == null) { charset = MimeUtility.javaCharset(Charset.defaultCharset().displayName()); } InternetHeaders headers = null; if (!StringUtils.isBlank(mid)) { headers = manager.getHeaders(hfactory.openSession(), repositoryName, mid); } manager.saveDraft(hfactory.openSession(), session, repositoryName, identity, to, cc, bcc, subject, body, attachments, isHtml, charset, headers, priority); } catch (NamingException e) { throw new MailException(e); } catch (Exception e) { if (e instanceof MailException) { throw (MailException) e; } throw new MailException(e); } finally { } }
From source file:com.duroty.application.open.manager.OpenManager.java
/** * DOCUMENT ME!// w w w . jav a 2 s . c om * * @param session DOCUMENT ME! * @param repositoryName DOCUMENT ME! * @param from DOCUMENT ME! * @param to DOCUMENT ME! * @param subject DOCUMENT ME! * @param body DOCUMENT ME! * * @throws MailException DOCUMENT ME! */ private void sendData(Session msession, InternetAddress from, InternetAddress to, String username, String password, String signature) throws Exception { try { HtmlEmail email = new HtmlEmail(); email.setMailSession(msession); email.setFrom(from.getAddress(), from.getPersonal()); email.addTo(to.getAddress(), to.getPersonal()); email.setSubject("Duroty System"); email.setHtmlMsg("<p>Username: <b>" + username + "</b></p><p>Password: " + password + "<b></b></p><p>" + signature + "</p>"); email.setCharset(MimeUtility.javaCharset(Charset.defaultCharset().displayName())); email.send(); } finally { } }