2016-04-21 78 views
1

我需要使用java實現下面的邏輯。需要用附件打開ms outlook

- >當我點擊一個按鈕時,MS Outlook需要打開To,CC,Subject和附件。

我們可以使用mailto來做這件事,但如果我們使用mailto,我們不能添加附件。

我需要從共享文件夾添加多個附件到MS Outlook

請幫助我。

使用切換,可以有單個附件,但我需要打開Outlook與2+附件併發送按鈕應該可用,以便用戶可以發送郵件

+1

爲什麼要使用Outlook? – Stefan

+0

http://stackoverflow.com/questions/248569/starting-outlook-and-having-an-email-pre-populated-from-command-line – jmehrens

+0

@stefan - 用戶需要前景,因爲他們正在使用 – Thomas

回答

1

使用JavaMail創建一個帶有多部分MIME消息的To,CC,主題和附件。然後,不要傳輸消息調用saveChangeswriteTo,並將電子郵件存儲到文件系統。

有一個undocumented/eml開關,可用於打開MIME標準格式。例如,outlook /eml filename.eml有一個記錄的/f開關,它將打開msg文件。例如outlook /f filename.msgx-unsent可用於切換髮送按鈕。

下面是一個例子,讓你開始:

public static void main(String[] args) throws Exception { 
    //Create message envelope. 
    MimeMessage msg = new MimeMessage((Session) null); 
    msg.addFrom(InternetAddress.parse("[email protected]")); 
    msg.setRecipients(Message.RecipientType.TO, 
      InternetAddress.parse("[email protected]")); 
    msg.setRecipients(Message.RecipientType.CC, 
      InternetAddress.parse("[email protected]")); 
    msg.setSubject("Hello Outlook"); 
    //msg.setHeader("X-Unsent", "1"); 

    MimeMultipart mmp = new MimeMultipart(); 
    MimeBodyPart body = new MimeBodyPart(); 
    body.setDisposition(MimePart.INLINE); 
    body.setContent("This is the body", "text/plain"); 
    mmp.addBodyPart(body); 

    MimeBodyPart att = new MimeBodyPart(); 
    att.attachFile("c:\\path to file.attachment"); 
    mmp.addBodyPart(att); 

    msg.setContent(mmp); 
    msg.saveChanges(); 


    File resultEmail = File.createTempFile("test", ".eml"); 
    try (FileOutputStream fs = new FileOutputStream(resultEmail)) { 
     msg.writeTo(fs); 
     fs.flush(); 
     fs.getFD().sync(); 
    } 

    System.out.println(resultEmail.getCanonicalPath()); 

    ProcessBuilder pb = new ProcessBuilder(); 
    pb.command("cmd.exe", "/C", "start", "outlook.exe", 
      "/eml", resultEmail.getCanonicalPath()); 
    Process p = pb.start(); 
    try { 
     p.waitFor(); 
    } finally { 
     p.getErrorStream().close(); 
     p.getInputStream().close(); 
     p.getErrorStream().close(); 
     p.destroy(); 
    } 
} 

你必須處理清理電子郵件客戶端關閉後。

您還必須考慮保留在文件系統上的電子郵件的安全含義。

+0

你能請提供示例代碼 – Thomas

+0

僅當您承諾閱讀[JAVAMAIL API常見問題](http://www.oracle.com/technetwork/java/faq-135477.html)並保持JavaMail處於最新狀態時才提供示例代碼。在這裏簽字: – jmehrens

+0

謝謝你的代碼片段。但是我一次添加了多個附件 – Thomas