Outlook

A

Alan Alan

Hello,
sorry to disturb it s my first time interacting with Outlook and i really need help. I m creating a pdf report when a mail is send with outlook. I have some code in C# and everything works fine with Outlook 2007. I m using the 2003 Outlook dll. My problem is when a client have Outlook 2003 installed the message is sent but the mail window stays open.
this problem started when i added a handler on the close event in case the mail is not sent i need to kill all the objects..
Here is my code sample :

using System;

using System.Data;

using System.Text;

using System.Collections.Generic;

using Dessau.LiguoriFramework.BLL;

using Outlook = Microsoft.Office.Interop.Outlook;





namespace Dessau.LiguoriFramework.BLL

{

public class OutlookMessage

{

#region Private Members



private Outlook.MailItem _oMail;

private Outlook.Application _oApp;

private Outlook.MAPIFolder _oContacts;

private Outlook.NameSpace _oNameSpace;

private Outlook.Inspectors _oInspectors;

private Outlook.Inspector _oInspector;



#endregion



#region Delegate



public delegate bool ItemSendHandler();



#endregion



#region Event



public event ItemSendHandler OnMailSend;



#endregion



#region Constructor



public OutlookMessage()

{

_oApp = new Outlook.Application();

_oNameSpace = _oApp.GetNamespace("mapi");

_oMail = _oApp.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;

_oApp.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(OnMailItemSend);

_oInspectors = _oApp.Application.Inspectors;

_oInspectors.NewInspector += new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(_oInspectors_NewInspector);



}



#endregion



#region Private Properties



private Outlook.Items Contacts

{

get

{

if (_oContacts == null)

_oContacts = _oNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);

return _oContacts.Items;

}

}



#endregion



#region Public properties



public List<string> MailTo

{

get { return GetContacts(_oMail.To); }

set { _oMail.To = SetContacts(value); }

}



public List<string> CarbonCopy

{

get { return GetContacts(_oMail.CC); }

set { _oMail.CC = SetContacts(value); }

}



public List<string> BlindCarbonCopy

{

get { return GetContacts(_oMail.BCC); }

set { _oMail.BCC = SetContacts(value); }

}



public string Body

{

get { return _oMail.Body; }

set { _oMail.Body = value; }

}



public string Subject

{

get { return _oMail.Subject; }

set { _oMail.Subject = value; }

}



public List<String> GetAttachements

{

get { return GetMailAttachements(); }

}



public bool UseWordMail

{

get { return _oMail.FormDescription.UseWordMail; }

set { _oMail.FormDescription.UseWordMail = value; }

}



#endregion



#region Public Methods



public void AddAttachement(string attachment)

{

if (string.IsNullOrEmpty(attachment))

return;

_oMail.Attachments.Add(attachment, Type.Missing, Type.Missing, Type.Missing);

}



public void Display()

{

try

{

_oMail.Display(true);

}

catch

{ End(); }

}



public void End()

{

_oApp = null;

_oMail = null;

_oInspectors = null;

_oInspector = null;

GC.Collect();

}



public string GetContactCompanyName(string nameOrMailAddress)

{

if (nameOrMailAddress == null)

return string.Empty;



Outlook.ContactItem contactitem;

string oFilter = string.Format("[FullName] = '{0}' Or [Email1Address] = '{0}' Or [Email2Address] = '{0}' Or [Email3Address] = '{0}'", nameOrMailAddress);



contactitem = Contacts.Find(oFilter) as Outlook.ContactItem;



return contactitem == null ? string.Empty :

contactitem.CompanyName;

}



public string GetContactCompanyPhoneNumber(string nameOrMailAddress)

{

if (nameOrMailAddress == null)

return string.Empty;



Outlook.ContactItem contactitem;

string oFilter = string.Format("[FullName] = '{0}' Or [Email1Address] = '{0}' Or [Email2Address] = '{0}' Or [Email3Address] = '{0}'", nameOrMailAddress);



contactitem = Contacts.Find(oFilter) as Outlook.ContactItem;



return contactitem == null ? string.Empty :

contactitem.BusinessTelephoneNumber;

}



public string GetContactFullName(string nameOrMailAddress)

{

if (nameOrMailAddress == null)

return string.Empty;



Outlook.ContactItem contactitem;

string oFilter = string.Format("[FullName] = '{0}' Or [Email1Address] = '{0}' Or [Email2Address] = '{0}' Or [Email3Address] = '{0}'", nameOrMailAddress);



contactitem = Contacts.Find(oFilter) as Outlook.ContactItem;



return contactitem == null ? string.Empty :

contactitem.FullName;

}



#endregion



#region Private methods



void OnMailItemSend(object Item, ref bool Cancel)

{

try

{

if (_oApp == null || _oMail == null || _oInspectors == null)

{

return;

}

// Trigger the invocation list if not empty.

if (OnMailSend == null)

return;



// Stop as soon as one delegate returns false

foreach (ItemSendHandler vch in OnMailSend.GetInvocationList())

if (!vch())

{

Cancel = true;

End();

return;

}

End();

}

catch

{

End();

Cancel = true;

}

}



void _oInspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)

{

if (_oApp == null || _oMail == null || _oInspectors == null)

return;



((Outlook.InspectorEvents_Event)Inspector).Close += new Microsoft.Office.Interop.Outlook.InspectorEvents_CloseEventHandler(OutlookMessage_Close);

_oInspector = Inspector;

}



void OutlookMessage_Close()

{

if (_oInspector != null)

((Outlook.InspectorEvents_Event)_oInspector).Close -= new Microsoft.Office.Interop.Outlook.InspectorEvents_CloseEventHandler(OutlookMessage_Close);

if (_oApp != null)

_oApp.ItemSend -= new Outlook.ApplicationEvents_11_ItemSendEventHandler(OnMailItemSend);



//_oApp = null;

//_oMail = null;

//_oInspectors = null;

//_oInspector = null;

//GC.Collect();

End();

}
......


Submitted via EggHeadCafe - Software Developer Portal of Choice
Professional ADO.NET 2.0 Programming [Wrox]
http://www.eggheadcafe.com/tutorial...88c4-7949157d13db/professional-adonet-20.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top