MS EMail SMTP
This commit is contained in:
parent
3312ebc569
commit
fcd44e78ff
@ -14,6 +14,12 @@ Public Class MyComService
|
|||||||
' ausführen, damit der Dienst gestartet werden kann.
|
' ausführen, damit der Dienst gestartet werden kann.
|
||||||
Try
|
Try
|
||||||
_MyLogger = New LogConfig(LogConfig.PathType.CustomPath, Path.Combine(My.Application.Info.DirectoryPath, "Log"))
|
_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()
|
_Logger = _MyLogger.GetLogger()
|
||||||
_firebird = New Firebird(_MyLogger, My.Settings.FB_ConnString, My.Settings.FB_DATABASE, My.Settings.FB_USER, My.Settings.FB_PW)
|
_firebird = New Firebird(_MyLogger, My.Settings.FB_ConnString, My.Settings.FB_DATABASE, My.Settings.FB_USER, My.Settings.FB_PW)
|
||||||
_Email = New Email(_MyLogger)
|
_Email = New Email(_MyLogger)
|
||||||
@ -61,7 +67,12 @@ Public Class MyComService
|
|||||||
Try
|
Try
|
||||||
_MyLogger = New LogConfig(LogConfig.PathType.CustomPath, Path.Combine(My.Application.Info.DirectoryPath, "Log"))
|
_MyLogger = New LogConfig(LogConfig.PathType.CustomPath, Path.Combine(My.Application.Info.DirectoryPath, "Log"))
|
||||||
_Logger = _MyLogger.GetLogger()
|
_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)
|
_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
|
If _firebird._DBInitialized = False Then
|
||||||
_Logger.Warn("Firebird-DB could not be intitialized!")
|
_Logger.Warn("Firebird-DB could not be intitialized!")
|
||||||
|
|||||||
184
Message/Email.vb
184
Message/Email.vb
@ -4,6 +4,9 @@ Imports Independentsoft.Email.Smtp
|
|||||||
Imports Independentsoft.Email.Mime
|
Imports Independentsoft.Email.Mime
|
||||||
Imports Independentsoft.Email.Imap
|
Imports Independentsoft.Email.Imap
|
||||||
Imports DigitalData.Modules.Logging
|
Imports DigitalData.Modules.Logging
|
||||||
|
Imports System.Net.Mail
|
||||||
|
Imports System.Net
|
||||||
|
|
||||||
Public Class Email
|
Public Class Email
|
||||||
Private _logger As Logging.Logger
|
Private _logger As Logging.Logger
|
||||||
Private _logConfig As LogConfig
|
Private _logConfig As LogConfig
|
||||||
@ -128,145 +131,96 @@ Public Class Email
|
|||||||
End Function
|
End Function
|
||||||
Public Function NewEmail(mailto As String, mailSubject As String, mailBody As String,
|
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,
|
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
|
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
|
Dim oError As Boolean = False
|
||||||
'Für jeden Empfänger eine Neue Mail erzeugen
|
Dim oReceipiants As String()
|
||||||
For Each oMailempfaenger As String In oEmpfaenger
|
If mailto.Contains(";") Then
|
||||||
_logger.Debug($"Working on email for {oMailempfaenger}..")
|
oReceipiants = mailto.Split(";")
|
||||||
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
|
|
||||||
|
|
||||||
Dim thisDate1 As Date = #6/10/2011#
|
|
||||||
Console.WriteLine("Today is " + thisDate1.ToString("MMMM dd, yyyy") + ".")
|
|
||||||
oTextBodyPart.Body = formattedBody
|
|
||||||
oMessage.BodyParts.Add(oTextBodyPart)
|
|
||||||
|
|
||||||
If attment <> String.Empty Then
|
|
||||||
_logger.Debug("Attachment Path is: {0}", 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
|
Else
|
||||||
_logger.Warn($"Attachment {attment.ToString} is not existing!")
|
ReDim Preserve oReceipiants(0)
|
||||||
|
oReceipiants(0) = mailto
|
||||||
End If
|
End If
|
||||||
End If
|
For Each oMailReceipiant As String In oReceipiants
|
||||||
Dim oEmailCient As SmtpClient
|
_logger.Debug($"oMailReceipiant [{oMailReceipiant}]")
|
||||||
|
_logger.Debug($"mailsmtp [{mailsmtp}]")
|
||||||
|
Dim sClient As Net.Mail.SmtpClient
|
||||||
Try
|
Try
|
||||||
oEmailCient = New SmtpClient(mailsmtp, mailport)
|
sClient = New Net.Mail.SmtpClient(mailsmtp, mailport)
|
||||||
Catch ex As Exception
|
Catch ex As Exception
|
||||||
_logger.Warn("clsEmail.Create oClient: " & ex.Message)
|
_logger.Warn($"Could not create SMTP-Client: [{ex.Message}]")
|
||||||
oError = True
|
Return False
|
||||||
Continue For
|
|
||||||
End Try
|
End Try
|
||||||
Try
|
sClient.DeliveryMethod = SmtpDeliveryMethod.Network
|
||||||
oEmailCient.Connect()
|
|
||||||
Catch ex As Exception
|
Dim mymesssage As New MailMessage
|
||||||
_logger.Warn("clsEmail.oClient.Connect: " & ex.Message)
|
sClient.Port = mailport
|
||||||
oError = True
|
_logger.Debug($"mailport [{mailport}]")
|
||||||
' Continue For
|
|
||||||
End Try
|
|
||||||
_logger.Debug("Connected to oClient!")
|
|
||||||
If AUTH_TYPE = "SSL" Then
|
If AUTH_TYPE = "SSL" Then
|
||||||
oEmailCient.EnableSsl = True
|
_logger.Debug("SSL = true")
|
||||||
'oClient.ValidateRemoteCertificate = True
|
sClient.EnableSsl = 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")
|
|
||||||
Else
|
Else
|
||||||
oEmailCient.EnableSsl = False
|
_logger.Debug("SSL = false")
|
||||||
_logger.Info("Authentification NONE. SSL disabled")
|
sClient.EnableSsl = False
|
||||||
End If
|
End If
|
||||||
Try
|
_logger.Debug($"mailUser [{mailUser}]")
|
||||||
oEmailCient.Connect()
|
sClient.Credentials = New NetworkCredential(mailUser, mailPW)
|
||||||
Catch ex As Exception
|
sClient.UseDefaultCredentials = False
|
||||||
_logger.Warn("clsEmail.oClient.Connect: " & ex.Message)
|
|
||||||
oError = True
|
If Test = True Then
|
||||||
' Continue For
|
mymesssage.Body = $"This is the body (text will be replaced within profile)! <br> mailsmtp: {mailsmtp} <br> mailport: {mailport} <br> mailUser: {mailUser} <br> mailPW: XXXX <br> AUTH_TYPE: {AUTH_TYPE}"
|
||||||
End Try
|
|
||||||
Try
|
|
||||||
If mailsmtp.Contains("office365.com") Then
|
|
||||||
oEmailCient.Login(mailUser, mailPW, AuthenticationType.None)
|
|
||||||
Else
|
Else
|
||||||
oEmailCient.Login(mailUser, mailPW)
|
mymesssage.Body = mailBody
|
||||||
End If
|
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)
|
||||||
|
|
||||||
_logger.Debug("Logged in!")
|
'If attment.ToLower.EndsWith("pdf") Then
|
||||||
Catch ex As Exception
|
' oAttachment.ContentType = New Independentsoft.Email.Mime.ContentType("application", "pdf")
|
||||||
Try
|
'ElseIf attment.ToLower.EndsWith("jpg") Then
|
||||||
If mailsmtp.Contains("office365.com") Then
|
' oAttachment.ContentType = New Independentsoft.Email.Mime.ContentType("application", "jpg")
|
||||||
oEmailCient.Login(mailUser, mailPW, AuthenticationType.Login)
|
'ElseIf attment.ToLower.EndsWith("docx") Then
|
||||||
|
' oAttachment.ContentType = New Independentsoft.Email.Mime.ContentType("application", "MS-word")
|
||||||
|
'End If
|
||||||
|
mymesssage.Attachments.Add(oAttachment)
|
||||||
Else
|
Else
|
||||||
oEmailCient.Login(mailUser, mailPW, AuthenticationType.Anonymous)
|
_logger.Warn($"Attachment {attment.ToString} is not existing - Mail won't be send!")
|
||||||
|
oError = True
|
||||||
|
|
||||||
End If
|
End If
|
||||||
|
End If
|
||||||
|
_logger.Debug($"mailfrom [{mailfrom}]")
|
||||||
|
|
||||||
Catch ex1 As Exception
|
mymesssage.From = New MailAddress(mailfrom)
|
||||||
Try
|
_logger.Debug($"mailSubject [{mailSubject}]")
|
||||||
oEmailCient.Login(mailUser, mailPW, AuthenticationType.Login)
|
mymesssage.Subject = mailSubject
|
||||||
Catch ex2 As Exception
|
mymesssage.To.Add(New MailAddress(oMailReceipiant))
|
||||||
_logger.Warn("clsEmail.oClient.Login: " & ex.Message)
|
_logger.Debug($"Now Sending mail...")
|
||||||
oError = True
|
sClient.Send(mymesssage)
|
||||||
oEmailCient.Disconnect()
|
_logger.Debug($"Mail has been sent!")
|
||||||
Continue For
|
_logger.Info("Message to " & oMailReceipiant & " has been send.")
|
||||||
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()
|
|
||||||
|
|
||||||
Catch ex As Exception
|
|
||||||
_logger.Error(ex)
|
|
||||||
oError = True
|
|
||||||
End Try
|
|
||||||
Next
|
Next
|
||||||
|
If oError = False Then
|
||||||
If oError = True Then
|
|
||||||
Return False
|
|
||||||
Else
|
|
||||||
Return True
|
Return True
|
||||||
|
Else
|
||||||
|
Return False
|
||||||
End If
|
End If
|
||||||
|
|
||||||
Catch ex As Exception
|
Catch ex As Exception
|
||||||
_logger.Error(ex)
|
_logger.Error(ex)
|
||||||
Return False
|
Return False
|
||||||
End Try
|
End Try
|
||||||
|
|
||||||
End Function
|
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)
|
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
|
Try
|
||||||
If POLLTYPE = "POP" Then
|
If POLLTYPE = "POP" Then
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user