2012-10-18 86 views
-1

通過Outlook郵件發送xml後,C#應用程序崩潰。此外,它還要求在發送郵件時關閉Outlook。使用OUTLOOK 2010.它可以在較低版本下正常工作。通過outlook郵件發送xml後,C#應用程序崩潰

using System; 
using System.Reflection; 

namespace Amdocs.Infra.Controls 
{ 
    public class OutlookWrapper 
    { 
    public OutlookWrapper() 
    { 
     outlookType = Type.GetTypeFromProgID ("Outlook.Application", true); 
     outlookObject = Activator.CreateInstance (outlookType); 
     namespaceMAPI = outlookType.InvokeMember ("GetNamespace", BindingFlags.InvokeMethod, null, outlookObject, new object [] {"MAPI"}); 
     namespaceMAPIType = namespaceMAPI.GetType(); 
     namespaceMAPIType.InvokeMember ("Logon", BindingFlags.InvokeMethod, null, namespaceMAPI, new object [] {null, null, true, false}); 
    } 
#endregion 

#region ~OutlookWrapper() 
~OutlookWrapper() 
{ 
    namespaceMAPIType.InvokeMember ("Logoff", BindingFlags.InvokeMethod, null, namespaceMAPI, new object [] {}); 
} 
#endregion 

#region SendMail 

#region public void SendMail (bool modal, string toValue) 
public void SendMail (bool modal, string toValue) 
{ 
    SendMail (modal, toValue, String.Empty); 
} 
#endregion 
#region public void SendMail (bool modal, string toValue, string subjectValue) 
public void SendMail (bool modal, string toValue, string subjectValue) 
{ 
    SendMail (modal, toValue, subjectValue, String.Empty); 
} 
#endregion 
#region public void SendMail (bool modal, string toValue, string subjectValue, string bodyValue) 
public void SendMail (bool modal, string toValue, string subjectValue, string bodyValue) 
{ 
    SendMail (modal, toValue, subjectValue, bodyValue, null); 
} 
#endregion 
#region public void SendMail (bool modal, string toValue, string subjectValue, string bodyValue, string[] attachments) 
public void SendMail (bool modal, string toValue, string subjectValue, string bodyValue, string[] attachments) 
{ 
    CreateNewMailItem (toValue, subjectValue, bodyValue, attachments); 
    outlookMailItemType.InvokeMember ("Send", BindingFlags.InvokeMethod, null, outlookMailItem, new object [] {modal}); 
} 
#endregion 

#endregion 

#region NewMail 

#region public void NewMail() 
public void NewMail() 
{ 
    NewMail (true); 
} 
#endregion 
#region public void NewMail (bool modal) 
public void NewMail (bool modal) 
{ 
    NewMail (modal, String.Empty); 
} 
#endregion 
#region public void NewMail (bool modal, string toValue) 
public void NewMail (bool modal, string toValue) 
{ 
    NewMail (modal, toValue, String.Empty); 
} 
#endregion 
#region public void NewMail (bool modal, string toValue, string subjectValue) 
public void NewMail (bool modal, string toValue, string subjectValue) 
{ 
    NewMail (modal, toValue, subjectValue, String.Empty); 
} 
#endregion 
#region public void NewMail (bool modal, string toValue, string subjectValue, string bodyValue) 
public void NewMail (bool modal, string toValue, string subjectValue, string bodyValue) 
{ 
    NewMail (modal, toValue, subjectValue, bodyValue, null); 
} 
#endregion 
#region public void NewMail (bool modal, string toValue, string subjectValue, string bodyValue, string[] attachments) 
public void NewMail (bool modal, string toValue, string subjectValue, string bodyValue, string[] attachments) 
{ 
    CreateNewMailItem (toValue, subjectValue, bodyValue, attachments); 
    outlookMailItemType.InvokeMember ("Display", BindingFlags.InvokeMethod, null, outlookMailItem, new object [] {modal}); 
} 
#endregion 

#endregion 

#region private void CreateNewMailItem (string toValue, string subjectValue, string bodyValue, string[] attachments) 
private void CreateNewMailItem (string toValue, string subjectValue, string bodyValue, string[] attachments) 
{ 
    outlookMailItem = outlookType.InvokeMember ("CreateItem", BindingFlags.InvokeMethod, null, outlookObject, new object [] {null}); 
    if (outlookMailItem == null) throw new ApplicationException (StringsManager.GetString("Cannot create outlook mail item.")); 

    outlookMailItemType = outlookMailItem.GetType(); 
    if (outlookMailItemType == null) throw new ApplicationException (StringsManager.GetString("Cannot get outlook mail item type.")); 

    outlookMailItemType.InvokeMember ("To",  BindingFlags.SetProperty, null, outlookMailItem, new object [] {toValue}); 
    outlookMailItemType.InvokeMember ("Subject", BindingFlags.SetProperty, null, outlookMailItem, new object [] {subjectValue}); 
    outlookMailItemType.InvokeMember ("Body", BindingFlags.SetProperty, null, outlookMailItem, new object [] {bodyValue}); 

    if (attachments != null) 
    { 
    object attachmentsObject = outlookMailItemType.InvokeMember ("Attachments", BindingFlags.GetProperty, null, outlookMailItem, new object [] {}); 
    if (attachmentsObject == null) throw new ApplicationException (StringsManager.GetString("Cannot get outlook attachments property.")); 

    System.Type attachmentsType = attachmentsObject.GetType(); 
    if (attachmentsType == null) throw new ApplicationException (StringsManager.GetString("Cannot get outlook attachments property type.")); 

    int bodyLength = bodyValue.Length; 

    /* 
    * We do not need this since we have the original text message 
    * 
    object bodyObject = outlookMailItemType.InvokeMember ("Body", BindingFlags.GetProperty, null, outlookMailItem, new object [] {}); 
    if (bodyObject == null) throw new ApplicationException (StringsManager.GetString("Cannot get outlook body property.")); 

    System.Type bodyType = bodyObject.GetType(); 
    if (bodyType == null) throw new ApplicationException (StringsManager.GetString("Cannot get outlook body property type.")); 
    */ 

    foreach (string s in attachments) 
     attachmentsType.InvokeMember ("Add", BindingFlags.InvokeMethod, null, attachmentsObject, new object [] {s, olByValue, ++bodyLength, s}); 
    } 
} 
#endregion 

#region private members 

private System.Type outlookType   = null; 
private object  outlookObject  = null; 
private object  namespaceMAPI  = null; 
private System.Type namespaceMAPIType = null; 
object    outlookMailItem  = null; 
System.Type   outlookMailItemType = null; 

private const int olFolderDeleted = 3; 
private const int olFolderOutbox = 4; 
private const int olFolderSentItems = 5; 
private const int olFolderInbox  = 6; 
private const int olFolderCalendar = 9; 
private const int olFolderContacts = 10; 
private const int olFolderJournal = 11; 
private const int olFolderNotes  = 12; 
private const int olFolderTasks  = 13; 
private const int olFolderDrafts = 16; 

private const int olByValue  = 1; 
private const int olByReference = 4; 
private const int olEmbeddeditem = 5; 

#endregion 
} 
} 
+0

請告訴我們你到目前爲止試過的東西(例如:_add some code,screenShots,..._) –

+0

另外,試着制定一個問題(畢竟這是一個問答網站) – Prutswonder

+0

好的,我們現在知道您的應用崩潰了,但您沒有提出任何問題。 –

回答

0

我發現這個問題...轉到屬性(前景)..兼容性......選擇任何一個版本我的應用程序與XP Service Pack 3的工作了兼容性的。 感謝上帝......我在尋找錯誤細節方面浪費了太多時間......但最後......這個簡單的東西讓我困擾了4-5天......如果你有類似的東西,問題...祝你好運

相關問題