Skip to main content

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

Comments

Popular posts from this blog

Excel document for pinging list of computers VBA

Here is an excel document I created that will ping a list of nodes in column A and give results in column B. There are much better tools that can be used such as angry ip scanner  http://www.angryip.org/w/Home . I just wrote this as an example Requirements: Tested with Office 2010 Download: Download

Powershell - Com+ Application Recycle

Needed a script to recycle a com+ application nightly and this is what i came up with. This script will write each recycle it does to the event log under application. Run locally or via a scheduled task. #Recycle COM+ Application and write to the event log the status # 1.0 Release # Run script locally # Write to the event log ######################################## #Configurable ######################################## #Com+ ApplicationName $ComPlusLikeAppName = "Put the name of Com+ Application here a like statement is used to eval so you can get away with putting part of it" #EventLog to write to. $eventlog = "Application" #Source for eventlog. $source = "RecycleComObject" #Successful Event ID $SEventID = 0 #Error Event ID $EEventID = 666 #Process that COM+ runs under $process = "dllhost.exe". ######################################## #Clear $CurrentMemory = $null $PRocessID = $null $Commandline = $null $GUID = $null $AppID = $null $Messag

Powershell : Certutil Find Expired Certs on CA server

Wrote this to get certificate expiration information for certificates that expired 5 days ago to ones that expire in 90 days. Wrap an invoke-command around this for remote query. $Before = (get-date).adddays(90).ToString("MM/dd/yyyy") $After = (get-date).AddDays(-5).ToString("MM/dd/yyyy") <# https://blogs.technet.microsoft.com/poshchap/2016/01/01/powershell-and-certutil-exe/ We create a date range with $Before, i.e. certificates expiring before this date, and $After, i.e. certificates expiring after this date. These values are converted into something that certutil can understand - $Restrict. This is then used with the certutil -restrict parameter. #> $Restrict = "NotAfter<=$Before,NotAfter>=$After" $Report = @() $cmd = & certutil.exe -view -restrict $Restrict -out "RequesterName,CommonName,Certificate Expiration Date","Certificate Template" $SplitLines = $cmd.Split("`n`r") $Index = 0 foreach ($line in $Sp