Messaging: Add Email2 Class using Limilabs Mail.dll

This commit is contained in:
Jonathan Jenne 2021-07-22 16:45:08 +02:00
parent 104636ded4
commit a09953cedc
9 changed files with 694 additions and 6 deletions

View File

@ -160,6 +160,12 @@
<Compile Include="frmDocViewPure.vb">
<SubType>Form</SubType>
</Compile>
<Compile Include="frmEmail.Designer.vb">
<DependentUpon>frmEmail.vb</DependentUpon>
</Compile>
<Compile Include="frmEmail.vb">
<SubType>Form</SubType>
</Compile>
<Compile Include="frmFilesystem.Designer.vb">
<DependentUpon>frmFilesystem.vb</DependentUpon>
</Compile>
@ -233,6 +239,9 @@
<EmbeddedResource Include="frmDocViewPure.resx">
<DependentUpon>frmDocViewPure.vb</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="frmEmail.resx">
<DependentUpon>frmEmail.vb</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="frmFilesystem.resx">
<DependentUpon>frmFilesystem.vb</DependentUpon>
</EmbeddedResource>
@ -304,6 +313,10 @@
<Project>{903b2d7d-3b80-4be9-8713-7447b704e1b0}</Project>
<Name>Logging</Name>
</ProjectReference>
<ProjectReference Include="..\Modules.Messaging\Messaging.vbproj">
<Project>{af664d85-0a4b-4bab-a2f8-83110c06553a}</Project>
<Name>Messaging</Name>
</ProjectReference>
<ProjectReference Include="..\Modules.Windream\Windream.vbproj">
<Project>{4c86df8f-a280-40d4-85b0-10b1bf66c15c}</Project>
<Name>Windream</Name>

234
GUIs.Test.TestGUI/frmEmail.Designer.vb generated Normal file
View File

