Einträge mit Tag “domino”

VB .NET - Mit Lotus Notes E-Mails und Dateianhängen arbeiten

29. Dec 2008 12:05 (bearbeiten)

Mit den folgenden Klassen ist es möglich, aus einer Domäne heraus, eine beliebige Lotus Notes-Datenbank zu öffnen und alle E-Mails darin aufzulisten. Es werden dabei alle Dateianhänge aus den E-Mails in eine Liste gefügt.

Ein Beispielaufruf findet sich am Ende dieses Artikels.

  1. Public Class NotesMail
  2.  
  3. Public Subject As String
  4. Public From As String
  5. Public Body As String
  6. Public AttachmentFilenames As New List(Of Domino.NotesEmbeddedObject)
  7.  
  8. End Class
  9.  
  10. Public Class NotesMailCollection
  11. Inherits List(Of NotesMail)
  12. End Class
  13.  
  14. Public Class MailAccount
  15. Public Host As String
  16. Public DatabaseFilename As String
  17. Public Password As String
  18. End Class
  19.  
  20. Public Class NotesTools
  21.  
  22. Public Shared Function GetMails(ByVal aMailAccount As MailAccount, ByVal MoveMails As Boolean) As NotesMailCollection
  23. Try
  24. Return _GetMails(aMailAccount, MoveMails)
  25. Catch ex As Exception
  26. MsgBox(ex.Message, MsgBoxStyle.Critical)
  27. Return Nothing
  28. End Try
  29. End Function
  30.  
  31. Private Shared Function _GetMails(ByVal aMailAccount As MailAccount, ByVal MoveMails As Boolean) As NotesMailCollection
  32.  
  33. ' Die Session starten
  34. Dim Session As New Domino.NotesSession
  35. Session.Initialize(aMailAccount.Password)
  36.  
  37. ' Datenbank öffnen
  38. Dim MailDB As Domino.NotesDatabase
  39. Dim MailServer As String = Session.GetEnvironmentString("MailServer", True)
  40. ' Dim DatabaseFilename As String = Session.GetEnvironmentString("MailFile", True)
  41.  
  42. MailDB = Session.GetDatabase(MailServer, aMailAccount.DatabaseFilename)
  43.  
  44. If Not MailDB.IsOpen Then
  45. MailDB.Open()
  46. End If
  47.  
  48. Dim result As New NotesMailCollection
  49.  
  50. Dim n_View As Domino.NotesView
  51. Dim n_ViewNav As Domino.NotesViewNavigator
  52. Dim n_ViewEntry As Domino.NotesViewEntry
  53. Dim n_Document As Domino.NotesDocument
  54.  
  55. n_View = MailDB.GetView("($Inbox)")
  56. n_ViewNav = n_View.CreateViewNav
  57. n_ViewEntry = n_ViewNav.GetFirstDocument
  58.  
  59. ' For all mails
  60. Do While Not (n_ViewEntry Is Nothing)
  61.  
  62. n_Document = n_ViewEntry.Document
  63. Dim oAttachment As Domino.NotesEmbeddedObject
  64.  
  65. If n_Document.HasEmbedded Then
  66.  
  67. ' Add data to our object
  68. Dim nm As New NotesMail
  69. With nm
  70. .Body = n_Document.GetItemValue("Body")(0).ToString
  71. .Subject = n_Document.GetItemValue("Subject")(0).ToString
  72. .From = n_Document.GetItemValue("From")(0).ToString
  73. End With
  74.  
  75. Dim fileNames As Object() = Session.Evaluate("@AttachmentNames", n_Document)
  76. For Each fileName As String In fileNames
  77. oAttachment = n_Document.GetAttachment(fileName)
  78. nm.AttachmentFilenames.Add(oAttachment)
  79. Next
  80.  
  81. ' Add object to our list
  82. result.Add(nm)
  83.  
  84. ' Remove from list
  85. If MoveMails Then
  86. ' n_Document.PutInFolder("Importiert " & Now.ToShortDateString, True)
  87. ' n_Document.RemoveFromFolder("($Inbox)")
  88. End If
  89.  
  90. End If
  91.  
  92. ' Next Mail
  93. n_ViewEntry = n_ViewNav.GetNextDocument(n_ViewEntry)
  94. Loop
  95.  
  96. Return result
  97.  
  98. End Function
  99.  
  100. End Class
  1. public Sub Test
  2.  
  3. ' Account Einstellungen
  4. Dim acc As New MailAccount
  5. With acc
  6. .Host = "DominoServer"
  7. .DatabaseFilename = "mail\postfach.nsf"
  8. .Password = "meingeheimespasswort"
  9. End With
  10.  
  11. ' GO! GO! GO! Alle Mails auflisten
  12. Dim lstMails As NotesMailCollection = NotesTools.GetMails(acc, False)
  13.  
  14. For Each itm As NotesMail In lstMails
  15. ' Alle Anhänge in Verzeichnis abspeichern
  16. For Each att As Domino.NotesEmbeddedObject In itm.AttachmentFilenames
  17. strFilename = att.Name
  18. strDirectory = "C:\tmp"
  19. My.Computer.FileSystem.CreateDirectory(strDirectory)
  20. strFullFilename = strDirectory & "\" & strFilename
  21. att.ExtractFile(strFullFilename)
  22. Next
  23.  
  24. Next
  25. end sub