using System; using System.Reflection; namespace LBOutlookLibrary { public abstract partial class LBComObject { private object _Item; internal object Item { get { return _Item; } set { _Item = value; if (value != null) MyType = _Item.GetType(); } } public Type MyType { get; set; } protected LBComObject() { } protected LBComObject(string ProgID) { Type objClassType; objClassType = Type.GetTypeFromProgID(ProgID); Item = Activator.CreateInstance(objClassType); } protected LBComObject(object item) => Item = item; private object DoInvokeMember(string name, object[] parameters, BindingFlags bf, Binder b) { try { return MyType.InvokeMember(name, bf, b, _Item, parameters); } catch (Exception ex) { throw new Exception(string.Format("LBComObject.DoInvokeMember {0}.{1}", MyType.Name, name), ex); } } protected void SetProperty(string propertyName, params object[] parameters) => DoInvokeMember(propertyName, parameters, BindingFlags.SetProperty, null); protected object GetProperty(string propertyName) => DoInvokeMember(propertyName, null, BindingFlags.GetProperty, null); protected object GetProperty(string propertyName, params object[] parameters) => DoInvokeMember(propertyName, parameters, BindingFlags.GetProperty, null); protected object InvokeMethod(string methodName, params object[] parameters) { if (parameters != null) FixParameters(parameters); return DoInvokeMember(methodName, parameters, BindingFlags.InvokeMethod, null); } private void FixParameters(object[] parameters) { // Loop through parameters checking to make sure that none of them are LBComObjects for (int i = 0; i < parameters.Length; i++) if (parameters[i] is LBComObject) parameters[i] = (parameters[i] as LBComObject).Item; } protected object InvokeMethod(string methodName) => InvokeMethod(methodName, null); } public partial class LBApplicationClass : LBComObject { public LBApplicationClass() : base("Outlook.Application") { } public LBApplicationClass(object item) : base(item) { } public object CreateItem(LBOlItemType ItemType) => InvokeMethod("CreateItem", ItemType); public void Quit() => InvokeMethod("Quit"); } public enum LBOlItemType { olMailItem = 0, olAppointmentItem = 1, olContactItem = 2, olTaskItem = 3, olJournalItem = 4, olNoteItem = 5, olPostItem = 6, olDistributionListItem = 7 } public partial class LBMailItem : LBComObject { public LBMailItem() { } public LBMailItem(object item) : base(item) { } } public partial class LBMailItemClass : LBComObject { public LBMailItemClass() { } public LBMailItemClass(object item) : base(item) { } public string Body { get { return (GetProperty("Body").ToString()); } set { SetProperty("Body", value); } } public LBOlBodyFormat BodyFormat { get { return (LBOlBodyFormat)GetProperty("BodyFormat"); } set { SetProperty("BodyFormat", value); } } public LBAttachments Attachments => new LBAttachments(GetProperty("Attachments")); public string Subject { get { return (GetProperty("Subject").ToString()); } set { SetProperty("Subject", value); } } public LBRecipients Recipients => new LBRecipients(GetProperty("Recipients")); public string To { get { return (GetProperty("To").ToString()); } set { SetProperty("To", value); } } public void Send() => InvokeMethod("Send"); } public enum LBOlBodyFormat { olFormatUnspecified = 0, olFormatPlain = 1, olFormatHTML = 2, olFormatRichText = 3 } public partial class LBAttachments : LBComObject { public LBAttachments() { } public LBAttachments(object item) : base(item) { } public int Count => (GetProperty("Count") as int? ?? 0); public new LBAttachment Item => new LBAttachment(GetProperty("Item")); public LBAttachment Add(object Source) => new LBAttachment(InvokeMethod("Add", Source, Missing.Value, Missing.Value, Missing.Value)); public LBAttachment Add(object Source, object Type, object Position, object DisplayName) { return new LBAttachment(InvokeMethod("Add", Source, Type, Position, DisplayName)); } public void Remove(int Index) => InvokeMethod("Remove", Index); } public partial class LBRecipients : LBComObject { public LBRecipients() { } public LBRecipients(object item) : base(item) { } public int Count => (GetProperty("Count") as int? ?? 0); public new LBRecipient Item => new LBRecipient(GetProperty("Item")); public LBRecipient Add(string Name) => new LBRecipient(InvokeMethod("Add", Name)); public void Remove(int Index) => InvokeMethod("Remove", Index); public bool ResolveAll() => InvokeMethod("ResolveAll") as bool? ?? false; } public partial class LBAttachment : LBComObject { public LBAttachment() { } public LBAttachment(object item) : base(item) { } public LBOlAttachmentType Type => (LBOlAttachmentType)GetProperty("Type"); } public partial class LBRecipient : LBComObject { public LBRecipient() { } public LBRecipient(object item) : base(item) { } public string Address => (GetProperty("Address").ToString()); public string Name => (GetProperty("Name").ToString()); public bool Resolve() => InvokeMethod("Resolve") as bool? ?? false; } public enum LBOlAttachmentType { olByValue = 1, olByReference = 4, olEmbeddeditem = 5, olOLE = 6 } }