@ -0,0 +1,234 @@
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class frmEmail
Inherits System.Windows.Forms.Form
'Das Formular überschreibt den Löschvorgang, um die Komponentenliste zu bereinigen.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Wird vom Windows Form-Designer benötigt.
Private components As System.ComponentModel.IContainer
'Hinweis: Die folgende Prozedur ist für den Windows Form-Designer erforderlich.
'Das Bearbeiten ist mit dem Windows Form-Designer möglich.
'Das Bearbeiten mit dem Code-Editor ist nicht möglich.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Me.txtUser = New System.Windows.Forms.TextBox()
Me.ContextMenuStrip1 = New System.Windows.Forms.ContextMenuStrip(Me.components)
Me.txtPort = New System.Windows.Forms.TextBox()
Me.txtServer = New System.Windows.Forms.TextBox()
Me.txtPassword = New System.Windows.Forms.TextBox()
Me.Label1 = New System.Windows.Forms.Label()
Me.Label2 = New System.Windows.Forms.Label()
Me.Label3 = New System.Windows.Forms.Label()
Me.Label4 = New System.Windows.Forms.Label()
Me.Button1 = New System.Windows.Forms.Button()
Me.cmbSecurity = New System.Windows.Forms.ComboBox()
Me.Label5 = New System.Windows.Forms.Label()
Me.Button2 = New System.Windows.Forms.Button()
Me.ListBox1 = New System.Windows.Forms.ListBox()
Me.Button3 = New System.Windows.Forms.Button()
Me.txtMessageID = New System.Windows.Forms.TextBox()
Me.Label6 = New System.Windows.Forms.Label()
Me.SuspendLayout()
'
'txtUser
'
Me.txtUser.Location = New System.Drawing.Point(12, 80)
Me.txtUser.Name = "txtUser"
Me.txtUser.Size = New System.Drawing.Size(177, 20)
Me.txtUser.TabIndex = 0
Me.txtUser.Text = "johnnie@posteo.de"
'
'ContextMenuStrip1
'
Me.ContextMenuStrip1.Name = "ContextMenuStrip1"
Me.ContextMenuStrip1.Size = New System.Drawing.Size(61, 4)
'
'txtPort
'
Me.txtPort.Location = New System.Drawing.Point(296, 35)
Me.txtPort.Name = "txtPort"
Me.txtPort.Size = New System.Drawing.Size(100, 20)
Me.txtPort.TabIndex = 2
Me.txtPort.Text = "465"
'
'txtServer
'
Me.txtServer.Location = New System.Drawing.Point(12, 35)
Me.txtServer.Name = "txtServer"
Me.txtServer.Size = New System.Drawing.Size(278, 20)
Me.txtServer.TabIndex = 3
Me.txtServer.Text = "posteo.de"
'
'txtPassword
'
Me.txtPassword.Location = New System.Drawing.Point(195, 80)
Me.txtPassword.Name = "txtPassword"
Me.txtPassword.PasswordChar = Global.Microsoft.VisualBasic.ChrW(42)
Me.txtPassword.Size = New System.Drawing.Size(201, 20)
Me.txtPassword.TabIndex = 5
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(9, 19)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(38, 13)
Me.Label1.TabIndex = 6
Me.Label1.Text = "Server"
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.Location = New System.Drawing.Point(293, 19)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(26, 13)
Me.Label2.TabIndex = 6
Me.Label2.Text = "Port"
'
'Label3
'
Me.Label3.AutoSize = True
Me.Label3.Location = New System.Drawing.Point(9, 64)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(55, 13)
Me.Label3.TabIndex = 6
Me.Label3.Text = "Username"
'
'Label4
'
Me.Label4.AutoSize = True
Me.Label4.Location = New System.Drawing.Point(192, 64)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(53, 13)
Me.Label4.TabIndex = 6
Me.Label4.Text = "Password"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(12, 154)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(75, 23)
Me.Button1.TabIndex = 7
Me.Button1.Text = "Test Login"
Me.Button1.UseVisualStyleBackColor = True
'
'cmbSecurity
'
Me.cmbSecurity.FormattingEnabled = True
Me.cmbSecurity.Items.AddRange(New Object() {"PLAINTEXT", "SSL", "STARTTLS"})
Me.cmbSecurity.Location = New System.Drawing.Point(402, 34)
Me.cmbSecurity.Name = "cmbSecurity"
Me.cmbSecurity.Size = New System.Drawing.Size(121, 21)
Me.cmbSecurity.TabIndex = 8
Me.cmbSecurity.Text = "SSL"
'
'Label5
'
Me.Label5.AutoSize = True
Me.Label5.Location = New System.Drawing.Point(399, 19)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(45, 13)
Me.Label5.TabIndex = 6
Me.Label5.Text = "Security"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(93, 154)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(107, 23)
Me.Button2.TabIndex = 7
Me.Button2.Text = "Load Messages"
Me.Button2.UseVisualStyleBackColor = True
'
'ListBox1
'
Me.ListBox1.Dock = System.Windows.Forms.DockStyle.Right
Me.ListBox1.FormattingEnabled = True
Me.ListBox1.Location = New System.Drawing.Point(541, 0)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(259, 450)
Me.ListBox1.TabIndex = 9
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(206, 154)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(107, 23)
Me.Button3.TabIndex = 7
Me.Button3.Text = "Load Message"
Me.Button3.UseVisualStyleBackColor = True
'
'txtMessageID
'
Me.txtMessageID.Location = New System.Drawing.Point(12, 128)
Me.txtMessageID.Name = "txtMessageID"
Me.txtMessageID.Size = New System.Drawing.Size(511, 20)
Me.txtMessageID.TabIndex = 5
'
'Label6
'
Me.Label6.AutoSize = True
Me.Label6.Location = New System.Drawing.Point(9, 112)
Me.Label6.Name = "Label6"
Me.Label6.Size = New System.Drawing.Size(61, 13)
Me.Label6.TabIndex = 6
Me.Label6.Text = "MessageID"
'
'frmEmail
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(800, 450)
Me.Controls.Add(Me.ListBox1)
Me.Controls.Add(Me.cmbSecurity)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.Label5)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label6)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.txtMessageID)
Me.Controls.Add(Me.txtPassword)
Me.Controls.Add(Me.txtServer)
Me.Controls.Add(Me.txtPort)
Me.Controls.Add(Me.txtUser)
Me.Name = "frmEmail"
Me.Text = "frmEmail"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents txtUser As TextBox
Friend WithEvents ContextMenuStrip1 As ContextMenuStrip
Friend WithEvents txtPort As TextBox
Friend WithEvents txtServer As TextBox
Friend WithEvents txtPassword As TextBox
Friend WithEvents Label1 As Label
Friend WithEvents Label2 As Label
Friend WithEvents Label3 As Label
Friend WithEvents Label4 As Label
Friend WithEvents Button1 As Button
Friend WithEvents cmbSecurity As ComboBox
Friend WithEvents Label5 As Label
Friend WithEvents Button2 As Button
Friend WithEvents ListBox1 As ListBox
Friend WithEvents Button3 As Button
Friend WithEvents txtMessageID As TextBox
Friend WithEvents Label6 As Label
End Class

