181 lines
7.7 KiB
VB.net
181 lines
7.7 KiB
VB.net
|
|
Imports System
|
|
Imports Independentsoft.Email
|
|
Imports Independentsoft.Email.Smtp
|
|
Imports Independentsoft.Email.Mime
|
|
Public Class clsEmail
|
|
Private Shared MailAktiv As Boolean = False
|
|
|
|
'Public Shared Function Init()
|
|
' Try
|
|
' Dim DT As DataTable = clsDatatabase.Return_Datatable("select * from TBDD_EMAIL_ACCOUNT where ACTIVE = 1")
|
|
' If DT.Rows.Count = 1 Then
|
|
' For Each row As DataRow In DT.Rows
|
|
|
|
' MailFrom = row.Item("EMAIL_ABS")
|
|
' MAilSMTP = row.Item("EMAIL_SMTP")
|
|
' MailSSL = row.Item("EMAIL_SSL")
|
|
' MailUser = row.Item("EMAIL_USER")
|
|
' MailUser_PW = row.Item("EMAIL_USER_PW")
|
|
' MailAktiv = True
|
|
' Exit For
|
|
' Next
|
|
' Else
|
|
' MailAktiv = False
|
|
' End If
|
|
|
|
' Return True
|
|
' Catch ex As Exception
|
|
' clsLogger.Add(ex.Message, True, "clsEmail.Init")
|
|
' Return False
|
|
' End Try
|
|
' End Function
|
|
'Public Shared Function Send_EMail(ByVal MailBetreff As String, ByVal vBody As String, MailEmpfaenger As String, MailFrom As String, MAilSMTP As String, MailUser As String, MailUser_PW As String, SSL As Boolean,
|
|
' Optional attment As String = "", Optional test As Boolean = False)
|
|
' '#### E-MAIL NACHRICHT VERSENDEN
|
|
' Try
|
|
' Dim empfaenger As String()
|
|
' If MailEmpfaenger.Contains(";") Then
|
|
' empfaenger = MailEmpfaenger.Split(";")
|
|
' Else
|
|
' ReDim Preserve empfaenger(0)
|
|
' empfaenger(0) = MailEmpfaenger
|
|
' End If
|
|
' 'Für jeden Empfänger eine Neue Mail erzeugen
|
|
' For Each _mailempfaenger As String In empfaenger
|
|
' 'Neue Nachricht erzeugen:
|
|
' Dim message As New MailMessage(MailFrom, _mailempfaenger, MailBetreff,
|
|
' "<font face=""Arial"">" & vBody & "</font>") '& "<br>" &
|
|
' '"<br>Domain: " & Environment.UserDomainName &
|
|
' '"<br>Gesendet am: " & My.Computer.Clock.LocalTime.ToShortDateString & "-" &
|
|
' 'My.Computer.Clock.LocalTime.ToLongTimeString &
|
|
|
|
' ' create and add the attachment(s) */
|
|
' If attment <> String.Empty Then
|
|
' If System.IO.File.Exists(attment) Then
|
|
' Dim Attachment As Attachment = New Attachment(attment)
|
|
' message.Attachments.Add(Attachment)
|
|
' End If
|
|
' End If
|
|
|
|
' With message
|
|
' .IsBodyHtml = True
|
|
' End With
|
|
|
|
' 'Einen SMTP Client erzeugen und Anmeldungsinformationen hinterlegen
|
|
' Dim emailClient As New SmtpClient(MAilSMTP)
|
|
' emailClient.EnableSsl = SSL
|
|
' 'Email mit Authentifizierung
|
|
' Dim SMTPUserInfo As New System.Net.NetworkCredential(MailUser, MailUser_PW) ', My.Settings.vDomain)
|
|
' emailClient.UseDefaultCredentials = False
|
|
' emailClient.Credentials = SMTPUserInfo
|
|
' emailClient.Port = 25
|
|
' clsLogger.Add("==> Email erfolgreich an " & _mailempfaenger & " versendet!", False)
|
|
' clsLogger.Add("==> Text: " & vBody, False)
|
|
' clsLogger.Add("", False)
|
|
' '*Send the message */
|
|
|
|
' emailClient.Send(message)
|
|
' If test = True Then
|
|
' MsgBox("The testmail was send successfully", MsgBoxStyle.Information)
|
|
' End If
|
|
' Next
|
|
' Return True
|
|
' Catch ex As Exception
|
|
' clsLogger.Add(ex.Message, True, "cls.SendEmail")
|
|
' If test = True Then
|
|
' MsgBox("Unexpected error in Send Testmail: " & ex.Message, MsgBoxStyle.Critical)
|
|
' End If
|
|
' Return False
|
|
' End Try
|
|
'End Function
|
|
Public Shared Function Email_Send_Independentsoft(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,
|
|
SSL As Boolean, 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", "plain", "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("c:\testfolder\test.docx")
|
|
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
|
|
message.BodyParts.Add(attachment1)
|
|
End If
|
|
End If
|
|
Dim client As SmtpClient
|
|
Try
|
|
client = New SmtpClient(mailsmtp, mailport)
|
|
Catch ex As Exception
|
|
clsLogger.AddError(ex.Message, "clsEmail.SendMail(Create Client)")
|
|
_error = True
|
|
Continue For
|
|
End Try
|
|
|
|
Try
|
|
client.Connect()
|
|
Catch ex As Exception
|
|
clsLogger.AddError(ex.Message, "clsEmail.SendMail(Client.Connect)")
|
|
_error = True
|
|
Continue For
|
|
|
|
End Try
|
|
client.StartTls()
|
|
Try
|
|
client.Login(mailUser, mailPW)
|
|
Catch ex As Exception
|
|
clsLogger.AddError(ex.Message, "clsEmail.SendMail(Client.Login)")
|
|
_error = True
|
|
client.Disconnect()
|
|
Continue For
|
|
End Try
|
|
Try
|
|
client.Send(message)
|
|
Catch ex As Exception
|
|
clsLogger.AddError(ex.Message, "clsEmail.SendMail(Client.Send)")
|
|
_error = True
|
|
client.Disconnect()
|
|
Continue For
|
|
End Try
|
|
client.Disconnect()
|
|
|
|
Catch ex As Exception
|
|
clsLogger.AddError(ex.Message, "clsEmail.SendMail()")
|
|
_error = True
|
|
End Try
|
|
Next
|
|
clsLogger.WriteLog()
|
|
|
|
If _error = True Then
|
|
Return False
|
|
Else
|
|
Return True
|
|
End If
|
|
End Function
|
|
|
|
End Class
|