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.
|
||||
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!")
|
||||
|
||||
214
Message/Email.vb
214
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()
|
||||
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
|
||||
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
|
||||
_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")
|
||||
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)
|
||||
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()
|
||||
|
||||
Catch ex As Exception
|
||||
_logger.Error(ex)
|
||||
oError = True
|
||||
End Try
|
||||
Next
|
||||
|
||||
If oError = True Then
|
||||
Return False
|
||||
Dim oReceipiants As String()
|
||||
If mailto.Contains(";") Then
|
||||
oReceipiants = mailto.Split(";")
|
||||
Else
|
||||
Return True
|
||||
ReDim Preserve oReceipiants(0)
|
||||
oReceipiants(0) = mailto
|
||||
End If
|
||||
For Each oMailReceipiant As String In oReceipiants
|
||||
_logger.Debug($"oMailReceipiant [{oMailReceipiant}]")
|
||||
_logger.Debug($"mailsmtp [{mailsmtp}]")
|
||||
Dim sClient As Net.Mail.SmtpClient
|
||||
Try
|
||||
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 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 Test = True Then
|
||||
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}"
|
||||
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 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
|
||||
_logger.Warn($"Attachment {attment.ToString} is not existing - Mail won't be send!")
|
||||
oError = True
|
||||
|
||||
End If
|
||||
End If
|
||||
_logger.Debug($"mailfrom [{mailfrom}]")
|
||||
|
||||
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 = 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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user