-1

通过 Outlook 邮件发送 xml 后,C# 应用程序崩溃。它还需要在发送邮件时关闭 Outlook。使用 OUTLOOK 2010。它适用于较低版本。

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
}
}
4

1 回答 1

0

我发现了问题...转到属性(Outlook 的)...兼容性...选择任何旧版本 我的应用程序与 XP Service Pack 3 兼容。感谢上帝...我花了很多时间来查找错误详细信息...但最后..这个简单的事情困扰了我 4-5 天..如果您有类似的问题,可以使用这个作为您的主要调查来节省时间...祝你好运

于 2012-10-22T03:48:45.043 回答