2012-10-18 54 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天......如果你有类似的东西,问题...祝你好运

相关问题