Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text
Imports System.Net.Mail
Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.Data.Filtering
Imports System.IO
Imports DevExpress.ExpressApp.Utils

Public Class EMailSendQueueVC
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
    End Sub

    Private Sub aEMailSendQueue_Send_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aEMailSendQueue_Send.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            If View.SelectedObjects.Count = 0 Then Throw New Exception("*Nincs kiválasztva egyetlen sor sem! A folyamat megszakadt!")
            Dim _WebParameters As WebParameters = _ocur.FindObject(Of WebParameters)(CriteriaOperator.Parse("1=1"))
            If String.IsNullOrEmpty(_WebParameters.SMTPHost) Or
                String.IsNullOrEmpty(_WebParameters.SMTPPassword) Or
                String.IsNullOrEmpty(_WebParameters.SMTPPort) Or
                String.IsNullOrEmpty(_WebParameters.SMTPUserName) Then Throw New Exception("*Hiányosak az SMTP Server adatok! Kérem, ellenőrizze!")

            For Each _EMailSendQueueHeader As EMailSendQueueHeader In View.SelectedObjects
                If _EMailSendQueueHeader.EMailSendQueueRows.Count > 0 Then
                    Dim _SMTPServer As New SmtpClient
                    With (_SMTPServer)
                        .Credentials = New Net.NetworkCredential(_WebParameters.SMTPUserName, _WebParameters.SMTPPassword)
                        .Port = _WebParameters.SMTPPort
                        .Host = _WebParameters.SMTPHost
                        .EnableSsl = _WebParameters.EnableSsl
                    End With
                    For Each _EMailSendQueueRows As EMailSendQueueRows In _EMailSendQueueHeader.EMailSendQueueRows
                        Try
                            Dim _Mail As New MailMessage
                            With (_Mail)
                                .From = New MailAddress(_EMailSendQueueRows.EMailFrom)
                                .To.Add(_EMailSendQueueRows.EMailTo)
                                .Subject = _EMailSendQueueRows.Subject
                                .IsBodyHtml = True
                                .Body = _EMailSendQueueRows.HTMLBody
                                If _EMailSendQueueRows.FileAttachments1 IsNot Nothing Then
                                    Dim _MemoryStream As New MemoryStream()
                                    _EMailSendQueueRows.FileAttachments1.SaveToStream(_MemoryStream)
                                    _MemoryStream.Seek(0, System.IO.SeekOrigin.Begin)
                                    Dim _Attachment = New Attachment(_MemoryStream, _EMailSendQueueRows.FileAttachments1.FileName, "application/pdf")
                                    .Attachments.Add(_Attachment)
                                End If
                                If _EMailSendQueueRows.FileAttachments2 IsNot Nothing Then
                                    Dim _MemoryStream As New MemoryStream()
                                    _EMailSendQueueRows.FileAttachments2.SaveToStream(_MemoryStream)
                                    _MemoryStream.Seek(0, System.IO.SeekOrigin.Begin)
                                    Dim _Attachment = New Attachment(_MemoryStream, _EMailSendQueueRows.FileAttachments2.FileName, "application/pdf")
                                    .Attachments.Add(_Attachment)
                                End If
                                If _EMailSendQueueRows.FileAttachments3 IsNot Nothing Then
                                    Dim _MemoryStream As New MemoryStream()
                                    _EMailSendQueueRows.FileAttachments3.SaveToStream(_MemoryStream)
                                    _MemoryStream.Seek(0, System.IO.SeekOrigin.Begin)
                                    Dim _Attachment = New Attachment(_MemoryStream, _EMailSendQueueRows.FileAttachments3.FileName, "application/pdf")
                                    .Attachments.Add(_Attachment)
                                End If

                                For Each _EMailSendQueueRows_FileAttachment As EMailSendQueueRows_FileAttachment In _EMailSendQueueRows.EMailSendQueueRows_FileAttachment
                                    Dim _MemoryStream As New MemoryStream()
                                    _EMailSendQueueRows_FileAttachment.FileAttachments.SaveToStream(_MemoryStream)
                                    _MemoryStream.Seek(0, System.IO.SeekOrigin.Begin)
                                    Dim _Attachment = New Attachment(_MemoryStream, _EMailSendQueueRows_FileAttachment.FileAttachments.FileName, "application/pdf")
                                    .Attachments.Add(_Attachment)
                                Next
                            End With

                            _SMTPServer.Send(_Mail)
                            _EMailSendQueueRows.ExceptionMessage = ""
                        Catch ex As Exception
                            _EMailSendQueueRows.ExceptionMessage = ex.Message
                        End Try
                    Next
                End If
                _EMailSendQueueHeader.IsExecuted = True
            Next
            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
End Class
