Constancia

VB_CLS_OUTLOOK

Jul 12th, 2015
2,019
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 17.25 KB | None | 0 0
  1. Imports Microsoft.Office.Interop
  2. Imports Microsoft.Office.Interop.Outlook
  3. 'Imports Microsoft.Office
  4. Imports System
  5.  
  6.  
  7. Public Class OUTLOOK
  8.  
  9.  
  10. #Region "VARIABLES PÚBLICAS"
  11.  
  12.     Public L_Mensajes As List(Of Mensaje)
  13.  
  14. #End Region
  15.  
  16. #Region "VARIABLES PRIVADAS"
  17.  
  18.     Private AppOutlook As Microsoft.Office.Interop.Outlook.Application
  19.     Private oSesion As Microsoft.Office.Interop.Outlook.NameSpace
  20.  
  21.     Private oCarpetaRaiz As Microsoft.Office.Interop.Outlook.MAPIFolder
  22.     Private oBandejaEntrada As Microsoft.Office.Interop.Outlook.MAPIFolder
  23.     Private cl_Mensaje As Mensaje
  24.  
  25.     Private oMensajes As Microsoft.Office.Interop.Outlook.Items
  26.     Private oMensaje As Microsoft.Office.Interop.Outlook.MailItem
  27.  
  28.     Private Arr_Remitentes() As String, Arr_Destinatarios() As String
  29.     Private oMail As Microsoft.Office.Interop.Outlook.MailItem
  30.     Private oRemitente As Microsoft.Office.Interop.Outlook.Recipient
  31.     Private oDestinatarios As Microsoft.Office.Interop.Outlook.Recipients
  32.     Private oDestinatario As Microsoft.Office.Interop.Outlook.Recipient
  33.  
  34.     'Private AppOutlook As Outlook.Application, oSesion As Outlook.NameSpace
  35.     'Private oCuentas As Outlook.Accounts, oCuenta As Outlook.Account, oMail As Outlook.MailItem
  36.     'Private oCuentas As Microsoft.Office.Interop.Outlook.Accounts
  37.     'Private oCuenta As Microsoft.Office.Interop.Outlook.Account
  38.     'Private oCarpeta As Microsoft.Office.Interop.Outlook.MAPIFolder
  39.     'Private oCarpeta2 As Microsoft.Office.Interop.Outlook.MAPIFolder
  40.  
  41. #End Region
  42.  
  43.  
  44.     Public Sub New()
  45.  
  46.         _sError = ""
  47.  
  48.         AppOutlook = New Microsoft.Office.Interop.Outlook.Application
  49.         oSesion = AppOutlook.GetNamespace("MAPI")
  50.  
  51.  
  52.         _sVersion_Outlook = CStr(AppOutlook.Version)
  53.         _iVersion_Outlook = Left(AppOutlook.Version, 2)
  54.  
  55.         '** ? Silenciar Alertas
  56.  
  57.     End Sub
  58.  
  59. #Region "PROPIEDADES"
  60.  
  61.     Private _sError As String
  62.     Public Property sError() As String
  63.         Get
  64.             Return _sError
  65.         End Get
  66.         Set(ByVal value As String)
  67.             _sError = value
  68.         End Set
  69.     End Property
  70.  
  71.     Private _sVersion_Outlook As String
  72.     Public Property sVersion_Outlook() As String
  73.         Get
  74.             Return _sVersion_Outlook
  75.         End Get
  76.         Set(ByVal value As String)
  77.             _sVersion_Outlook = value
  78.         End Set
  79.     End Property
  80.  
  81.     Private _iVersion_Outlook As Integer
  82.     Public Property iVersion_Outlook() As Integer
  83.         Get
  84.             Return _iVersion_Outlook
  85.         End Get
  86.         Set(ByVal value As Integer)
  87.             _iVersion_Outlook = value
  88.         End Set
  89.     End Property
  90.  
  91.     Private _sCarpetaRaiz As String
  92.     Public Property sCarpetaRaiz() As String
  93.         Get
  94.             Return _sCarpetaRaiz
  95.         End Get
  96.         Set(ByVal value As String)
  97.             _sCarpetaRaiz = value
  98.         End Set
  99.     End Property
  100.  
  101.     Private _sBandejaEntrada As String
  102.     Public Property sBandejaEntrada() As String
  103.         Get
  104.             Return _sBandejaEntrada
  105.         End Get
  106.         Set(ByVal value As String)
  107.             _sBandejaEntrada = value
  108.         End Set
  109.     End Property
  110.  
  111.     Private _Filtro_Remite As String
  112.     Public Property Filtro_Remite() As String
  113.         Get
  114.             Return _Filtro_Remite
  115.         End Get
  116.         Set(ByVal value As String)
  117.             _Filtro_Remite = value
  118.         End Set
  119.     End Property
  120.  
  121.  
  122.     Private _bFiltro_Remite_Exacto As Boolean
  123.     Public Property bFiltro_Remite_Exacto() As Boolean
  124.         Get
  125.             Return _bFiltro_Remite_Exacto
  126.         End Get
  127.         Set(ByVal value As Boolean)
  128.             _bFiltro_Remite_Exacto = value
  129.         End Set
  130.     End Property
  131.  
  132.  
  133.     Private _Filtro_Asunto As String
  134.     Public Property Filtro_Asunto() As String
  135.         Get
  136.             Return _Filtro_Asunto
  137.         End Get
  138.         Set(ByVal value As String)
  139.             _Filtro_Asunto = value
  140.         End Set
  141.     End Property
  142.  
  143.  
  144.     Private _bFiltro_Asunto_Exacto As Boolean
  145.     Public Property bFiltro_Asunto_Exacto() As Boolean
  146.         Get
  147.             Return _bFiltro_Asunto_Exacto
  148.         End Get
  149.         Set(ByVal value As Boolean)
  150.             _bFiltro_Asunto_Exacto = value
  151.         End Set
  152.     End Property
  153.  
  154.  
  155.  
  156. #End Region
  157.  
  158. #Region "METODOS PÚBLICOS"
  159.  
  160.    
  161.     Public Sub LeerCorreo()
  162.  
  163.         Dim iMensajes As Integer
  164.         Dim bFiltrado As Boolean
  165.  
  166.         oMensajes = oBandejaEntrada.Items
  167.         oMensajes.Sort("[ReceivedTime]", True)
  168.  
  169.         L_Mensajes = New List(Of Mensaje)
  170.  
  171.         For iMensajes = 1 To oMensajes.Count
  172.  
  173.             cl_Mensaje = New Mensaje
  174.             cl_Mensaje.iMensaje = iMensajes
  175.  
  176.             Try
  177.                 oMensaje = oMensajes.Item(iMensajes)
  178.  
  179.                 bFiltrado = CumpleFiltro()
  180.  
  181.                 If bFiltrado Then
  182.  
  183.                     With cl_Mensaje
  184.                         If _iVersion_Outlook > 13 Then .IdOutlook = oMensaje.ConversationID
  185.                         .ObjMensaje = oMensaje
  186.                         .Remite_Nombre = oMensaje.SenderName
  187.                         .Remite_eMail = oMensaje.SenderEmailAddress
  188.                         .Asunto = oMensaje.Subject
  189.                         .Fecha_Recibido = oMensaje.ReceivedTime
  190.                         .Cuerpo = oMensaje.Body
  191.                         .nAdjuntos = oMensaje.Attachments.Count
  192.                     End With
  193.  
  194.                 End If
  195.  
  196.             Catch ex As System.Exception
  197.                 cl_Mensaje.sError = "ERROR AL LEER EL MENSAJE" & vbCrLf & ex.Message
  198.                 ' MsgBox(cl_Mensaje.sError)
  199.             End Try
  200.  
  201.             If bFiltrado Then L_Mensajes.Add(cl_Mensaje)
  202.  
  203.             'Console.WriteLine("MENSAJE : " & iMensajes)
  204.             'Console.WriteLine("REMITENTE : " & oMensaje.SenderName)
  205.             'Console.WriteLine("ASUNTO : " & oMensaje.Subject)
  206.             'Console.WriteLine("FECHA : " & oMensaje.ReceivedTime)
  207.             ''Console.WriteLine("CUERPO : " & Mensaje.Body)
  208.             'Console.WriteLine(vbCrLf & "*************************************************************" & vbCrLf)
  209.  
  210.         Next
  211.  
  212.     End Sub
  213.  
  214.     Public Sub EnviaMail(StrRemite As String, StrDestinatarios As String, StrAsunto As String, StrCuerpo As String, bHTML As Boolean)
  215.  
  216.         Dim sDest As String
  217.  
  218.         Try
  219.             oMail = AppOutlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)
  220.             oMail.Subject = StrAsunto
  221.             oMail.Body = StrCuerpo
  222.  
  223.             Arr_Remitentes = Split(StrRemite, ";")
  224.             Arr_Destinatarios = Split(StrDestinatarios, ";")
  225.  
  226.             oRemitente = AppOutlook.GetNamespace("MAPI").CreateRecipient(StrRemite)
  227.             oRemitente.Resolve()
  228.             oMail.ReplyRecipients.Add(StrRemite)
  229.  
  230.             oDestinatarios = oMail.Recipients
  231.             For Each sDest In Arr_Destinatarios
  232.                 oDestinatario = oDestinatarios.Add(sDest)
  233.                 oDestinatario.Resolve()
  234.             Next
  235.  
  236.             If (oDestinatario.Resolved) Then
  237.                 oMail.Send()
  238.             Else
  239.                 _sError = "La Dirección del Destinatario es Incorrecta"
  240.             End If
  241.         Catch ex As System.Exception
  242.             _sError = "Error al Enviar el Mensaje"
  243.         End Try
  244.     End Sub
  245.  
  246.     Public Function InicializaCarpetas() As Boolean
  247.  
  248.         Dim iCarp As Integer, iCarp2 As Integer
  249.  
  250.         For iCarp = 1 To oSesion.Folders.Count
  251.  
  252.             If oSesion.Folders(iCarp).Name = _sCarpetaRaiz Then
  253.                 oCarpetaRaiz = oSesion.Folders(iCarp)
  254.  
  255.                 For iCarp2 = 1 To oCarpetaRaiz.Folders.Count
  256.                     If oCarpetaRaiz.Folders(iCarp2).Name = _sBandejaEntrada Then
  257.                         oBandejaEntrada = oCarpetaRaiz.Folders(iCarp2)
  258.                         Return True
  259.                     End If
  260.                 Next
  261.             End If
  262.         Next
  263.  
  264.         Return False
  265.  
  266.     End Function
  267.  
  268.     'Public Sub Conectar()
  269.  
  270.  
  271.     '    Dim iMensajes As Integer
  272.  
  273.     '    BandejaEntrada = oSesion.Folders.Item(1).Folders.Item("Bandeja de entrada")
  274.     '    oMensajes = BandejaEntrada.Items
  275.  
  276.  
  277.     '    For iMensajes = 1 To oMensajes.Count
  278.     '        oMensaje = oMensajes.Item(iMensajes)
  279.  
  280.  
  281.     '        Console.WriteLine("MENSAJE : " & iMensajes)
  282.     '        Console.WriteLine("REMITENTE : " & oMensaje.SenderName)
  283.     '        Console.WriteLine("ASUNTO : " & oMensaje.Subject)
  284.     '        Console.WriteLine("FECHA : " & oMensaje.ReceivedTime)
  285.     '        'Console.WriteLine("CUERPO : " & Mensaje.Body)
  286.     '        Console.WriteLine(vbCrLf & "*************************************************************" & vbCrLf)
  287.  
  288.     '    Next
  289.  
  290.  
  291.     '    'For Each mailboxFolder As MAPIFolder In oSesion.Folders
  292.     '    '    Console.WriteLine("*******************>>" & mailboxFolder.Name)
  293.  
  294.     '    '    For Each inboxFolder As MAPIFolder In mailboxFolder.Folders
  295.     '    '        Console.WriteLine(inboxFolder.Name)
  296.     '    '    Next
  297.  
  298.     '    'Next
  299.  
  300.     'End Sub
  301.  
  302. #End Region
  303.  
  304. #Region "METODOS PRIVADOS"
  305.  
  306.  
  307.     Private Function CumpleFiltro() As Boolean
  308.  
  309.         Dim bCumple As Boolean = True
  310.  
  311.         If _Filtro_Asunto <> "" Then
  312.  
  313.             If bFiltro_Asunto_Exacto Then
  314.                 If Not Trim(UCase(_Filtro_Asunto)) = Trim(UCase(oMensaje.Subject)) Then bCumple = False
  315.             Else
  316.                 If InStr(1, Trim(UCase(oMensaje.Subject)), Trim(UCase(_Filtro_Asunto))) = 0 Then bCumple = False
  317.             End If
  318.  
  319.         End If
  320.  
  321.         If Not bCumple Then Return bCumple
  322.  
  323.         If _Filtro_Remite <> "" Then
  324.  
  325.             If bFiltro_Remite_Exacto Then
  326.                 If Not Trim(UCase(_Filtro_Remite)) = Trim(UCase(oMensaje.SenderName)) Then bCumple = False
  327.             Else
  328.                 If InStr(1, Trim(UCase(oMensaje.SenderName)), Trim(UCase(_Filtro_Remite))) = 0 Then bCumple = False
  329.             End If
  330.  
  331.         End If
  332.  
  333.         Return bCumple
  334.  
  335.     End Function
  336.  
  337.  
  338. #End Region
  339.  
  340.     '************************************* CLASE MENSAJE *******************************************************
  341.  
  342.     Public Class Mensaje
  343.  
  344.  
  345. #Region "VARIABLES PÚBLICAS"
  346.  
  347.         Public L_Adjuntos As List(Of Adjunto)
  348.  
  349. #End Region
  350.  
  351. #Region "VARIABLES PRIVADAS"
  352.  
  353.         Private obAdjunto As Microsoft.Office.Interop.Outlook.Attachment
  354.         Private cl_Adjunto As Adjunto
  355.  
  356. #End Region
  357.  
  358.  
  359. #Region "PROPIEDADES"
  360.  
  361.  
  362.         '*************************************************************************
  363.         '******************* PROPIEDADES *****************************************
  364.         '*************************************************************************
  365.  
  366.  
  367.         Private _IdOutlook As String
  368.         Public Property IdOutlook() As String
  369.             Get
  370.                 Return _IdOutlook
  371.             End Get
  372.             Set(ByVal value As String)
  373.                 _IdOutlook = value
  374.             End Set
  375.         End Property
  376.  
  377.         Private _ObjMensaje As Microsoft.Office.Interop.Outlook.MailItem
  378.         Public Property ObjMensaje() As Microsoft.Office.Interop.Outlook.MailItem
  379.             Get
  380.                 Return _ObjMensaje
  381.             End Get
  382.             Set(ByVal value As Microsoft.Office.Interop.Outlook.MailItem)
  383.                 _ObjMensaje = value
  384.             End Set
  385.         End Property
  386.  
  387.         Private _iMensaje As Integer
  388.         Public Property iMensaje() As Integer
  389.             Get
  390.                 Return _iMensaje
  391.             End Get
  392.             Set(ByVal value As Integer)
  393.                 _iMensaje = value
  394.             End Set
  395.         End Property
  396.  
  397.         Private _sError As String
  398.         Public Property sError() As String
  399.             Get
  400.                 Return _sError
  401.             End Get
  402.             Set(ByVal value As String)
  403.                 _sError = value
  404.             End Set
  405.         End Property
  406.  
  407.         Private _Remite_Nombre As String
  408.         Public Property Remite_Nombre() As String
  409.             Get
  410.                 Return _Remite_Nombre
  411.             End Get
  412.             Set(ByVal value As String)
  413.                 _Remite_Nombre = value
  414.             End Set
  415.         End Property
  416.  
  417.         Private _Remite_eMail As String
  418.         Public Property Remite_eMail() As String
  419.             Get
  420.                 Return _Remite_eMail
  421.             End Get
  422.             Set(ByVal value As String)
  423.                 _Remite_eMail = value
  424.             End Set
  425.         End Property
  426.  
  427.         Private _Asunto As String
  428.         Public Property Asunto() As String
  429.             Get
  430.                 Return _Asunto
  431.             End Get
  432.             Set(ByVal value As String)
  433.                 _Asunto = value
  434.             End Set
  435.         End Property
  436.  
  437.         Private _Fecha_Recibido As Date
  438.         Public Property Fecha_Recibido() As Date
  439.             Get
  440.                 Return _Fecha_Recibido
  441.             End Get
  442.             Set(ByVal value As Date)
  443.                 _Fecha_Recibido = value
  444.             End Set
  445.         End Property
  446.  
  447.         Private _Cuerpo As String
  448.         Public Property Cuerpo() As String
  449.             Get
  450.                 Return _Cuerpo
  451.             End Get
  452.             Set(ByVal value As String)
  453.                 _Cuerpo = value
  454.             End Set
  455.         End Property
  456.  
  457.         Private _nAdjuntos As Integer
  458.         Public Property nAdjuntos() As Integer
  459.             Get
  460.                 Return _nAdjuntos
  461.             End Get
  462.             Set(ByVal value As Integer)
  463.                 _nAdjuntos = value
  464.  
  465.                 If _nAdjuntos > 0 Then
  466.                     Call CargaAdjuntos()
  467.                 End If
  468.  
  469.             End Set
  470.         End Property
  471.  
  472. #End Region
  473.  
  474.  
  475. #Region "METODOS PÚBLICOS"
  476.  
  477.         Public Sub New()
  478.  
  479.             _sError = ""
  480.  
  481.         End Sub
  482.  
  483.         Public Function ExtraerAdjunto(RutaDestino As String, NomFichero As String, Optional bExacto As Boolean = False) As Boolean
  484.  
  485.             On Error GoTo TrataError
  486.  
  487.             For i As Integer = 0 To Me.L_Adjuntos.Count - 1
  488.  
  489.                 If bExacto Then
  490.                     If Me.L_Adjuntos(i).Fichero = NomFichero Then
  491.                         Me.L_Adjuntos(i).oAdjunto.SaveAsFile(RutaDestino & Me.L_Adjuntos(i).Fichero)
  492.                         Return True
  493.  
  494.                     End If
  495.                 Else
  496.                     If InStr(1, Me.L_Adjuntos(i).Fichero, NomFichero) > 0 Then
  497.                         Me.L_Adjuntos(i).oAdjunto.SaveAsFile(RutaDestino & Me.L_Adjuntos(i).Fichero)
  498.                         Return True
  499.                     End If
  500.                 End If
  501.  
  502.             Next
  503.  
  504.             _sError = "OUTLOOK.ExtraerAdjunto() : No se Encuentra el Archivo Adjunto (" & NomFichero & ")" : Return False
  505.  
  506.             Exit Function
  507. TrataError:
  508.             _sError = "OUTLOOK.ExtraerAdjunto() : " & Err.Description : Return False
  509.         End Function
  510.  
  511.  
  512. #End Region
  513.  
  514.  
  515. #Region "METODOS PRIVADOS"
  516.  
  517.         Private Sub CargaAdjuntos()
  518.  
  519.             L_Adjuntos = New List(Of Adjunto)
  520.  
  521.             For i As Integer = 1 To _nAdjuntos
  522.  
  523.                 obAdjunto = Me.ObjMensaje.Attachments.Item(i)
  524.  
  525.                 cl_Adjunto = New Adjunto
  526.                 cl_Adjunto.iAdjunto = i
  527.  
  528.                 Try
  529.                     With cl_Adjunto
  530.                         .Fichero = obAdjunto.FileName
  531.                         .Tamanno = obAdjunto.Size
  532.                         .oAdjunto = obAdjunto
  533.                     End With
  534.  
  535.                 Catch ex As System.Exception
  536.                     cl_Adjunto.sError = "ERROR AL COMPROBAR EL ADJUNTO" & vbCrLf & ex.Message
  537.                 End Try
  538.  
  539.                 L_Adjuntos.Add(cl_Adjunto)
  540.  
  541.             Next
  542.  
  543.             obAdjunto = Nothing
  544.  
  545.         End Sub
  546.  
  547. #End Region
  548.  
  549.  
  550.     End Class
  551.  
  552.  
  553.     '************************************* CLASE ADJUNTO *******************************************************
  554.  
  555.     Public Class Adjunto
  556.  
  557. #Region "PROPIEDADES"
  558.  
  559.         Private _oAdjunto As Microsoft.Office.Interop.Outlook.Attachment
  560.         Public Property oAdjunto() As Microsoft.Office.Interop.Outlook.Attachment
  561.             Get
  562.                 Return _oAdjunto
  563.             End Get
  564.             Set(ByVal value As Microsoft.Office.Interop.Outlook.Attachment)
  565.                 _oAdjunto = value
  566.             End Set
  567.         End Property
  568.  
  569.         Private _iAdjunto As Integer
  570.         Public Property iAdjunto() As Integer
  571.             Get
  572.                 Return _iAdjunto
  573.             End Get
  574.             Set(ByVal value As Integer)
  575.                 _iAdjunto = value
  576.             End Set
  577.         End Property
  578.  
  579.         Private _sError As String
  580.         Public Property sError() As String
  581.             Get
  582.                 Return _sError
  583.             End Get
  584.             Set(ByVal value As String)
  585.                 _sError = value
  586.             End Set
  587.         End Property
  588.  
  589.         Private _Fichero As String
  590.         Public Property Fichero() As String
  591.             Get
  592.                 Return _Fichero
  593.             End Get
  594.             Set(ByVal value As String)
  595.                 _Fichero = value
  596.             End Set
  597.         End Property
  598.  
  599.         Private _Tamanno As Integer
  600.         Public Property Tamanno() As Integer
  601.             Get
  602.                 Return _Tamanno
  603.             End Get
  604.             Set(ByVal value As Integer)
  605.                 _Tamanno = value
  606.             End Set
  607.         End Property
  608.  
  609.  
  610. #End Region
  611.  
  612.  
  613.     End Class
  614.  
  615. End Class
Advertisement
Add Comment
Please, Sign In to add comment