SendEmail Function .Net 2.0 VB

This function handles most of the basic email options including a single CC and single BCC. I'll add the ability to use multiple CC's and BCC's and Attachments in the future.

If you didn't know already there is a great resource for this at They have minimal and advanced examples.

Public Shared Function SendEmail( _
     ByVal Mail_Server As String _
    , ByVal Mail_Server_Login As String _
    , ByVal Mail_Server_Password As String _
    , ByVal From_Email As String _
    , ByVal From_Name As String _
    , ByVal To_Email As String _
    , ByVal To_Name As String _
    , ByVal Subject As String _
    , ByVal Body As String _
    , Optional ByVal To_CC As String = "" _
    , Optional ByVal To_BCC As String = "" _
    , Optional ByVal IsBodyHTML As Boolean = False _
    , Optional ByVal MailPriority As MailPriority = 0 _
    ) As Boolean
        Dim Message As New MailMessage()
        Message.From = New MailAddress(From_Email, From_Name)
        Message.To.Add(New MailAddress(To_Email, To_Name))
        'Message.To.Add(New MailAddress(""))
        If To_CC.Length > 0 Then
            Message.CC.Add(New MailAddress(To_CC))
        End If
        If To_BCC.Length > 0 Then
            Message.Bcc.Add(New MailAddress(To_BCC))
        End If
        Message.Subject = Subject
        Message.Body = Body
        Message.Priority = MailPriority
        Message.IsBodyHtml = IsBodyHTML
        Dim SMTPClient As New SmtpClient(Mail_Server)
        SMTPClient.Credentials = New NetworkCredential(Mail_Server_Login, Mail_Server_Password)
        Return True
    Catch ex As Exception
        Return False
    End Try
End Function

No comments:

Post a Comment