MS Service ZuGferd und Begin GUI
This commit is contained in:
219
Message/Email.vb
219
Message/Email.vb
@@ -2,104 +2,147 @@
|
||||
Imports Independentsoft.Email
|
||||
Imports Independentsoft.Email.Smtp
|
||||
Imports Independentsoft.Email.Mime
|
||||
|
||||
Imports DigitalData.Modules.Logging
|
||||
Public Class Email
|
||||
Private Shared Logger As NLog.Logger = NLog.LogManager.GetCurrentClassLogger
|
||||
Public Function NewSendMail(ByVal mailSubject As String, ByVal mailBody As String, mailto As String,
|
||||
mailfrom As String, mailsmtp As String, mailport As Integer, mailUser As String, mailPW As String,
|
||||
AUTH_TYPE As String, Optional attment As String = "")
|
||||
Dim empfaenger As String()
|
||||
If mailto.Contains(";") Then
|
||||
empfaenger = mailto.Split(";")
|
||||
Else
|
||||
ReDim Preserve empfaenger(0)
|
||||
empfaenger(0) = mailto
|
||||
End If
|
||||
Dim _error As Boolean = False
|
||||
'Für jeden Empfänger eine Neue Mail erzeugen
|
||||
For Each _mailempfaenger As String In empfaenger
|
||||
Try
|
||||
Dim message As New Message()
|
||||
message.From = New Mailbox(mailfrom, mailfrom)
|
||||
message.[To].Add(New Mailbox(_mailempfaenger))
|
||||
message.Subject = mailSubject
|
||||
Dim textBodyPart As New BodyPart()
|
||||
textBodyPart.ContentType = New ContentType("text", "html", "utf-8")
|
||||
textBodyPart.ContentTransferEncoding = ContentTransferEncoding.QuotedPrintable
|
||||
textBodyPart.Body = mailBody
|
||||
message.BodyParts.Add(textBodyPart)
|
||||
If attment <> String.Empty Then
|
||||
If System.IO.File.Exists(attment) Then
|
||||
Dim attachment1 As New Attachment(attment)
|
||||
If attment.ToLower.EndsWith("pdf") Then
|
||||
attachment1.ContentType = New ContentType("application", "pdf")
|
||||
ElseIf attment.ToLower.EndsWith("jpg") Then
|
||||
attachment1.ContentType = New ContentType("application", "jpg")
|
||||
ElseIf attment.ToLower.EndsWith("docx") Then
|
||||
attachment1.ContentType = New ContentType("application", "MS-word")
|
||||
Private _logger As DigitalData.Modules.Logging.Logger
|
||||
Private _logConfig As LogConfig
|
||||
Public Sub New(LogConfig As LogConfig)
|
||||
_logger = LogConfig.GetLogger()
|
||||
_logConfig = LogConfig
|
||||
End Sub
|
||||
Public Function NewEmail(mailto As String, mailSubject As String, mailBody As String,
|
||||
mailfrom As String, mailsmtp As String, mailport As Integer, mailUser As String, mailPW As String,
|
||||
AUTH_TYPE As String, Optional attment As String = "")
|
||||
Try
|
||||
_logger.Debug($"in NewEmail..")
|
||||
Dim oEmpfaenger As String()
|
||||
If mailto.Contains(";") Then
|
||||
oEmpfaenger = mailto.Split(";")
|
||||
Else
|
||||
ReDim Preserve oEmpfaenger(0)
|
||||
oEmpfaenger(0) = mailto
|
||||
End If
|
||||
Dim oError As Boolean = False
|
||||
'Für jeden Empfänger eine Neue Mail erzeugen
|
||||
For Each oMailempfaenger As String In oEmpfaenger
|
||||
_logger.Debug($"Working on email for {oMailempfaenger}..")
|
||||
Try
|
||||
Dim oMessage As New Message()
|
||||
oMessage.From = New Mailbox(mailfrom, mailfrom)
|
||||
oMessage.[To].Add(New Mailbox(oMailempfaenger))
|
||||
oMessage.Subject = mailSubject
|
||||
_logger.Debug($"Message created..")
|
||||
Dim oTextBodyPart As New BodyPart()
|
||||
oTextBodyPart.ContentType = New ContentType("text", "html", "utf-8")
|
||||
oTextBodyPart.ContentTransferEncoding = ContentTransferEncoding.QuotedPrintable
|
||||
oTextBodyPart.Body = mailBody
|
||||
oMessage.BodyParts.Add(oTextBodyPart)
|
||||
If attment <> String.Empty Then
|
||||
If System.IO.File.Exists(attment) Then
|
||||
Dim attachment1 As New Attachment(attment)
|
||||
If attment.ToLower.EndsWith("pdf") Then
|
||||
attachment1.ContentType = New ContentType("application", "pdf")
|
||||
ElseIf attment.ToLower.EndsWith("jpg") Then
|
||||
attachment1.ContentType = New ContentType("application", "jpg")
|
||||
ElseIf attment.ToLower.EndsWith("docx") Then
|
||||
attachment1.ContentType = New ContentType("application", "MS-word")
|
||||
End If
|
||||
oMessage.BodyParts.Add(attachment1)
|
||||
Else
|
||||
_logger.Warn($"Attachment {attment.ToString} is not existing!")
|
||||
End If
|
||||
message.BodyParts.Add(attachment1)
|
||||
End If
|
||||
End If
|
||||
Dim client As SmtpClient
|
||||
Try
|
||||
client = New SmtpClient(mailsmtp, mailport)
|
||||
Catch ex As Exception
|
||||
Logger.Warn("clsEmail.SendMail(Create Client): " & ex.Message)
|
||||
_error = True
|
||||
Continue For
|
||||
End Try
|
||||
Try
|
||||
client.Connect()
|
||||
Catch ex As Exception
|
||||
Logger.Warn("clsEmail.SendMail(Client.Connect): " & ex.Message)
|
||||
_error = True
|
||||
Continue For
|
||||
End Try
|
||||
Logger.Info("Connected to Client!")
|
||||
If AUTH_TYPE = "SSL" Then
|
||||
client.EnableSsl = True
|
||||
client.ValidateRemoteCertificate = True
|
||||
Logger.Info("Authentification via SSL.")
|
||||
ElseIf AUTH_TYPE = "TLS" Then
|
||||
Dim oEmailCient As SmtpClient
|
||||
Try
|
||||
client.EnableSsl = False
|
||||
Logger.Info("Authentification via TLS. SSL enabled")
|
||||
'client.StartTls()
|
||||
oEmailCient = New SmtpClient(mailsmtp, mailport)
|
||||
Catch ex As Exception
|
||||
Logger.Warn("clsEmail.SendMail(Client.StartTls): " & ex.Message)
|
||||
_error = True
|
||||
_logger.Warn("clsEmail.Create Client: " & ex.Message)
|
||||
oError = True
|
||||
Continue For
|
||||
End Try
|
||||
Try
|
||||
oEmailCient.Connect()
|
||||
Catch ex As Exception
|
||||
_logger.Warn("clsEmail.Client.Connect: " & ex.Message)
|
||||
oError = True
|
||||
' Continue For
|
||||
End Try
|
||||
_logger.Info("Connected to Client!")
|
||||
If AUTH_TYPE = "SSL" Then
|
||||
oEmailCient.EnableSsl = True
|
||||
'client.ValidateRemoteCertificate = True
|
||||
_logger.Info("Authentification via SSL.")
|
||||
ElseIf AUTH_TYPE = "TLS" Then
|
||||
' client.ValidateRemoteCertificate = False
|
||||
oEmailCient.StartTls()
|
||||
oEmailCient.EnableSsl = False
|
||||
_logger.Info("Authentification via TLS. SSL disabled")
|
||||
Else
|
||||
oEmailCient.EnableSsl = False
|
||||
_logger.Info("Authentification NONE. SSL disabled")
|
||||
End If
|
||||
Try
|
||||
|
||||
oEmailCient.Connect()
|
||||
Catch ex As Exception
|
||||
_logger.Warn("clsEmail.Client.Connect: " & ex.Message)
|
||||
oError = True
|
||||
' Continue For
|
||||
End Try
|
||||
Try
|
||||
If mailsmtp.Contains("office365.com") Then
|
||||
oEmailCient.Login(mailUser, mailPW, AuthenticationType.None)
|
||||
Else
|
||||
oEmailCient.Login(mailUser, mailPW)
|
||||
End If
|
||||
|
||||
_logger.Info("Logged in!")
|
||||
Catch ex As Exception
|
||||
Try
|
||||
If mailsmtp.Contains("office365.com") Then
|
||||
oEmailCient.Login(mailUser, mailPW, AuthenticationType.Login)
|
||||
Else
|
||||
oEmailCient.Login(mailUser, mailPW, AuthenticationType.Anonymous)
|
||||
End If
|
||||
|
||||
Catch ex1 As Exception
|
||||
Try
|
||||
oEmailCient.Login(mailUser, mailPW, AuthenticationType.Login)
|
||||
Catch ex2 As Exception
|
||||
_logger.Warn("clsEmail.Client.Login: " & ex.Message)
|
||||
oError = True
|
||||
oEmailCient.Disconnect()
|
||||
Continue For
|
||||
End Try
|
||||
End Try
|
||||
End Try
|
||||
Try
|
||||
oEmailCient.Send(oMessage)
|
||||
_logger.Info("Message to " & oMailempfaenger & " has been send.")
|
||||
oError = False
|
||||
Catch ex As Exception
|
||||
_logger.Warn("clsEmail.Client.Send: " & ex.Message)
|
||||
oError = True
|
||||
oEmailCient.Disconnect()
|
||||
Continue For
|
||||
End Try
|
||||
oEmailCient.Disconnect()
|
||||
|
||||
End If
|
||||
Try
|
||||
client.Login(mailUser, mailPW)
|
||||
Logger.Info("Logged in!")
|
||||
Catch ex As Exception
|
||||
Logger.Warn("clsEmail.SendMail(Client.Login): " & ex.Message)
|
||||
_error = True
|
||||
client.Disconnect()
|
||||
Continue For
|
||||
_logger.Error(ex)
|
||||
oError = True
|
||||
End Try
|
||||
Try
|
||||
client.Send(message)
|
||||
Logger.Info("Message to " & _mailempfaenger & " has been send.")
|
||||
Catch ex As Exception
|
||||
Logger.Warn("clsEmail.SendMail(Client.Send): " & ex.Message)
|
||||
_error = True
|
||||
client.Disconnect()
|
||||
Continue For
|
||||
End Try
|
||||
client.Disconnect()
|
||||
Catch ex As Exception
|
||||
Logger.Error(ex)
|
||||
_error = True
|
||||
End Try
|
||||
Next
|
||||
If _error = True Then
|
||||
Next
|
||||
|
||||
If oError = True Then
|
||||
Return False
|
||||
Else
|
||||
Return True
|
||||
End If
|
||||
Catch ex As Exception
|
||||
_logger.Error(ex)
|
||||
Return False
|
||||
Else
|
||||
Return True
|
||||
End If
|
||||
End Try
|
||||
End Function
|
||||
End Class
|
||||
|
||||
@@ -43,12 +43,15 @@
|
||||
<OptionInfer>On</OptionInfer>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="DigitalData.Modules.Logging">
|
||||
<HintPath>..\Modules.Logging\bin\Debug\DigitalData.Modules.Logging.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Independentsoft.Email">
|
||||
<HintPath>P:\Projekte DIGITAL DATA\DIGITAL DATA - Entwicklung\DLL_Bibliotheken\Email .NET\Bin\Independentsoft.Email.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\NLog.4.5.10\lib\net45\NLog.dll</HintPath>
|
||||
<Reference Include="NLog">
|
||||
<HintPath>..\Modules.Logging\bin\Debug\NLog.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Configuration" />
|
||||
|
||||
Reference in New Issue
Block a user