View File

@ -0,0 +1,123 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="ContextMenuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root>

View File

@ -0,0 +1,64 @@
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Messaging
Imports Limilabs.Mail
Public Class frmEmail
Private Logconfig As LogConfig
Private Email As Email2
Private Sub frmEmail_Load(sender As Object, e As EventArgs) Handles Me.Load
Logconfig = New LogConfig(LogConfig.PathType.Temp)
Email = New Email2(Logconfig)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim oResult = Email.Test_Login(txtServer.Text, txtUser.Text, txtPassword.Text, Email2.EmailSecurity.SSL)
If oResult = True Then
AddLog($"Connection to {txtServer.Text} successful.")
Else
AddLog($"Connection to {txtServer.Text} failed!")
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim oMessages = Email.Get_Messages(txtServer.Text, txtUser.Text, txtPassword.Text, Email2.EmailSecurity.SSL, "Inbox")
AddLog($"Found {oMessages.Count} Messages!")
For Each oMessage In oMessages
AddLog(oMessage.MessageID)
Next
End Sub
Private Sub AddLog(pMessage)
ListBox1.Items.Add(pMessage)
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim oMessageId As String = txtMessageID.Text
Dim oMail As IMail = Email.Get_Message(txtServer.Text, txtUser.Text, txtPassword.Text, Email2.EmailSecurity.SSL, oMessageId, "Inbox")
Dim oFilename As String = IO.Path.GetTempFileName
oMail.Save(oFilename)
AddLog($"Mail saved to {oFilename}")
Dim oEmailTempPath = Email.Remove_AttachmentsFromEmail(oFilename)
AddLog($"Mail without attachments saved to {oEmailTempPath}")
Dim oAttachments As List(Of String) = Email.Save_AttachmentsToDisk(oFilename)
For Each oAttachment In oAttachments
AddLog($"Attachmen saved to {oAttachment}")
Next
End Sub
Private Sub ListBox1_SelectedValueChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedValueChanged
txtMessageID.Text = ListBox1.SelectedItem
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
txtMessageID.Text = ListBox1.SelectedItem
End Sub
End Class

View File

@ -29,6 +29,7 @@ Partial Class frmStart
Me.Button5 = New System.Windows.Forms.Button()
Me.Button6 = New System.Windows.Forms.Button()
Me.Button7 = New System.Windows.Forms.Button()
Me.Button8 = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'Button1
@ -94,6 +95,15 @@ Partial Class frmStart
Me.Button7.Text = "Mail"
Me.Button7.UseVisualStyleBackColor = True
'
'Button8
'
Me.Button8.Location = New System.Drawing.Point(254, 73)
Me.Button8.Name = "Button8"
Me.Button8.Size = New System.Drawing.Size(236, 55)
Me.Button8.TabIndex = 0
Me.Button8.Text = "IMAP"
Me.Button8.UseVisualStyleBackColor = True
'
'frmStart
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
@ -104,6 +114,7 @@ Partial Class frmStart
Me.Controls.Add(Me.Button4)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button8)
Me.Controls.Add(Me.Button7)
Me.Controls.Add(Me.Button1)
Me.Name = "frmStart"
@ -119,4 +130,5 @@ Partial Class frmStart
Friend WithEvents Button5 As Button
Friend WithEvents Button6 As Button
Friend WithEvents Button7 As Button
Friend WithEvents Button8 As Button
End Class

View File

@ -27,7 +27,7 @@ Public Class frmStart
frmDocView.Show()
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
frmMail.Show()
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click, Button8.Click
frmEmail.Show()
End Sub
End Class

241
Modules.Messaging/Email2.vb Normal file
View File

