Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: pstnitindivyang

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default pstnitindivyang

    package com.dataculling;

    import com.pff.*;
    import org.apache.commons.io.FileUtils;

    import javax.activation.DataHandler;
    import javax.activation.FileDataSource;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Multipart;
    import javax.mail.Session;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Vector;

    /**
    * Created by tr01 on 1/24/14.
    */
    public class ExtractPSTCls {

    private String strBaseFolder = "";
    private String strRootFolderName="";
    private String folderpath="";

    static List<File> files = new ArrayList<File>();



    public ExtractPSTCls(String filename,String strBaseFolder) {
    try {
    this.strBaseFolder = strBaseFolder+"\\";
    PSTFile pstFile = new PSTFile(filename);
    System.out.println("pst constructor..."+pstFile.getMessageStore().getDispl ayName());
    strRootFolderName = pstFile.getMessageStore().getDisplayName();

    new File(strBaseFolder+"\\"+strRootFolderName).mkdirs( );

    processFolder(pstFile.getRootFolder());
    } catch (Exception err) {
    err.printStackTrace();
    }
    }

    private String getRidOfIllegalFileNameCharacters(String strName)
    {
    String strLegalName = strName.replace(":", " ").replace("\\", " ").replace("?", " ").replace("/", " ").replace("|", " ").replace("*", " ").replace("<", " ").replace(">", " ").replace("\t", " ").replace("\"", " ");
    if (strLegalName.length() >= 100)
    {
    strLegalName = strLegalName.substring(0, 100);
    }
    return strLegalName;
    }

    public void processFolder(PSTFolder folder)
    throws PSTException, java.io.IOException
    {

    // the root folder doesn't have a display name
    folderpath = strBaseFolder+strRootFolderName+"\\"+folder.getDis playName();
    System.out.println("folder path::"+folderpath);
    new File(folderpath).mkdirs();

    //System.out.println(""+strBaseFolder+"\\"+folder.ge tDisplayName());

    // go through the folders...
    if (folder.hasSubfolders()) {
    Vector<PSTFolder> childFolders = folder.getSubFolders();
    for (PSTFolder childFolder : childFolders) {
    processFolder(childFolder);
    }
    }

    // and now the emails for this folder
    if (folder.getContentCount() > 0) {

    PSTMessage email = (PSTMessage)folder.getNextChild();

    while (email != null) {
    // printDepth();
    writeAttachmentToDisk(email);

    createMessage(email.getDisplayTo(),email.getSender Name(),email.getDisplayBCC(),email.getDisplayCC(), email.getSubject(),email.getBody(),files);

    email = (PSTMessage)folder.getNextChild();
    }

    }

    }



    public void writeAttachmentToDisk(PSTMessage pstMessage)throws IOException
    {
    String msgFilePath=folderpath+"\\"+getRidOfIllegalFileNam eCharacters(pstMessage.getSubject());
    new File(msgFilePath).mkdirs();

    try{
    for(int i=0;i<pstMessage.getNumberOfAttachments();i++)
    {
    PSTAttachment pstAttachment = pstMessage.getAttachment(i);
    File file = new File(msgFilePath+"\\"+pstAttachment.getDisplayName ());
    FileUtils.copyInputStreamToFile(pstAttachment.getF ileInputStream(), file);

    CheckFileTypeCls.processFileType(file,file.getName ().substring(file.getName().lastIndexOf(".")));
    files.add(file);
    }
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }

    public void createMessage(String strto, String from, String strBCC,String strCC,String subject, String body, List<File> attachments) {
    try {
    Message message = new MimeMessage(Session.getInstance(System.getProperti es()));

    if(!from.equals(""))
    {
    message.setFrom(new InternetAddress(from.replaceAll(" ","").replaceAll(";",",")));
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(strto.replaceAll(" ","").replaceAll(";",",")));
    message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(strBCC.replaceAll(" ","").replaceAll(";",",")));
    message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(strCC.replaceAll(" ","").replaceAll(";",",")));

    }

    message.setSubject(subject);

    // create the message part
    MimeBodyPart content = new MimeBodyPart();
    // fill message
    content.setText(body);
    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(content);
    // add attachments
    for(File file : attachments) {
    MimeBodyPart attachment = new MimeBodyPart();

    javax.activation.DataSource dataSource = new FileDataSource(file);

    attachment.setDataHandler(new DataHandler(dataSource));
    attachment.setFileName(file.getName());
    multipart.addBodyPart(attachment);
    }
    // integration
    message.setContent(multipart);
    // store file
    System.out.println("eml file save at :: "+folderpath+"\\"+subject+".eml");
    message.writeTo(new FileOutputStream(new File(folderpath + "\\" + subject + ".eml")));
    } catch (MessagingException ex) {
    ex.printStackTrace();

    } catch (IOException ex) {
    ex.printStackTrace();

    }
    }

    }



    &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &


    package com.dataculling;

    import org.apache.commons.io.FileUtils;

    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.util.Enumeration;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipFile;

    /**
    * Created by tr01 on 1/24/14.
    */
    public class ExtractZipCls {

    public boolean unzipToFile(String srcZipFileName,
    String destDirectoryName) {

    try {
    BufferedInputStream bufIS = null;
    // create the destination directory structure (if needed)
    File destDirectory = new File(destDirectoryName);
    destDirectory.mkdirs();

    // open archive for reading
    File file = new File(srcZipFileName);
    ZipFile zipFile = new ZipFile(file, ZipFile.OPEN_READ);

    //for every zip archive entry do
    Enumeration<? extends ZipEntry> zipFileEntries = zipFile.entries();
    while (zipFileEntries.hasMoreElements()) {
    ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
    System.out.println("\tExtracting entry: " + entry);

    //create destination file
    File destFile = new File(destDirectory, entry.getName());

    //create parent directories if needed
    File parentDestFile = destFile.getParentFile();
    parentDestFile.mkdirs();

    if (!entry.isDirectory()) {
    bufIS = new BufferedInputStream(
    zipFile.getInputStream(entry));

    FileOutputStream fOS = new FileOutputStream(destFile);
    //copy file
    //copy(bufIS,fOS);
    FileUtils.copyInputStreamToFile(bufIS, destFile);
    bufIS.close();

    String zipFilePath = destDirectory.getPath() + File.separatorChar + entry.getName();
    CheckFileTypeCls.processFileType(new File(zipFilePath),entry.getName().substring(entry. getName().toLowerCase().lastIndexOf(".")));
    }
    }

    return true;
    } catch (Exception e) {
    e.printStackTrace();
    return false;
    }
    }

    }

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: pstnitindivyang

    Do you have a question?

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: pstnitindivyang

    Welcome to the Forum! Please read this topic to learn how to post your code correctly along with other useful info for newcomers.

    Code posted unformatted, "raw," as you've done will be largely ignored. Post it properly and ask a question.