public bool IniciarSesion(string cuenta, string contrasena) { bool resultado = true; _cuenta = cuenta; try { //Return a reference to the MAPI layer oApp = new Microsoft.Office.Interop.Outlook.Application(); oNameSpace = oApp.GetNamespace("MAPI"); /*********************************************************************** * Logs on the user * Profile: Set to null if using the currently logged on user, or set * to an empty string ("") if you wish to use the default Outlook Profile. * Password: Set to null if using the currently logged on user, or set * to an empty string ("") if you wish to use the default Outlook Profile * password. * ShowDialog: Set to True to display the Outlook Profile dialog box. * NewSession: Set to True to start a new session. Set to False to * use the current session. ***********************************************************************/ oNameSpace.Logon(_cuenta, contrasena, false, true); //gets defaultfolder for my Outlook Outbox oOutboxFolder = oNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderOutbox); } catch (Exception) { resultado = false; } return(resultado); }
public OutlookPortal() { oApp = new Microsoft.Office.Interop.Outlook.Application(); oNameSpace = oApp.GetNamespace("MAPI"); oOutboxFolder = oNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderOutbox); oNameSpace.Logon(null, null, false, false); oMailItem = (Microsoft.Office.Interop.Outlook._MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem); }
/// <summary> /// Saves an email object of type Microsoft.Office.Interop.Outlook.MailItem to Drafts Box /// </summary> /// <param name="new_message">Microsoft.Office.Interop.Outlook.MailItem object</param> /// <returns>True/False on Success/Failure</returns> protected bool Save_to_Draftbox(_MI_ new_message) { Microsoft.Office.Interop.Outlook.Application app = null; Microsoft.Office.Interop.Outlook._NameSpace nameSpace = null; Microsoft.Office.Interop.Outlook.MAPIFolder drafts = null; try { app = new Microsoft.Office.Interop.Outlook.Application(); nameSpace = app.GetNamespace("MAPI"); nameSpace.Logon(null, null, false, false); drafts = nameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderDrafts); new_message.Save(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); return(false); } return(true); }
/// <summary> /// Constructor /// </summary> public OutlookMail() { //Return a reference to the MAPI layer oApp = new Microsoft.Office.Interop.Outlook.Application(); oNameSpace= oApp.GetNamespace("MAPI"); /*********************************************************************** * Logs on the user * Profile: Set to null if using the currently logged on user, or set * to an empty string ("") if you wish to use the default Outlook Profile. * Password: Set to null if using the currently logged on user, or set * to an empty string ("") if you wish to use the default Outlook Profile * password. * ShowDialog: Set to True to display the Outlook Profile dialog box. * NewSession: Set to True to start a new session. Set to False to * use the current session. ***********************************************************************/ oNameSpace.Logon(null,null,true,true); //gets defaultfolder for my Outlook Outbox oSentItems = oNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderSentMail); }
/// <summary> /// Takes an Outlook email object saves the attachemnts /// </summary> /// <param name="email_obj">Microsoft.Office.Interop.Outlook.MailItem object</param> /// <param name="query">String to search for in Subject</param> /// <returns>True/False on Success/Failure</returns> /* protected string Save_Attachments(_MI_ email_obj, bool hasFinalNotices) * { * System.Collections.IEnumerator enumer = email_obj.Attachments.GetEnumerator(); * int count = 1; * * while (enumer.MoveNext()) * { * * if (email_obj.Attachments[count].FileName.Contains(".zip") || email_obj.Attachments[count].FileName.Contains(".ZIP")) * { * email_obj.Attachments[count].SaveAsFile(UNZIP_PATH + email_obj.Attachments[count].FileName); * Console.WriteLine("Saving file as {0}", email_obj.Attachments[count].FileName); * Console.WriteLine("We got the IAVA Finals. Unzipping"); * Console.WriteLine("Attempting to unzip {0}", UNZIP_PATH + email_obj.Attachments[count].FileName); * LocalStorage ls = new LocalStorage(UNZIP_PATH); * string daterange = ls.CreateDateRangeName(); * ls.ExtractZipFile(UNZIP_PATH + email_obj.Attachments[count].FileName, daterange); * return UNZIP_PATH + daterange; * } * count++; * } * * return null; * }*/ /// <summary> /// Search a specified Subfolder in Outlook /// </summary> /// <param name="subfolder_name">Name of Outlook Subfolder to search</param> /// <param name="query">Text to query</param> /// <param name="search_location">Location to search: {(0, Sender), (1, Subject), (2, Body)}</param> /// <param name="only_unread">True/False - Search only Unread Messages/Search All</param> /// <returns>A list of _MI_ objects representing matching emails</returns> private List <_MI_> _Search_Subfolder(string subfolder_name, string query, int search_location, bool only_unread) { List <_MI_> results = new List <_MI_>(); try { Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application(); Microsoft.Office.Interop.Outlook._NameSpace nameSpace = null; Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null; Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null; _MI_ item = null; nameSpace = app.GetNamespace("MAPI"); nameSpace.Logon(null, null, false, false); inboxFolder = nameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox); try { subFolder = inboxFolder.Folders[subfolder_name]; } catch (Exception not_found) { Console.WriteLine(not_found.ToString()); } for (int i = 1; i <= subFolder.Items.Count; i++) { item = (_MI_)subFolder.Items[i]; query = query.Trim(); switch (search_location) { case 0: Console.WriteLine("Sender: {0}", item.SenderEmailAddress); Console.WriteLine("Other Sender: {0}", item.SenderName); if (item.SenderEmailAddress.ToLower().Contains(query.ToLower())) { if ((only_unread == false) || (only_unread == true && item.UnRead == true)) { results.Add(item); } } break; case 1: if (item.Subject.ToLower().Contains(query.ToLower())) { if ((only_unread == false) || (only_unread == true && item.UnRead == true)) { results.Add(item); } } break; case 2: if (item.Body.ToLower().Contains(query.ToLower())) { if ((only_unread == false) || (only_unread == true && item.UnRead == true)) { results.Add(item); } } break; default: throw new ArgumentException("Error in Query Location"); } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } return(results); }
/// <summary> /// Metodo que descarga los archivos adjuntos de los correos /// no leidos de la cuenta de outlook /// </summary> public void bandejaEntradaOutlook() { Microsoft.Office.Interop.Outlook.Application app = null; Microsoft.Office.Interop.Outlook._NameSpace ns = null; Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null; Microsoft.Office.Interop.Outlook.MailItem sinLeer; int p = 0, t = 0; while (!detenerHilo) { try { app = new Microsoft.Office.Interop.Outlook.Application(); ns = app.GetNamespace("MAPI"); ns.Logon(null, null, false, false); inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook. OlDefaultFolders.olFolderInbox); ///Se obtiene la bandeja de entrada de la cuenta de correo Microsoft.Office.Interop.Outlook.Items inboxItems = inboxFolder.Items; t = inboxFolder.UnReadItemCount; ///Se obtiene la bandeja de entrada de correos no leidos inboxItems = inboxItems.Restrict("[unread] = true"); while (p < t) { if (p == 0) { ///Se obtiene el primer elemento de la bandeja de entrada sinLeer = inboxItems.GetFirst(); } else { ///Se obtiene el elemento siguiente de la bandeja de entrada sinLeer = inboxItems.GetNext(); } ///Se obtiene los archivos adjuntos del correo Microsoft.Office.Interop.Outlook.Attachments adjuntos = sinLeer.Attachments; foreach (Microsoft.Office.Interop.Outlook.Attachment archivo in adjuntos) { if (ValidarXmlSobre(archivo.FileName)) { ///Se marca el correo como no leido sinLeer.UnRead = false; //Se descargar el archivo adjunto del correo archivo.SaveAsFile(RutasCarpetas.RutaCarpetaBandejaEntrada + archivo.FileName); string desde = sinLeer.Sender.ToString(); //Se sube el archivo al servidor FTP FTP ftp = new FTP(); ftp.CargarArchivos(archivo.FileName, RutasCarpetas.RutaCarpetaBandejaEntrada, 3); //Se guarda en la tabla de sobres Recibidos respuestaSobre.GenerarXML(RutasCarpetas.RutaCarpetaBandejaEntrada, archivo.FileName, desde); } //Se comprueba que sea un ACK else if (ValidarXmlACKSobre(archivo.FileName)) { archivo.SaveAsFile(RutasCarpetas.RutaCarpetaBandejaEntrada); sinLeer.UnRead = false; SobreTransito sobreTransito = ObtenerSobreTransito(archivo.FileName, sinLeer.Sender.ToString()); ManteUdoSobreTransito manteSobreTransito = new ManteUdoSobreTransito(); manteSobreTransito.Almacenar(sobreTransito); } } p = p + 1; } } catch (Exception) { } finally { ns = null; app = null; inboxFolder = null; Thread.Sleep(60000); } } }