2016-02-05 200 views
0

我試圖將附件添加到Outlook中的新電子郵件。Javascript:打開Outlook並將附件添加到新電子郵件

像下面(從here拍攝):

function sendEmail(){ 
    try{ 
    var theApp = new ActiveXObject("Outlook.Application"); 
    var objNS = theApp.GetNameSpace('MAPI'); 
    var theMailItem = theApp.CreateItem(0); // value 0 = MailItem 
    theMailItem.to = ('[email protected]'); 
    theMailItem.Subject = ('test'); 
    theMailItem.Body = ('test'); 
    //theMailItem.Attachments.Add("C\\file.txt"); 
    theMailItem.display(); 
    } 
    catch (err) { 
     alert(err.message); 
    } 
} 

這是工作(在Outlook中打開,並與上述數據預填充新的電子郵件窗口),但僅當應該添加附件行註釋掉。

如果它是註釋掉異常被拋出類似「文件無法找到」,但文件是存在的。它可以作爲附件手動添加到Outlook中。

看起來展望試圖找到文件,但不能因爲某些原因。 我嘗試了正斜槓,反斜槓和雙反斜槓 - 沒有運氣。

在Windows 7和8中測試,結果相同。 它只需要從IE工作。

也許有人可以提供修復到上面的代碼或具有工作一段代碼,增加了附接到Outlook?

或者可能知道的哪些需要改變一些IE或Outlook設置?

非常感謝,無論如何。

+0

您是否嘗試過使用Attachment.Add文件協議?即 - 'file:/// C:/file.txt' – Scot

+0

備註:這是否適用於受限制的非常受控制的環境?如果不能指望它經常打破...... (我從經驗中知道) –

+0

@邁克爾霍布斯。是的。 – yurin

回答

0

其實我做了一個錯誤的道路,所以下面的代碼是完全可行且可以使用。據測試,在Windows 8和IE 11

相信它會在IE中只有工作,它會打開彈出窗口詢問運行ActiveX許可。

function sendEmail(){ 
     try{ 
      var theApp = new ActiveXObject("Outlook.Application"); 
      var objNS = theApp.GetNameSpace('MAPI'); 
      var theMailItem = theApp.CreateItem(0); // value 0 = MailItem 
      theMailItem.to = ('[email protected]'); 
      theMailItem.Subject = ('test'); 
      theMailItem.Body = ('test'); 
      theMailItem.Attachments.Add("C\\file.txt"); 
      theMailItem.display(); 
     } 
     catch (err) { 
     alert(err.message); 
     } 
    } 
相關問題