'  
'   Rebex Sample Code License
' 
'   Copyright 2025, REBEX CR s.r.o.
'   All rights reserved.
'   https://www.rebex.net/
' 
'   Permission to use, copy, modify, and/or distribute this software for any
'   purpose with or without fee is hereby granted.
' 
'   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
'   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
'   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
'   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
'   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
'   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
'   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
'   OTHER DEALINGS IN THE SOFTWARE.
' 

Imports System
Imports System.IO
Imports System.Text
Imports Rebex.Mail

Class Program

    <STAThread()> Shared Function Main(ByVal args() As String) As Integer
            ' set the trial license key
            Rebex.Licensing.Key = Rebex.Samples.TrialKey.Key


        ' If no argument specified show syntax
        If args.Length < 1 Then
            ShowHelp()
            Return 1
        End If

        ' Load the mail message from disk
        Dim mail As New MsgMessage
        mail.Load(args(0))

        Console.WriteLine(
            "Message contains {0} attachments.",
            mail.Attachments.Count
        )

        ' If message has no attachments, just exit
        If mail.Attachments.Count = 0 Then Return 0

        For Each attachment As MsgAttachment In mail.Attachments
            ' Print attachment's file name and Content-Type
            Console.WriteLine(
                "Saving '{0}' ({1}).",
                attachment.FileName,
                attachment.Properties.GetValue(MsgPropertyTag.AttachMimeTag, MediaTypeNames.Application.Octet)
            )

            ' Save the attachment to file 
            attachment.Save(attachment.FileName)
        Next attachment

        Return 0
    End Function    'Main

    Private Shared Sub ShowHelp()
        Dim applicationName As String = AppDomain.CurrentDomain.FriendlyName
        Console.WriteLine("=====================================================================")
        Console.WriteLine(" {0} ", applicationName)
        Console.WriteLine("=====================================================================")
        Console.WriteLine("")
        Console.WriteLine("Extracts all attachments from .msg e-mail message.")
        Console.WriteLine("Supported e-mail formats: .msg (Microsoft Outlook).")
        Console.WriteLine("")
        Console.WriteLine("The program is a sample for Rebex MSG for .NET component.")
        Console.WriteLine("For more info, see https://www.rebex.net/msg/")
        Console.WriteLine("")
        Console.WriteLine("Syntax is: {0} <mailfile.msg>", applicationName)
    End Sub    'ShowHelp


End Class 'Program