[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 195
  • Last Modified:

Console App: Generate an email alert

How would I go about writing a console app to check if a file exists on a network folder and then send an email alert....I have done this with winforms..but cant see to get started in a console environment.
0
Robb Hill
Asked:
Robb Hill
1 Solution
 
Éric MoreauSenior .Net ConsultantCommented:
it is exactly the same thing
0
 
JipFromParisCommented:
Assuming you are using Visual Studio, you should create a new "Console Application" project. The wizard will generate a single class and a single Main method. This method will be the entry point for your application. Arguments for the Main method is an array of strings. Each one will be populated with a single argument from the command line at runtime. As stated by emoreau, the programming techniques you will use should be the same. Copying/Pasting most of your WinForms application should do the job. One last note : you can use the many static methods from the System.Console class in order to write text to the command window. Other methods let you control almost any aspect of the command window. Hope it helps.
0
 
Robb HillSenior .Net DeveloperAuthor Commented:
Here was my solution less the app config settings



Imports System.Net.Mail
Imports System.io

Module Module1

    Sub Main()

        'Open a file for reading
        Dim FILENAME As String = "c:/Scheduler.log"
        Dim looptest As Boolean = True

        'Get a StreamReader class that can be used to read the file
        Dim objStreamReader As StreamReader
        objStreamReader = File.OpenText(FILENAME)

        'Now, read the entire file into a string
        Dim contents As String = objStreamReader.ReadToEnd()

        Dim a() As String = Split(contents, vbCrLf)
        Dim now As DateTime = DateTime.Now.ToShortDateString
        For i As Integer = 0 To a.Length - 1

            Dim strArray() As String = a(i).Split("-")
            strArray(1) = LTrim(strArray(1))
            Dim strDate() As String = strArray(1).Split(" ")

            If strDate(0) = now Then
                looptest = True
                Exit Sub
            Else
                looptest = False
            End If
        Next

        If looptest = False Then
            Dim mMailMessage As New MailMessage()
            'recipient e-mail address.
            mMailMessage.To.Add(New MailAddress(My.Settings.EmailAddress))
            ' Set the subject of the mail message
            mMailMessage.Subject = "ALERT"
            ' Set the body of the mail message
            mMailMessage.Body = "<HTML><BODY><B>ALERT! The system did not process the ADS_Blend Job</B></BODY></HTML>"
            ' Set the format of the mail message body as HTML
            mMailMessage.IsBodyHtml = True
            ' Set the priority of the mail message to normal
            mMailMessage.Priority = MailPriority.Normal

            ' Instantiate a new instance of SmtpClient
            Dim mSmtpClient As New SmtpClient()
            '    mSmtpClient.Host = "127.0.0.1"
            ' Send the mail message
            mSmtpClient.Send(mMailMessage)
        End If
    End Sub
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now