From fcd44e78ff6f79267ec28ab7841c627c5b9cc29b Mon Sep 17 00:00:00 2001 From: Digital Data - Marlon Schreiber Date: Thu, 14 Mar 2019 22:00:57 +0100 Subject: [PATCH] MS EMail SMTP --- DD_CommunicationService/MyComService.vb | 13 +- Message/Email.vb | 192 +++++++++--------------- 2 files changed, 85 insertions(+), 120 deletions(-) diff --git a/DD_CommunicationService/MyComService.vb b/DD_CommunicationService/MyComService.vb index 25735c18..520f783a 100644 --- a/DD_CommunicationService/MyComService.vb +++ b/DD_CommunicationService/MyComService.vb @@ -14,6 +14,12 @@ Public Class MyComService ' ausführen, damit der Dienst gestartet werden kann. Try _MyLogger = New LogConfig(LogConfig.PathType.CustomPath, Path.Combine(My.Application.Info.DirectoryPath, "Log")) + If My.Settings.LOG_ERRORS_ONLY = False Then + _MyLogger.Debug = True + Else + _MyLogger.Debug = False + End If + _Logger = _MyLogger.GetLogger() _firebird = New Firebird(_MyLogger, My.Settings.FB_ConnString, My.Settings.FB_DATABASE, My.Settings.FB_USER, My.Settings.FB_PW) _Email = New Email(_MyLogger) @@ -61,7 +67,12 @@ Public Class MyComService Try _MyLogger = New LogConfig(LogConfig.PathType.CustomPath, Path.Combine(My.Application.Info.DirectoryPath, "Log")) _Logger = _MyLogger.GetLogger() - _MyLogger.Debug = My.Settings.LOG_ERRORS_ONLY + If My.Settings.LOG_ERRORS_ONLY = False Then + _MyLogger.Debug = True + Else + _MyLogger.Debug = False + End If + _Email = New Email(_MyLogger) _firebird = New Firebird(_MyLogger, My.Settings.FB_ConnString, My.Settings.FB_DATABASE, My.Settings.FB_USER, My.Settings.FB_PW) If _firebird._DBInitialized = False Then _Logger.Warn("Firebird-DB could not be intitialized!") diff --git a/Message/Email.vb b/Message/Email.vb index 8cbd8885..198f8a9d 100644 --- a/Message/Email.vb +++ b/Message/Email.vb @@ -4,6 +4,9 @@ Imports Independentsoft.Email.Smtp Imports Independentsoft.Email.Mime Imports Independentsoft.Email.Imap Imports DigitalData.Modules.Logging +Imports System.Net.Mail +Imports System.Net + Public Class Email Private _logger As Logging.Logger Private _logConfig As LogConfig @@ -128,145 +131,96 @@ Public Class Email End Function 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, SENDER_INSTANCE As String, Optional attment As String = "") + AUTH_TYPE As String, SENDER_INSTANCE As String, Optional attment As String = "", Optional Test As Boolean = False) Try - _logger.Debug($"in NewEmail..") - Dim oEmpfaenger As String() + Dim oError As Boolean = False + Dim oReceipiants As String() If mailto.Contains(";") Then - oEmpfaenger = mailto.Split(";") + oReceipiants = mailto.Split(";") Else - ReDim Preserve oEmpfaenger(0) - oEmpfaenger(0) = mailto + ReDim Preserve oReceipiants(0) + oReceipiants(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}..") + For Each oMailReceipiant As String In oReceipiants + _logger.Debug($"oMailReceipiant [{oMailReceipiant}]") + _logger.Debug($"mailsmtp [{mailsmtp}]") + Dim sClient As Net.Mail.SmtpClient 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 - Dim formattedBody = mailBody + sClient = New Net.Mail.SmtpClient(mailsmtp, mailport) + Catch ex As Exception + _logger.Warn($"Could not create SMTP-Client: [{ex.Message}]") + Return False + End Try + sClient.DeliveryMethod = SmtpDeliveryMethod.Network - Dim thisDate1 As Date = #6/10/2011# - Console.WriteLine("Today is " + thisDate1.ToString("MMMM dd, yyyy") + ".") - oTextBodyPart.Body = formattedBody - oMessage.BodyParts.Add(oTextBodyPart) + Dim mymesssage As New MailMessage + sClient.Port = mailport + _logger.Debug($"mailport [{mailport}]") + If AUTH_TYPE = "SSL" Then + _logger.Debug("SSL = true") + sClient.EnableSsl = True + Else + _logger.Debug("SSL = false") + sClient.EnableSsl = False + End If + _logger.Debug($"mailUser [{mailUser}]") + sClient.Credentials = New NetworkCredential(mailUser, mailPW) + sClient.UseDefaultCredentials = False - If attment <> String.Empty Then - _logger.Debug("Attachment Path is: {0}", attment) + If Test = True Then + mymesssage.Body = $"This is the body (text will be replaced within profile)!
mailsmtp: {mailsmtp}
mailport: {mailport}
mailUser: {mailUser}
mailPW: XXXX
AUTH_TYPE: {AUTH_TYPE}" + Else + mymesssage.Body = mailBody + End If + _logger.Debug($"mailBody [{mailBody}]") + 'mymesssage.IsBodyHtml = True + Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(mymesssage.Body) + htmlView.ContentType = New System.Net.Mime.ContentType("text/html") + mymesssage.AlternateViews.Add(htmlView) + attment = attment.Replace("W:\", "\\windream\objects\") + If attment <> String.Empty Then + If System.IO.File.Exists(attment) Then + _logger.Info("Attachment Path is: {0}", attment) + Dim oAttachment As New System.Net.Mail.Attachment(attment) - 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 - End If - Dim oEmailCient As SmtpClient - Try - oEmailCient = New SmtpClient(mailsmtp, mailport) - Catch ex As Exception - _logger.Warn("clsEmail.Create oClient: " & ex.Message) - oError = True - Continue For - End Try - Try - oEmailCient.Connect() - Catch ex As Exception - _logger.Warn("clsEmail.oClient.Connect: " & ex.Message) - oError = True - ' Continue For - End Try - _logger.Debug("Connected to oClient!") - If AUTH_TYPE = "SSL" Then - oEmailCient.EnableSsl = True - 'oClient.ValidateRemoteCertificate = True - _logger.Debug("Authentification via SSL.") - ElseIf AUTH_TYPE = "TLS" Then - ' oClient.ValidateRemoteCertificate = False - oEmailCient.StartTls() - oEmailCient.EnableSsl = False - _logger.Info("Authentification via TLS. SSL disabled") + 'If attment.ToLower.EndsWith("pdf") Then + ' oAttachment.ContentType = New Independentsoft.Email.Mime.ContentType("application", "pdf") + 'ElseIf attment.ToLower.EndsWith("jpg") Then + ' oAttachment.ContentType = New Independentsoft.Email.Mime.ContentType("application", "jpg") + 'ElseIf attment.ToLower.EndsWith("docx") Then + ' oAttachment.ContentType = New Independentsoft.Email.Mime.ContentType("application", "MS-word") + 'End If + mymesssage.Attachments.Add(oAttachment) Else - oEmailCient.EnableSsl = False - _logger.Info("Authentification NONE. SSL disabled") - End If - Try - oEmailCient.Connect() - Catch ex As Exception - _logger.Warn("clsEmail.oClient.Connect: " & ex.Message) + _logger.Warn($"Attachment {attment.ToString} is not existing - Mail won't be send!") 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.Debug("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.oClient.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("NewEmail.Send: " & ex.Message) - oError = True - oEmailCient.Disconnect() - Continue For - End Try - oEmailCient.Disconnect() + End If + End If + _logger.Debug($"mailfrom [{mailfrom}]") - Catch ex As Exception - _logger.Error(ex) - oError = True - End Try + mymesssage.From = New MailAddress(mailfrom) + _logger.Debug($"mailSubject [{mailSubject}]") + mymesssage.Subject = mailSubject + mymesssage.To.Add(New MailAddress(oMailReceipiant)) + _logger.Debug($"Now Sending mail...") + sClient.Send(mymesssage) + _logger.Debug($"Mail has been sent!") + _logger.Info("Message to " & oMailReceipiant & " has been send.") Next - - If oError = True Then - Return False - Else + If oError = False Then Return True + Else + Return False End If + Catch ex As Exception _logger.Error(ex) Return False End Try + End Function + Public Function DELETE_EMAIL(POLLTYPE As String, msgid As String, MYMAIL_SERVER As String, MYMAIL_PORT As Integer, MYMAIL_USER As String, MYMAIL_USER_PW As String) Try If POLLTYPE = "POP" Then