Imports System.Net.Mail


Public Class dbMailHelper
  ''' <summary>
  ''' Sends an mail message
  ''' </summary>
  ''' <param name="from">Sender address</param>
  ''' <param name="toEmail">Recepient address</param>
  ''' <param name="bcc">Bcc recepient</param>
  ''' <param name="cc">Cc recepient</param>
  ''' <param name="subject">Subject of mail message</param>
  ''' <param name="body">Body of mail message</param>
  Public Sub SendMailMessage(ByVal from As String, ByVal toEmail As String, ByVal bcc As String, ByVal cc As String, ByVal subject As String, ByVal body As String)
    ' Instantiate a new instance of MailMessage
    Dim mMailMessage As New MailMessage()

    ' Set the sender address of the mail message
    mMailMessage.From = New MailAddress(from)
    ' Set the recepient address of the mail message
    mMailMessage.To.Add(New MailAddress(toEmail))
    ' Check if the bcc value is null or an empty string
    If (bcc IsNot Nothing) AndAlso (bcc <> String.Empty) Then
      ' Set the Bcc address of the mail message
      mMailMessage.Bcc.Add(New MailAddress(bcc))
    End If

    ' Check if the cc value is null or an empty value
    If (cc IsNot Nothing) AndAlso (cc <> String.Empty) Then
      ' Set the CC address of the mail message
      mMailMessage.CC.Add(New MailAddress(cc))
    End If
    ' Set the subject of the mail message
    mMailMessage.Subject = subject
    ' Set the body of the mail message
    mMailMessage.Body = body
    ' 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()
    ' Send the mail message
    mSmtpClient.Send(mMailMessage)
  End Sub
  ''' <summary>
  ''' Sends an mail message
  ''' </summary>
  ''' <param name="from">Sender address</param>
  ''' <param name="toEmail">Recepient address</param>
  ''' <param name="bcc">Bcc recepient</param>
  ''' <param name="cc">Cc recepient</param>
  ''' <param name="subject">Subject of mail message</param>
  ''' <param name="body">Body of mail message</param>
  ''' <param name="oAttachment">Single mail attachment</param>
  Public Shared Sub SendMailMessage(ByVal from As String, ByVal toEmail As String, ByVal bcc As String, ByVal cc As String, ByVal subject As String, ByVal body As String, _
   ByVal oAttachment As Attachment)
    ' Instantiate a new instance of MailMessage
    Dim mMailMessage As New MailMessage()

    ' Set the sender address of the mail message
    mMailMessage.From = New MailAddress(from)
    ' Set the recepient address of the mail message
    mMailMessage.To.Add(New MailAddress(toEmail))
    ' Check if the bcc value is null or an empty string
    If (bcc IsNot Nothing) AndAlso (bcc <> String.Empty) Then
      ' Set the Bcc address of the mail message
      mMailMessage.Bcc.Add(New MailAddress(bcc))
    End If

    ' Check if the cc value is null or an empty value
    If (cc IsNot Nothing) AndAlso (cc <> String.Empty) Then
      ' Set the CC address of the mail message
      mMailMessage.CC.Add(New MailAddress(cc))
    End If
    ' Set the subject of the mail message
    mMailMessage.Subject = subject
    ' Set the body of the mail message
    mMailMessage.Body = body
    ' 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

    ' adds the attachment if specified
    If oAttachment IsNot Nothing Then
      mMailMessage.Attachments.Add(oAttachment)
    End If

    ' Instantiate a new instance of SmtpClient
    Dim mSmtpClient As New SmtpClient()
    ' Send the mail message
    mSmtpClient.Send(mMailMessage)
  End Sub

End Class
