'Option Strict On
Option Explicit On
Imports IntraSell_Net_Base

Public Class Email
    Public Property EmailFrom As String
    Public Property EmailTo As String
    Public Property Subject As String
    Public Property Mailtext As String
    Public Property FilePath As String
    Public Property SofortSenden As Boolean
End Class

Module ModuleMail


    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="cSubject"></param>
    ''' <param name="cName"></param>
    ''' <param name="cAttach">cAttach can be file1;file2;file3</param>
    ''' <param name="cBody"></param>
    ''' <param name="cBodyHTML"></param>
    ''' <param name="SofortSenden"></param>
    Public Sub MailWithOutlook(cSubject As String,
                               cName As String,
                               cAttach As String,
                               cBody As String,
                               cBodyHTML As String,
                               SofortSenden As Boolean)

        Dim fn As Object
        Dim objOutlookMsg
        'Dim objOutlookRecip
        Dim objOutlookAttach

        Dim objOutlook
        objOutlook = CreateObject("Outlook.Application")

        'create object
        objOutlookMsg = objOutlook.createitem(0)
        'objOutlookRecip = objOutlookMsg.Recipients
        objOutlookAttach = objOutlookMsg.Attachments

        objOutlookMsg.display()
        If cName <> "" Then
            objOutlookMsg.Recipients.Add(cName)
        End If

        If cAttach <> "" Then
            For Each fn In Split(cAttach, ";")
                If fn <> "" Then
                    objOutlookAttach.Add(fn)
                End If
            Next
        End If

        objOutlookMsg.Subject = cSubject
        If cBody <> "" Then objOutlookMsg.Body = cBody
        If cBodyHTML <> "" Then objOutlookMsg.htmlBody = cBodyHTML

        If SofortSenden Then
            objOutlookMsg.send()
        End If

    End Sub

    Function GetMailBody(fileName)
        Return IO.File.ReadAllText(fileName)
    End Function

    Public Sub SendAusdruckPerEmail(Vorgangtyp As String,
                                    Vorgang_Nummer As String,
                                    FormatNummer As String,
                                    Dateiname As String,
                                    SofortSenden As Boolean)
        ' a file named vorgangtyp_vorgangnummer.htm is created
        Dim OutputFilename = OpenAusdruck_InWord_Filename_RTF(Vorgangtyp, Vorgang_Nummer, FormatNummer, Dateiname)
        Dim htmlBody = GetMailBody(OutputFilename)
        'send with outlook
        Dim Email As String = GetKundenEmail(Vorgangtyp, Vorgang_Nummer)
        If String.IsNullOrWhiteSpace(Email) Then
            Email = InputBox("Email:", "Email")
        End If

        Dim Subject As String = intrasell.vorgang.GetDruckForType(Vorgangtyp) & " Nr. " & Vorgang_Nummer
        Call MailWithOutlook(Subject, Email, "", "", htmlBody, SofortSenden)

    End Sub

End Module
