VB.NET  •  Send E-mail With SMTP

Listing 3. Sending an e-mail message is easier than ever with the .NET Framework classes. This code uses an SMTP e-mail server you define in the app settings to send notifications of file-change violations and to send summary reports. The code sends e-mail to a list of semicolon-delimited addresses.

Private Sub SendEmail(ByVal Subject As String, _
   ByVal MessageText As String)
   Dim sAddr As String
   Dim sAddrs() As String = _
      Split(m_MonitorControl.EmailToList, ";")
   If Not IsNothing(sAddrs) Then
      Dim mailMsg As New MailMessage()
      With mailMsg
         .From = m_MonitorControl.EmailFrom
         .Subject = Subject
         .Body = Now.ToString & _
            ControlChars.CrLf & MessageText
         .Priority = MailPriority.High
      End With
      SmtpMail.SmtpServer = _
         m_MonitorControl.SMTPServer
      For Each sAddr In sAddrs
         mailMsg.To = sAddr
         Try
            SmtpMail.Send(mailMsg)
         Catch e As Exception
            EventLog.WriteEntry(ServiceName & _
               " - Error", "Email error: " _
               & e.ToString)
         End Try
      Next
   End If
End Sub