92 lines
4.0 KiB
VB.net
92 lines
4.0 KiB
VB.net
|
|
Imports System
|
|
Imports Independentsoft.Email
|
|
Imports Independentsoft.Email.Smtp
|
|
Imports Independentsoft.Email.Mime
|
|
Imports DigitalData.Modules.Logging
|
|
Imports System.Net.Mail
|
|
Imports System.Net
|
|
Imports System.Net.Mime
|
|
|
|
Public Class clsEmail
|
|
Dim Logger As DigitalData.Modules.Logging.Logger
|
|
Private Shared MailAktiv As Boolean = False
|
|
Sub New(MyLogger As LogConfig)
|
|
Logger = MyLogger.GetLogger()
|
|
End Sub
|
|
Public Function Email_Send(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 = "", Optional Test As Boolean = False)
|
|
Try
|
|
Dim oError As Boolean = False
|
|
Dim oReceipiants As String()
|
|
If mailto.Contains(";") Then
|
|
oReceipiants = mailto.Split(";")
|
|
Else
|
|
ReDim Preserve oReceipiants(0)
|
|
oReceipiants(0) = mailto
|
|
End If
|
|
For Each oMailReceipiant As String In oReceipiants
|
|
Dim sClient = New Net.Mail.SmtpClient(mailsmtp)
|
|
Dim mymesssage As New MailMessage
|
|
sClient.Port = mailport
|
|
If AUTH_TYPE = "SSL" Then
|
|
sClient.EnableSsl = True
|
|
Else
|
|
sClient.EnableSsl = False
|
|
End If
|
|
|
|
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
|
|
|
|
'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($"working on attachment {attment.ToString}...")
|
|
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
|
|
|
|
|
|
mymesssage.From = New MailAddress(mailfrom)
|
|
mymesssage.Subject = mailSubject
|
|
mymesssage.To.Add(New MailAddress(oMailReceipiant))
|
|
sClient.Send(mymesssage)
|
|
Logger.Info($"Email successfully send to: [{oMailReceipiant}]!")
|
|
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
|
|
End Class
|