Example usage for javax.mail Message writeTo

List of usage examples for javax.mail Message writeTo

Introduction

In this page you can find the example usage for javax.mail Message writeTo.

Prototype

public void writeTo(OutputStream os) throws IOException, MessagingException;

Source Link

Document

Output a bytestream for this Part.

Usage

From source file:com.sonicle.webtop.mail.Service.java

public void processSaveMail(HttpServletRequest request, HttpServletResponse response) {
    MailAccount account = getAccount(request);
    String foldername = request.getParameter("folder");
    String uid = request.getParameter("id");

    try {//from w  w  w.j  a  v a 2  s .  c o m
        account.checkStoreConnected();
        FolderCache mcache = account.getFolderCache(foldername);
        Message msg = mcache.getMessage(Long.parseLong(uid));
        String subject = msg.getSubject();
        ServletUtils.setFileStreamHeadersForceDownload(response, subject + ".eml");
        OutputStream out = response.getOutputStream();
        msg.writeTo(out);
    } catch (Exception exc) {
        Service.logger.error("Exception", exc);
    }
}

From source file:com.sonicle.webtop.mail.Service.java

private void outputJarMailFolder(String foldername, Message msgs[], JarOutputStream jos) throws Exception {
    int digits = (msgs.length > 0 ? (int) Math.log10(msgs.length) + 1 : 1);
    for (int i = 0; i < msgs.length; ++i) {
        Message msg = msgs[i];
        String subject = msg.getSubject();
        if (subject != null)
            subject = subject.replace('/', '_').replace('\\', '_').replace(':', '-');
        else//from w  w  w.  j a v  a2s  . co m
            subject = "";
        java.util.Date date = msg.getReceivedDate();
        if (date == null)
            date = new java.util.Date();

        String fname = LangUtils.zerofill(i + 1, digits) + " - " + subject + ".eml";
        String fullname = null;
        if (foldername != null && !foldername.isEmpty())
            fullname = foldername + "/" + fname;
        else
            fullname = fname;
        JarEntry je = new JarEntry(fullname);
        je.setTime(date.getTime());
        jos.putNextEntry(je);
        msg.writeTo(jos);
        jos.closeEntry();
    }
    jos.flush();
}

From source file:com.sonicle.webtop.mail.Service.java

public void dmsArchiveMessages(FolderCache from, long nuids[], String idcategory, String idsubcategory,
        boolean fullthreads) throws MessagingException, FileNotFoundException, IOException {
    UserProfile profile = environment.getProfile();
    String archiveto = ss.getDmsArchivePath();
    for (long nuid : nuids) {
        Message msg = from.getMessage(nuid);

        String id = getMessageID(msg);
        if (id.startsWith("<")) {
            id = id.substring(1, id.length() - 1);
        }/*from ww  w .  jav a  2  s.  co m*/
        id = id.replaceAll("\\\\", "_");
        id = id.replaceAll("/", "_");
        String filename = archiveto + "/" + id + ".eml";
        String txtname = archiveto + "/" + id + ".txt";
        File file = new File(filename);
        File txtfile = new File(txtname);
        //Only if spool file does not exists
        if (!file.exists()) {

            String emailfrom = "nomail@nodomain.it";
            Address a[] = msg.getFrom();
            if (a != null && a.length > 0) {
                InternetAddress sender = ((InternetAddress) a[0]);
                emailfrom = sender.getAddress();
            }
            String emailto = "nomail@nodomain.it";
            a = msg.getRecipients(Message.RecipientType.TO);
            if (a != null && a.length > 0) {
                InternetAddress to = ((InternetAddress) a[0]);
                emailto = to.getAddress();
            }
            String subject = msg.getSubject();
            java.util.Date date = msg.getReceivedDate();
            java.util.Calendar cal = java.util.Calendar.getInstance();
            cal.setTime(date);
            int dd = cal.get(java.util.Calendar.DAY_OF_MONTH);
            int mm = cal.get(java.util.Calendar.MONTH) + 1;
            int yyyy = cal.get(java.util.Calendar.YEAR);
            String sdd = dd < 10 ? "0" + dd : "" + dd;
            String smm = mm < 10 ? "0" + mm : "" + mm;
            String syyyy = "" + yyyy;

            FileOutputStream fos = new FileOutputStream(file);
            msg.writeTo(fos);
            fos.close();

            PrintWriter pw = new PrintWriter(txtfile);
            pw.println(emailfrom);
            pw.println(emailto);
            pw.println(subject);
            pw.println(sdd + "/" + smm + "/" + syyyy);
            pw.println(profile.getUserId());
            pw.println(idcategory);
            pw.println(idsubcategory);
            pw.close();
        }
    }
    from.markDmsArchivedMessages(nuids, fullthreads);
}