Tuesday, November 22, 2011


This is an example of a vb.net 2010 console application i wrote to send emails off an open relay.


Imports System.Net.Mail

Module Module1

    Sub Main()
        'Arguments
        Dim inputArgumentEmailto As String = "/emailto="
        Dim inputArgumentSubject As String = "/subject="
        Dim inputArgumentSMTP As String = "/smtp="
        Dim inputArgumentBody As String = "/body="
        Dim inputArgumentEmailFrom As String = "/emailfrom="
        Dim inputArgumentQuestion As String = "/?"

        '  Dim inputQuestion As String = ""
        Dim inputEmailto As String = ""
        Dim inputSubject As String = ""
        Dim inputSMTP As String = ""
        Dim inputBody As String = ""
        Dim inputEmailFrom As String = ""

        Dim strTestArgs As Boolean
     
     

        For Each s As String In My.Application.CommandLineArgs

            If s.ToLower.StartsWith(inputArgumentQuestion) Then
                ' if /? is an argument then post help

                Console.WriteLine("Email Application")
                Console.WriteLine("Created by Tony Unger 11/22/2011")
                Console.WriteLine("Ver. 1.0")
                Console.WriteLine("This requires an open relay")
                Console.WriteLine("------------------------------")
                Console.WriteLine("Parameters")
                Console.WriteLine("")
                Console.WriteLine("/emailto=")
                Console.WriteLine("/subject=")
                Console.WriteLine("/smtp=")
                Console.WriteLine("/body=")
                Console.WriteLine("/emailfrom=")
                Console.WriteLine("")
                Console.WriteLine("Example:")
                Console.WriteLine("/emailto=toTony@asdf.com")
                Console.WriteLine("/subject=**Alert")
                Console.WriteLine("/smtp=192.168.1.1")
                Console.WriteLine("/emailfrom=FromTony@asdf.com")
                Console.WriteLine("/body=body")
                Console.WriteLine(" ""/body=This is an alert"" ")

                Exit Sub

            End If

            'Sets arg to string values
            If s.ToLower.StartsWith(inputArgumentEmailto) Then
                inputEmailto = s.Remove(0, inputArgumentEmailto.Length)
            End If

            If s.ToLower.StartsWith(inputArgumentSubject) Then
                inputSubject = s.Remove(0, inputArgumentSubject.Length)
            End If

            If s.ToLower.StartsWith(inputArgumentSMTP) Then
                inputSMTP = s.Remove(0, inputArgumentSMTP.Length)
            End If

            If s.ToLower.StartsWith(inputArgumentBody) Then
                inputBody = s.Remove(0, inputArgumentBody.Length)
            End If

            If s.ToLower.StartsWith(inputArgumentEmailFrom) Then
                inputEmailFrom = s.Remove(0, inputArgumentEmailFrom.Length)
            End If

        Next

        'Checks if all args are there
        If inputEmailto = "" Then
            Console.WriteLine("/emailto= is required")
            strTestArgs = True
        End If
        If inputSubject = "" Then
            Console.WriteLine("/subject= is required")
            strTestArgs = True
        End If
        If inputSMTP = "" Then
            Console.WriteLine("/smtp= is required")
            strTestArgs = True
        End If
        If inputBody = "" Then
            Console.WriteLine("/body= is required")
            strTestArgs = True
        End If
        If inputEmailFrom = "" Then
            Console.WriteLine("/emailfrom= is required")
            strTestArgs = True
        End If


        ' If any args are missing exit sub
        If strTestArgs = True Then
            Exit Sub
        End If
        'Email
        EmailtoSupport(inputEmailto, inputSubject, inputSMTP, inputBody, inputEmailFrom)
    End Sub

    Public Sub EmailtoSupport(inputEmailto As String, inputSubject As String, inputSMTP As String, inputBody As String, inputEmailFrom As String)


        Try
            Dim Mail As New MailMessage
            Mail.Subject = inputSubject
            Mail.To.Add(inputEmailto)
            Mail.From = New MailAddress(inputEmailFrom)
            Mail.Body = inputBody

            Dim SMTP As New SmtpClient(inputSMTP)
            SMTP.Port = "25"
            SMTP.Send(Mail)
            Console.WriteLine("Email Sent!")

        Catch ex As Exception
            If ex.Message.ToString = "Failure sending mail." Then
                Console.WriteLine("There was a failure sending the email.")
                Console.WriteLine("check your smtp address")
                Console.WriteLine("This program will only use port 25")
            Else
                Console.WriteLine(ex.Message.ToString)
            End If

        End Try
    End Sub

End Module

No comments:

Post a Comment