2014-10-20 23 views
0

我想建立一个Java拖放与Outlook电子邮件的作品。我一直在使用雅各布,因为无法使用标准的AWT Event将数据从Outlook传输到Java。也就是说,我从这里或其他网站取得的所有解决方案都导致了Java中的致命性崩溃。下面的代码:雅各布致命错误

import java.awt.dnd.*; 
import javax.swing.*; 
import java.awt.event.*; 
import java.awt.*; 
import java.awt.datatransfer.*; 
import java.io.*; 
import java.util.List; 
import sun.awt.datatransfer.*; 
import com.jacob.com.*; 
import com.jacob.activeX.*; 
public class D2 extends JFrame 
{ 

private static final String DIR = "FILES"; 

private static void saveSelectedOutlookMails(String directory) { 

    Dispatch xl = new Dispatch("Outlook.Application"); 

    //Dispatch selection = Dispatch.get(xl, "Selection").toDispatch(); 

    System.out.println(xl); 
    System.out.println(xl==null); 
    //PROGRAM CRASHES AFTER THIS LINE 
    Dispatch explorer = Dispatch.get(xl,"ActiveExplorer").toDispatch(); 
    System.out.println("explorer"); 
    Object selection = Dispatch.get(explorer, "Selection").toDispatch(); 
    Variant count = Dispatch.get(selection, "Count"); 

    for (int mailIndex = 1; mailIndex <= count.toInt(); mailIndex++) { 
     Object mailItem = Dispatch.call(selection, "Item", new Variant(mailIndex)).toDispatch(); 

     Variant senderName = Dispatch.get(mailItem, "SenderName"); 
     Variant subject = Dispatch.get(mailItem, "Subject"); 
     Variant body = Dispatch.get(mailItem, "Body"); 

     String emailFileName = subject.toString() +".txt"; 

     String fullPath = directory + "/" + emailFileName; 
     try { 
      File email = new File(fullPath); 
      PrintWriter writer = new PrintWriter(new FileWriter(email)); 
      writer.println("From: "+ senderName); 
      writer.println("Subject: "+ subject); 
      writer.println(""); 
      writer.print(body); 
      writer.close(); 
     } 
     catch (IOException e) { 

      System.out.println(e.getMessage()); 
      //logger.error("IOException writing e-mail with subject: '"+ subject +"'", e); 
      continue; 
     } 

     Object attachments = Dispatch.get(mailItem, "Attachments").toDispatch(); 
     Variant attachmentCount = Dispatch.get(attachments, "Count"); 

     if (attachmentCount.toInt() > 0) { 

      for(int attachmentIndex = 1; attachmentIndex<=attachmentCount.toInt(); attachmentIndex++) { 

       Object attachment = Dispatch.call(attachments, "Item", new Variant(attachmentIndex)).toDispatch(); 
       Variant fileNameVariant = Dispatch.get(attachment, "FileName"); 
       String fileName = fileNameVariant.toString(); 

       Variant saveResult = Dispatch.call(attachment, "SaveAsFile", directory, "/", fileName); 
      } 
     } 
    } 


} 
public D2() throws Exception 
{ 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setBounds(0,0,300,300); 
    this.setVisible(true); 

    DropTarget dropTarget=new DropTarget(); 
    dropTarget.setComponent(this); 
    dropTarget.addDropTargetListener(new DropTargetAdapter() 
      { 
       public void drop(DropTargetDropEvent dtde){ 

        saveSelectedOutlookMails(DIR); 

       } 



      }); 
} 


public static void main(String[] args) 

{ 

    try{ 

     new D2(); 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 
} 

}

The crash

+0

让我们缩小范围。你会在哪一行发生致命的崩溃? – 2014-10-20 17:03:29

+0

实例化资源管理器。之前的两张照片被执行,而后一张不是。 – MarkDacek 2014-10-21 14:17:37

+0

看起来问题可能在于您如何访问Outlook应用程序。我个人比Java更流畅地使用VB.NET,但是[Checkout this prior posting](http://stackoverflow.com/questions/17361340/delete-and-update-outlook-contact-using-jacob-library) – 2014-10-21 18:58:52

回答

0

好吧,在某种程度上首先,您要创建Outlook.Application之前,我从来没有见过 - 我只见过的Active X组件方式:

eg



    ActiveXComponent xl = new ActiveXComponent("Outlook.Application"); 
    Dispatch explorer = Dispatch.get(xl,"ActiveExplorer").toDispatch(); 
    Dispatch selection = Dispatch.get(explorer, "Selection").toDispatch(); 
    Variant count = Dispatch.get(selection, "Count"); 

    // loop over selected mail items. 
    for (int mailIndex = 1; mailIndex <= count.getInt(); mailIndex++) { 
     Dispatch mailItem = Dispatch.call(selection, "Item", new Variant(mailIndex)).toDispatch(); 
     Variant subject = Dispatch.get(mailItem, "Subject"); 
     // .... and so on 
    } 


其次,你的代码是不节能的邮件,它拉出所有字段和附件,并试图重新创建邮件,这似乎是不准确的,在最好的,只会是的信息是什么样的近似值。

你为什么不只是使用COM对象另存为整个.msg文件到硬盘?那么如果你需要再次访问它,你可以使用类似JDIC的东西来启动Outlook,并以其原始荣耀弹出消息,包括所有附件?

+0

首先..非常感谢您的回答。这个目标实际上并不是你想象的那样,所以不要太担心它似乎在做什么。 我可能应该将这个问题略加区分,但两种方式(ActiveX和直接调度)都会导致致命错误。你知道雅各布是否与当前版本的Java兼容?因为这是一个非常令人讨厌的崩溃,而不是你典型的例外。 – MarkDacek 2014-10-23 13:45:34

+0

我们在所有版本的Windows(XP,2008,Vista,7,8)上,在Java 6和7的生产环境中运行它。尽管我们只使用32位JVM。 我不认为它是必要的,但我总是确保在运行任何代码之前明确加载DLL。 – antsbull 2014-10-23 17:19:58

+0

我刚刚运行了代码的第一部分,使用jacob.jar,jacob-1.17-x86.dll和JDK 32位1.7.0_45,并且它打印出来: [email protected] ,假 ,资源管理器 – antsbull 2014-10-23 17:32:41