Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base

Public Class WebParameters_VC
	Inherits DevExpress.ExpressApp.ViewController

	Public Sub New()
		MyBase.New()
		InitializeComponent()
		RegisterActions(components) 
	End Sub

    Private Sub aSMTPTest_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aSMTPTest.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _WebParameters As WebParameters = TryCast(View.SelectedObjects(0), WebParameters)
            If _WebParameters Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")

            If String.IsNullOrEmpty(_WebParameters.SMTPHost) Then Throw New Exception("*Nincs megadva SMTP szerver!")
            If _WebParameters.SMTPPort < 1 Then Throw New Exception("*Érvénytelen SMTP Port!")
            If String.IsNullOrEmpty(_WebParameters.SMTPUserName) Then Throw New Exception("*Nincs megadva SMTP Felhasználói név!")
            If String.IsNullOrEmpty(_WebParameters.SMTPPassword) Then Throw New Exception("*Nincs megadva SMTP jelszó!")
            If String.IsNullOrEmpty(_WebParameters.Email_SupportAdmin) Then Throw New Exception("*Nincs megadva Support Admin e-mail cím!")
            If _WebParameters.HTMLMailTemplate_QuestionUpdate Is Nothing Then Throw New Exception("*Nincs beállítva Kérdés e-mail sablon!")
            If _WebParameters.HTMLMailTemplate_QuestionUpdate Is Nothing Then Throw New Exception("*Nincs beállítva Státusz e-mail sablon!")
            If _WebParameters.HTMLMailTemplate_AnswerUpdate Is Nothing Then Throw New Exception("*Nincs beállítva Válasz e-mail sablon!")

            Dim _MailMessage As New System.Net.Mail.MailMessage
            Dim _SmtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient(_WebParameters.SMTPHost)
            _MailMessage.From = New System.Net.Mail.MailAddress(_WebParameters.Email_SupportAdmin)
            _MailMessage.To.Add(_WebParameters.Email_SupportAdmin)
            _MailMessage.Subject = "Nuvolar SMTP beállítások - T E S Z T    E M A I L"
            _MailMessage.IsBodyHtml = True

            Dim _EmailBody As String = ""
            _EmailBody += String.Format("<b>SMTP szerver: </b>{0}<br><b>SMTP Port: </b>{1}<br><b>SMTP Felhasználó: </b>{2}<br><b>SMTP jelszó: </b>{3}<br><br>", _
                                      _WebParameters.SMTPHost, _WebParameters.SMTPPort, _WebParameters.SMTPUserName, _WebParameters.SMTPPassword)

            _EmailBody += "<h1><b><u>Kérdés sablon:</h1></b></u><br><br>"
            _EmailBody += _WebParameters.HTMLMailTemplate_QuestionStatusUpdate.HTMLBody
            _EmailBody += "<h1><b><u>Státusz sablon:</h1></b></u><br><br>"
            _EmailBody += _WebParameters.HTMLMailTemplate_QuestionStatusUpdate.HTMLBody
            _EmailBody += "<h1><b><u>Válasz sablon:</h1></b></u><br><br>"
            _EmailBody += _WebParameters.HTMLMailTemplate_AnswerUpdate.HTMLBody

            _MailMessage.Body = _EmailBody

            _SmtpClient.Port = _WebParameters.SMTPPort
            _SmtpClient.Credentials = New System.Net.NetworkCredential(_WebParameters.SMTPUserName, _WebParameters.SMTPPassword)
            _SmtpClient.EnableSsl = _WebParameters.EnableSsl

            _SmtpClient.Send(_MailMessage)

            Throw New Exception("*A teszt sikeres!" + vbNewLine + "Kérem ellenőrizze az e-mail-t!" + vbNewLine + _
                                String.Format("A címzett: {0}", _WebParameters.Email_SupportAdmin))
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly, "SMTP teszt")
        End Try
    End Sub
End Class