@ -0,0 +1,241 @@
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Filesystem
Imports Limilabs.Mail
Imports Limilabs.Client.IMAP
Imports Limilabs.Mail.MIME
Public Class Email2
Private ReadOnly Logger As Logger
Private ReadOnly LogConfig As LogConfig
Private ReadOnly FileEx As Filesystem.File
Public Sub New(pLogConfig As LogConfig)
LogConfig = pLogConfig
Logger = pLogConfig.GetLogger()
FileEx = New Filesystem.File(pLogConfig)
End Sub
Public Enum EmailSecurity
SSL
STARTTLS
End Enum
Private Function Get_PortForSecurityOption(pSecurity As EmailSecurity) As Integer
Select Case pSecurity
Case EmailSecurity.SSL
Return 993
Case EmailSecurity.STARTTLS
Return 143
Case Else
Return 0
End Select
End Function
Public Function Test_Login(pServer As String, pUsername As String, pPassword As String, pSecurity As EmailSecurity, Optional pFolder As String = "Inbox", Optional pPort As Integer = 0) As Boolean
Logger.Debug("Testing Login to Server [{0}:{1}] with user [{2}]", pServer, pPort, pUsername)
Try
Using oClient As New Imap()
Logger.Debug("Connecting to IMAP server [{0}]", pServer)
Dim oPort As Integer = pPort
If oPort = 0 Then
oPort = Get_PortForSecurityOption(pSecurity)
End If
If pSecurity = EmailSecurity.SSL Then
oClient.ConnectSSL(pServer, oPort)
ElseIf pSecurity = EmailSecurity.STARTTLS Then
oClient.Connect(pServer, oPort)
oClient.StartTLS()
Else
Throw New ArgumentOutOfRangeException("Security")
End If
Logger.Debug("Logging in with user [{0}]", pUsername)
oClient.UseBestLogin(pUsername, pPassword)
Logger.Debug("Fetching Inbox")
Dim oStatus As FolderStatus = oClient.SelectInbox()
Logger.Debug("Test finished!")
oClient.Close()
End Using
Return True
Catch ex As Exception
Logger.Warn("Login failed for server [{0}:{1}] with user [{2}]!", pServer, pPort, pUsername)
Logger.Error(ex)
Return False
End Try
End Function
Public Function Get_Messages(pServer As String, pUsername As String, pPassword As String, pSecurity As EmailSecurity, Optional pFolder As String = "Inbox", Optional pPort As Integer = 0) As List(Of IMail)
Logger.Debug("Fetching Messages from Server [{0}:{1}] with user [{2}]", pServer, pPort, pUsername)
Dim oMails As New List(Of IMail)
Try
Using oClient As New Imap()
Logger.Debug("Connecting to IMAP server [{0}]", pServer)
Dim oPort As Integer = pPort
If oPort = 0 Then
oPort = Get_PortForSecurityOption(pSecurity)
End If
If pSecurity = EmailSecurity.SSL Then
oClient.ConnectSSL(pServer, oPort)
ElseIf pSecurity = EmailSecurity.STARTTLS Then
oClient.Connect(pServer, oPort)
oClient.StartTLS()
Else
Throw New ArgumentOutOfRangeException("Security")
End If
Logger.Debug("Logging in with user [{0}]", pUsername)
oClient.UseBestLogin(pUsername, pPassword)
Logger.Debug("Fetching Folder [{0}]", pFolder)
Dim oStatus As FolderStatus = oClient.Select(pFolder)
Logger.Debug("Fetching Unseen UUIDs")
Dim oUUIDs As List(Of Long) = oClient.Search(Flag.Unseen)
Dim oMailBuilder As New MailBuilder()
Logger.Debug("Fetching Unseen Mails")
For Each oUUID As Long In oUUIDs
Dim oEmlFile As Byte() = oClient.GetMessageByUID(oUUID)
Dim oEmail As IMail = oMailBuilder.CreateFromEml(oEmlFile)
oMails.Add(oEmail)
Next
Logger.Debug("Emails fetched!")
oClient.Close()
End Using
Return oMails
Catch ex As Exception
Logger.Warn("Login failed for server [{0}:{1}] with user [{2}]!", pServer, pPort, pUsername)
Logger.Error(ex)
Return New List(Of IMail)
End Try
End Function
Public Function Get_Message(pServer As String, pUsername As String, pPassword As String, pSecurity As EmailSecurity, pMessageId As String, Optional pFolder As String = "Inbox", Optional pPort As Integer = 0) As IMail
Logger.Debug("Fetching Message [{0}] from Server [{1}:{2}] with user [{3}]", pMessageId, pServer, pPort, pUsername)
Dim oMail As IMail = Nothing
Try
Using oClient As New Imap()
Logger.Debug("Connecting to IMAP server [{0}]", pServer)
Dim oPort As Integer = pPort
If oPort = 0 Then
oPort = Get_PortForSecurityOption(pSecurity)
End If
If pSecurity = EmailSecurity.SSL Then
oClient.ConnectSSL(pServer, oPort)
ElseIf pSecurity = EmailSecurity.STARTTLS Then
oClient.Connect(pServer, oPort)
oClient.StartTLS()
Else
Throw New ArgumentOutOfRangeException("Security")
End If
Logger.Debug("Logging in with user [{0}]", pUsername)
oClient.UseBestLogin(pUsername, pPassword)
Logger.Debug("Fetching Inbox")
Dim oStatus As FolderStatus = oClient.Select(pFolder)
Logger.Debug("Fetching UUIDs")
Dim oUUIDs As List(Of Long) = oClient.Search(Flag.All)
Dim oMailBuilder As New MailBuilder()
Dim oInfos As List(Of MessageInfo) = oClient.GetMessageInfoByUID(oUUIDs)
Logger.Debug("Fetching Unseen Mails")
For Each oInfo As MessageInfo In oInfos
If oInfo.Envelope.MessageID = pMessageId Then
Dim oMailData As Byte() = oClient.GetMessageByUID(oInfo.UID)
oMail = oMailBuilder.CreateFromEml(oMailData)
Exit For
End If
Next
Logger.Debug("Emails fetched!")
oClient.Close()
End Using
Return oMail
Catch ex As Exception
Logger.Warn("Login failed for server [{0}:{1}] with user [{2}]!", pServer, pPort, pUsername)
Logger.Error(ex)
Return New List(Of IMail)
End Try
End Function
Public Function Remove_AttachmentsFromEmail(pFileName As String) As String
Try
Dim oTempFileName As String = IO.Path.GetTempFileName()
Dim oMailBuilder As New MailBuilder()
Dim oMail = oMailBuilder.CreateFromEmlFile(pFileName)
oMail.RemoveAttachments()
oMail.Save(oTempFileName)
Return oTempFileName
Catch ex As Exception
Logger.Error(ex)
Return Nothing
End Try
End Function
Public Function Save_AttachmentsToDisk(pFileName As String) As List(Of String)
Try
Dim oAttachmentPaths As New List(Of String)
Dim oMailBuilder As New MailBuilder()
Dim oMail = oMailBuilder.CreateFromEmlFile(pFileName)
Dim oTempPath As String = IO.Path.GetTempPath()
If oMail.Attachments.Count = 0 Then
Return New List(Of String)
End If
For Each oAttachment As MimeData In oMail.Attachments
Dim oPath As String = IO.Path.Combine(oTempPath, oAttachment.SafeFileName)
Dim oVersionedPath As String = FileEx.GetVersionedFilename(oPath)
oAttachment.Save(oVersionedPath)
oAttachmentPaths.Add(oVersionedPath)
Next
Return oAttachmentPaths
Catch ex As Exception
Logger.Error(ex)
Return New List(Of String)
End Try
End Function
End Class

View File

@ -1,3 +0,0 @@
Public Class Mail
End Class

View File

@ -84,7 +84,7 @@
<ItemGroup>
<Compile Include="Email.vb" />
<Compile Include="EventBus.vb" />
<Compile Include="Mail.vb" />
<Compile Include="Email2.vb" />
<Compile Include="My Project\AssemblyInfo.vb" />
<Compile Include="My Project\Application.Designer.vb">
<AutoGen>True</AutoGen>
@ -123,6 +123,10 @@
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Modules.Filesystem\Filesystem.vbproj">
<Project>{991d0231-4623-496d-8bd0-9ca906029cbc}</Project>
<Name>Filesystem</Name>
</ProjectReference>
<ProjectReference Include="..\Modules.Logging\Logging.vbproj">
<Project>{903b2d7d-3b80-4be9-8713-7447b704e1b0}</Project>
<Name>Logging</Name>