static void Main(string[] args) { Microsoft.Office.Interop.Outlook.Application myApp = new Microsoft.Office.Interop.Outlook.Application(); Microsoft.Office.Interop.Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI"); Microsoft.Office.Interop.Outlook.MAPIFolder myContacts = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts); mapiNameSpace.Logon(null, null, false, false);//"*****@*****.**""14March96kr#" mapiNameSpace.Logon(ConfigurationSettings.AppSettings["MailId"], ConfigurationSettings.AppSettings["Password"], Missing.Value, true); Microsoft.Office.Interop.Outlook.Items myItems = myContacts.Items; myInbox = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox); destFolder = myInbox.Folders["MaliciousAttachments"]; Microsoft.Office.Interop.Outlook.Items inboxItems = myInbox.Items; Microsoft.Office.Interop.Outlook.Explorer myexp = myInbox.GetExplorer(false); mapiNameSpace.Logon("Profile", Missing.Value, false, true); new Program().readingUnReadMailsAsync(inboxItems); }
private static readonly object _lock = new object(); // Sync the processing of items in the Outbox public OutboxMonitor(Microsoft.Office.Interop.Outlook.Application application) { try { _application = application; _ns = _application.GetNamespace("MAPI"); _outbox = _ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderOutbox); _items = _outbox.Items; _items.ItemAdd += Items_ItemAdd; SubmitNonDeferredItems(); SyncroniseDeferredSendStore(); CreateSubmitWorkerThreads(); Logger.LogInfo("Secure File Transfer Outbox monitor created and configured."); } catch (Exception ex) { Dispose(); Logger.LogError(ex); Logger.LogError("Failed to create OutboxMonitor. Disable DeferredSend"); throw; } }
/// <summary> /// para enviar correos desde la cuenta del outlook instalada en tu ordinador /// </summary> /// <param name="mailDirection"> el destino del correo </param> /// <param name="mailSubject"> el asunto del correo </param> /// <param name="mailContent">el contenido del correo </param> public string sendMail(string mailDirection, string mailSubject, string mailContent) { try { var oApp = new Microsoft.Office.Interop.Outlook.Application(); Microsoft.Office.Interop.Outlook.NameSpace ns = oApp.GetNamespace("MAPI"); var f = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox); System.Threading.Thread.Sleep(1000); var mailItem = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem); mailItem.Subject = mailSubject; mailItem.HTMLBody = mailContent; mailItem.To = mailDirection; mailItem.Send(); return(string.Empty); } catch (Exception ex) { //MessageBox.Show(ex.Message); return(ex.Message); } }
private static Appointment LookForNextAppointment(string textToSearch) { Microsoft.Office.Interop.Outlook.Application oApp = null; Microsoft.Office.Interop.Outlook.NameSpace mapiNamespace = null; Microsoft.Office.Interop.Outlook.MAPIFolder CalendarFolder = null; Microsoft.Office.Interop.Outlook.Items outlookCalendarItems = null; oApp = new Microsoft.Office.Interop.Outlook.Application(); mapiNamespace = oApp.GetNamespace("MAPI"); ; CalendarFolder = mapiNamespace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar); outlookCalendarItems = CalendarFolder.Items; outlookCalendarItems.Sort("[Start]"); var now = DateTime.Now.Date.ToString("g"); var nowIn1Year = DateTime.Now.Date.AddYears(1).ToString("g"); outlookCalendarItems = outlookCalendarItems.Restrict($"[Start] >= \"{now}\" AND [End] < \"{nowIn1Year}\" "); foreach (Microsoft.Office.Interop.Outlook.AppointmentItem item in outlookCalendarItems) { var subject = item.Subject; if (subject.IndexOf(textToSearch,StringComparison.InvariantCultureIgnoreCase)!=-1) { return new Appointment() { Subject = subject, Start = item.Start }; } } return null; }
public static void CheckMailToExecute() { try { Microsoft.Office.Interop.Outlook.Application myApp = new Microsoft.Office.Interop.Outlook.ApplicationClass(); Microsoft.Office.Interop.Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI"); Microsoft.Office.Interop.Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox); Microsoft.Office.Interop.Outlook.Items oItems = myInbox.Items.Restrict("[UnRead] = True"); foreach (var item in oItems) { if (item is Microsoft.Office.Interop.Outlook.MailItem) { Microsoft.Office.Interop.Outlook.MailItem mail = (Microsoft.Office.Interop.Outlook.MailItem)item; if (mail.UnRead && mail.Subject.StartsWith("MC")) { mail.UnRead = false; ExecuteMail(mail); } } } } catch (Exception ex) { Logger.Log(ex.Message); } }
public Outlook() { _ns = _application.Session; _inbox = _ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox); _items = _inbox.Items; foreach (var item in _items) { var mail = item as Microsoft.Office.Interop.Outlook.MailItem; var subject = mail.Subject; } }
/// <summary> /// Gets a list of all events in outlook /// </summary> public static List <CalendarEvent> GetAllEvents() { List <CalendarEvent> events = new List <CalendarEvent>(); Microsoft.Office.Interop.Outlook.Application oApp = null; Microsoft.Office.Interop.Outlook.NameSpace mapiNamespace = null; Microsoft.Office.Interop.Outlook.MAPIFolder CalendarFolder = null; Microsoft.Office.Interop.Outlook.Items outlookCalendarItems = null; oApp = new Microsoft.Office.Interop.Outlook.Application(); mapiNamespace = oApp.GetNamespace("MAPI"); CalendarFolder = mapiNamespace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar); outlookCalendarItems = CalendarFolder.Items; outlookCalendarItems.IncludeRecurrences = true; CalendarEvent cEvent = null; foreach (Microsoft.Office.Interop.Outlook.AppointmentItem item in outlookCalendarItems) { cEvent = null; if (item.IsRecurring) { Microsoft.Office.Interop.Outlook.RecurrencePattern rp = item.GetRecurrencePattern(); DateTime first = new DateTime(2008, 8, 31, item.Start.Hour, item.Start.Minute, 0); DateTime last = new DateTime(2008, 10, 1); Microsoft.Office.Interop.Outlook.AppointmentItem recur = null; for (DateTime cur = first; cur <= last; cur = cur.AddDays(1)) { try { recur = rp.GetOccurrence(cur); cEvent = new CalendarEvent(recur.GlobalAppointmentID, recur.Start, recur.End, recur.Location, recur.Subject, recur.Body); } catch { } } } else { cEvent = new CalendarEvent(item.GlobalAppointmentID, item.Start, item.End, item.Location, item.Subject, item.Body); } if (cEvent != null) { events.Add(cEvent); } } return(events); }
protected void outlookButton_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Outlook.Application oApp = null; Microsoft.Office.Interop.Outlook.NameSpace mapiNamespace = null; Microsoft.Office.Interop.Outlook.MAPIFolder CalendarFolder = null; Microsoft.Office.Interop.Outlook.Items outlookCalendarItems = null; oApp = new Microsoft.Office.Interop.Outlook.Application(); mapiNamespace = oApp.GetNamespace("MAPI");; CalendarFolder = mapiNamespace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar); outlookCalendarItems = CalendarFolder.Items; outlookCalendarItems.IncludeRecurrences = true; allCalendarItemsText.Text = ""; foreach (Microsoft.Office.Interop.Outlook.AppointmentItem item in outlookCalendarItems) { allCalendarItemsText.Text += item.Subject + " -> " + item.Start.ToLongDateString(); } }
private void btnRead_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Outlook.Application myApp = new Microsoft.Office.Interop.Outlook.Application(); Microsoft.Office.Interop.Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI"); Microsoft.Office.Interop.Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox); Microsoft.Office.Interop.Outlook.MAPIFolder oPublicFolder = myInbox.Parent; cmbOutlookFolders.Items.Clear(); foreach (Microsoft.Office.Interop.Outlook.MAPIFolder folder in oPublicFolder.Folders) { EnumerateFolders(folder); } cmbOutlookFolders.DisplayMember = "Value"; cmbOutlookFolders.ValueMember = "Key"; }
public static void GetAllTaskItems() { Microsoft.Office.Interop.Outlook.Application oApp = null; Microsoft.Office.Interop.Outlook.NameSpace mapiNamespace = null; Microsoft.Office.Interop.Outlook.MAPIFolder CalendarFolder = null; Microsoft.Office.Interop.Outlook.Items outlookCalendarItems = null; oApp = new Microsoft.Office.Interop.Outlook.Application(); mapiNamespace = oApp.GetNamespace("MAPI"); ; CalendarFolder = mapiNamespace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderTasks); outlookCalendarItems = CalendarFolder.Items; outlookCalendarItems.IncludeRecurrences = true; foreach (Microsoft.Office.Interop.Outlook.TaskItem item in outlookCalendarItems) { if (item.IsRecurring) { //Microsoft.Office.Interop.Outlook.RecurrencePattern rp = item.GetRecurrencePattern(); //DateTime first = new DateTime(2008, 8, 31, item.Start.Hour, item.Start.Minute, 0); //DateTime last = new DateTime(2008, 10, 1); //Microsoft.Office.Interop.Outlook.AppointmentItem recur = null; //for (DateTime cur = first; cur <= last; cur = cur.AddDays(1)) //{ // try // { // recur = rp.GetOccurrence(cur); // Console.WriteLine(recur.Subject + " -> " + cur.ToLongDateString()); // } // catch // { // } //} } else { Console.WriteLine(item.Subject + " -> " /* + item.Start.ToLongDateString()*/); } } }
public Outlook() { _ns = _application.Session; _inbox = _ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox); _items = _inbox.Items; foreach (var item in _items) { string subject = string.Empty; var mail = item as Microsoft.Office.Interop.Outlook.MailItem; if (mail != null) { var subject = mail.Subject; } else { Debug.WriteLine("Item is not a MailItem"); } } }
private void InitOutlookService() { if (!ifAlreadyInit) { oApp = new Microsoft.Office.Interop.Outlook.Application(); mapiNamespace = oApp.GetNamespace("MAPI"); ; calendarFolder = mapiNamespace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar); outlookCalendarItems = calendarFolder.Items; outlookCalendarItems.Sort("[Start]"); outlookCalendarItems.IncludeRecurrences = true; string s1 = GetDateInString(minTime); string s2 = GetDateInString(maxTime); var filterString = "[Start] >= '" + s1 + "' AND [End] < '" + s2 + "'"; outlookCalendarItems = outlookCalendarItems.Restrict(filterString); ifAlreadyInit = true; } }
/// <summary> /// para enviar correos desde la cuenta del outlook instalada en tu ordinador /// </summary> /// <param name="mailDirection"> el destino del correo </param> /// <param name="cc">el cc del correo </param> /// <param name="cco">el cco del correo </param> /// <param name="mailSubject"> el asunto del correo </param> /// <param name="mailContent">el contenido del correo </param> /// <param name="adjuntos">los archivos adjutnos del correo </param> public string sendMail(string mailDirection, string cc, string cco, string mailSubject, string mailContent, List <string> adjuntos) { try { var oApp = new Microsoft.Office.Interop.Outlook.Application(); Microsoft.Office.Interop.Outlook.NameSpace ns = oApp.GetNamespace("MAPI"); var f = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox); System.Threading.Thread.Sleep(1000); var mailItem = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem); mailItem.Subject = mailSubject; mailItem.HTMLBody = mailContent; mailItem.To = mailDirection; mailItem.CC = cc; mailItem.BCC = cco; if (adjuntos != null && adjuntos.Count() > 0) { foreach (string adj in adjuntos) { mailItem.Attachments.Add(adj); } } mailItem.Send(); return(string.Empty); } catch (Exception ex) { //MessageBox.Show(ex.Message); return(ex.Message); } }
public void read() { Microsoft.Office.Interop.Outlook.Application outlook = new Microsoft.Office.Interop.Outlook.Application(); Microsoft.Office.Interop.Outlook.NameSpace ns = outlook.GetNamespace("Mapi"); object _missing = Type.Missing; ns.Logon(_missing, _missing, false, true); Microsoft.Office.Interop.Outlook.MAPIFolder inbox = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox); string foldername = inbox.Name; int iMailCount = inbox.Items.Count; TraceService("Inside read method"); for (int iCount = 1; iCount <= iMailCount; iCount++) { Object mail1 = inbox.Items[iCount]; if (((mail1 as Microsoft.Office.Interop.Outlook.MailItem) != null) && ((mail1 as Microsoft.Office.Interop.Outlook.MailItem).UnRead == true)) { TraceService("Inside unread mail"); Microsoft.Office.Interop.Outlook.MailItem mail = (Microsoft.Office.Interop.Outlook.MailItem)inbox.Items[iCount]; SqlConnection con; string connection = @"server=CSM-DEV\SQL2008;database=TerexBest;uid=sa;pwd=rimc@123"; TraceService("Connection with database done1"); con = new SqlConnection(connection); SqlCommand cmd; cmd = new SqlCommand(); con.Open(); cmd.Connection = con; TraceService("Connection assigned to sql command"); string subject = mail.Subject.ToString(); TraceService("mail subject written" + subject); string body = mail.Body.ToString(); cmd.Parameters.Add("@subject", SqlDbType.NVarChar).Value = subject; TraceService(subject); //writing subject cmd.Parameters.Add("@body", SqlDbType.NVarChar).Value = body; TraceService(body); //writing subject cmd.Parameters.Add("@recievedtime", SqlDbType.DateTime).Value = mail.ReceivedTime; TraceService(mail.ReceivedTime.ToString()); //writing subject cmd.Parameters.Add("@mailfrom", SqlDbType.NVarChar).Value = mail.SenderEmailAddress; TraceService(mail.SenderEmailAddress); //writing subject TraceService("Before Inventory saved"); cmd.CommandText = "insert into storemail(subject,body,createddatetime,mailfrom,isActive) values(@subject,@body,@recievedtime,@mailfrom,1)"; TraceService("Inventory saved"); try { cmd.ExecuteNonQuery(); mail.Delete(); iMailCount = iMailCount - 1; } catch (SqlException ex) { ex.ToString(); } con.Close(); cmd.Dispose(); con.Dispose(); } } GC.Collect(); inbox = null; ns = null; outlook = null; }
public void CreateNewDistributionList(string[] tabAdresse) { Microsoft.Office.Interop.Outlook._Application OutlookApp = new Microsoft.Office.Interop.Outlook.Application(); Microsoft.Office.Interop.Outlook.NameSpace ns = null; Microsoft.Office.Interop.Outlook.MAPIFolder folderContacts = null; Microsoft.Office.Interop.Outlook.Items contactItems = null; Microsoft.Office.Interop.Outlook.MailItem mail = null; Microsoft.Office.Interop.Outlook.Recipients listRecipients = null; Microsoft.Office.Interop.Outlook.DistListItem distributionList = null; try { string addresseToAdd; ns = OutlookApp.GetNamespace("MAPI"); folderContacts = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts); contactItems = folderContacts.Items; // create a new e-mail message to access the recipients collection mail = contactItems.Add(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem) as Microsoft.Office.Interop.Outlook.MailItem; mail.To = ""; listRecipients = mail.Recipients; //création de la liste d'adresse foreach (string addresse in tabAdresse) { if (addresse != null) { addresseToAdd = RemoveDiacritics(addresse); listRecipients.Add(addresseToAdd); } } if (!listRecipients.ResolveAll()) { System.Windows.Forms.MessageBox.Show("There are no such contact names. " + "Please make sure that you have corresponding records in your address book", "Add-in Express", MessageBoxButtons.OK, MessageBoxIcon.Warning); } // create a new distribution list item distributionList = contactItems.Add(Microsoft.Office.Interop.Outlook.OlItemType.olDistributionListItem) as Microsoft.Office.Interop.Outlook.DistListItem; distributionList.DLName = "List judo"; distributionList.AddMembers(listRecipients); distributionList.Display(true); } catch (Exception e) { throw new Exception("Erreur Metier.cs/CreateNewDistributionList():\r\n" + e.Message, e); } finally { if (distributionList != null) { Marshal.ReleaseComObject(distributionList); } if (listRecipients != null) { Marshal.ReleaseComObject(listRecipients); } if (mail != null) { mail.Delete(); Marshal.ReleaseComObject(mail); } if (contactItems != null) { Marshal.ReleaseComObject(contactItems); } if (folderContacts != null) { Marshal.ReleaseComObject(folderContacts); } if (ns != null) { Marshal.ReleaseComObject(ns); } } }
public static void UpdateCache(bool resetException = false) { if (resetException) { OutlookException = null; } if (OutlookException != null) { return; } var events = new List <OutlookItem>(); Microsoft.Office.Interop.Outlook.Application oApp = null; Microsoft.Office.Interop.Outlook.NameSpace mapiNamespace = null; Microsoft.Office.Interop.Outlook.MAPIFolder calendarFolder = null; Microsoft.Office.Interop.Outlook.Items outlookCalendarItems = null; try { oApp = new Microsoft.Office.Interop.Outlook.Application(); mapiNamespace = oApp.GetNamespace("MAPI"); calendarFolder = mapiNamespace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar); outlookCalendarItems = calendarFolder.Items; outlookCalendarItems.Sort("[Start]"); outlookCalendarItems.IncludeRecurrences = true; //var filter = String.Format("[Start] >= \"{0}\" and [Start] <= \"{1}\"", DateTime.Today, DateTime.Today.AddDays(1)); //outlookCalendarItems = outlookCalendarItems.Find(filter); foreach (Microsoft.Office.Interop.Outlook.AppointmentItem item in outlookCalendarItems) { if (item.Start >= CacheRangeStart) { if (item.Start.Date > CacheRangeEnd) { break; } var parsed = LyncMeeting.ParseEmail(item.Body ?? string.Empty, item.Subject); events.Add(new OutlookItem() { Start = item.Start, End = item.End, Subject = item.Subject, LyncMeeting = parsed, OutlookAppointment = item }); } } events = events.OrderBy(e => e.Start).ToList(); lock (Lock) { _cache = events; _cacheLastUpdateTime = DateTime.Now; } } catch (COMException e) { OutlookException = e; } finally { Marshal.ReleaseComObject(oApp); } }