+ Reply to Thread
Results 1 to 3 of 3

Thread: Email Class

  1. #1
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    Email Class

    This is an email class that a few of us use where I work:

    Code:
    Imports System.Net.Mail
    Namespace MyApp.Utilities
        Public Class Email
            Public Shared Function SendEmail(ByVal emailTo As String, ByVal emailFrom As String, _
                                             ByVal emailSubject As String, ByVal emailBody As String, _
                                             ByVal cc As String, ByVal bcc As String, ByVal priority As MailPriority, _
                                             ByVal isEmailHtml As Boolean, ByVal sendAsync As Boolean) As Boolean
                Dim usrEmail As New MailMessage
                Dim usrEmailFrom As New MailAddress(emailFrom)
                usrEmail.From = usrEmailFrom
                getEmailTo(emailTo, usrEmail)
                getEmailCC(cc, usrEmail)
                getEmailBcc(bcc, usrEmail)
                usrEmail.Subject = emailSubject
                usrEmail.Body = emailBody
                usrEmail.IsBodyHtml = isEmailHtml
                usrEmail.Priority = priority
                Dim smtp As New SmtpClient
                If sendAsync Then
                    smtp.SendAsync(usrEmail, Nothing)
                Else
                    smtp.Send(usrEmail)
                End If
                usrEmail = Nothing
                Return True
            End Function
            Private Shared Sub getEmailTo(ByVal emailto As String, ByVal usrEmail As MailMessage)
                Dim arrayEmailTo As Array = emailto.Split(CChar(";"))
                Dim emailAddress As String = ""
                For Each emailAddress In arrayEmailTo
                    If Trim(emailAddress) <> "" Then
                        usrEmail.To.Add(emailAddress)
                    End If
                Next
            End Sub
            Private Shared Sub getEmailCC(ByVal emailcc As String, ByVal usrEmail As MailMessage)
                Dim arrayEmailTo As Array = emailcc.Split(CChar(";"))
                Dim emailAddress As String = ""
                For Each emailAddress In arrayEmailTo
                    If Trim(emailAddress) <> "" Then
                        usrEmail.To.Add(emailAddress)
                    End If
                Next
            End Sub
            Private Shared Sub getEmailBcc(ByVal emailbcc As String, ByVal usrEmail As MailMessage)
                Dim arrayEmailTo As Array = emailbcc.Split(CChar(";"))
                Dim emailAddress As String = ""
                For Each emailAddress In arrayEmailTo
                    If Trim(emailAddress) <> String.Empty Then
                        .To.Add(emailAddress)
                    End If
                Next
            End Sub
        End Class
    End Namespace
    



    To use:
    • Import MyApp.Utilities
    • Call Email.SendEmail and provide the appropriate parameters
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  2. #2
    Lazy Bum micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky's Avatar
    Join Date
    Jul 2008
    Location
    India
    Posts
    683
    Blog Entries
    1
    Rep Power
    6

    J, i think you have a small problem in ur code.
    It should be this
    Code:
    Private Shared Sub getEmailTo(ByVal emailto As String, ByVal usrEmail As MailMessage)
                Dim arrayEmailTo As Array = emailto.Split(CChar(";"))
                Dim emailAddress As String = ""
                For Each emailAddress In arrayEmailTo
                    If Trim(emailAddress) <> "" Then
                        usrEmail.To.Add(emailAddress)
                    End If
                Next
            End Sub
            Private Shared Sub getEmailCC(ByVal emailcc As String, ByVal usrEmail As MailMessage)
                Dim arrayEmailTo As Array = emailcc.Split(CChar(";"))
                Dim emailAddress As String = ""
                For Each emailAddress In arrayEmailTo
                    If Trim(emailAddress) <> "" Then
                        usrEmail.To.Add(emailAddress)
                    End If
                Next
            End Sub
            Private Shared Sub getEmailBcc(ByVal emailbcc As String, ByVal usrEmail As MailMessage)
                Dim arrayEmailTo As Array = emailbcc.Split(CChar(";"))
                Dim emailAddress As String = ""
                For Each emailAddress In arrayEmailTo
                    If Trim(emailAddress) <> String.Empty Then
                        .To.Add(emailAddress)
                    End If
                Next
            End Sub
    
    And it shows all the emails in TO field

  3. #3
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    Thanks M, I had to type this all out because the code is on a separate network here at work. Simple typo

    As far as the emails in the TO field, we'll discuss that in your thread. The above code is designed to do that, but I mentioned in your thread how to do this.
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


+ Reply to Thread

Similar Threads

  1. Global Error Class
    By richyrich in forum .Net Development
    Replies: 14
    Last Post: March 23rd, 2009, 10:32 AM
  2. Button Class
    By richyrich in forum HTML & CSS Help
    Replies: 18
    Last Post: February 27th, 2009, 12:32 PM
  3. class name is ambiguous
    By richyrich in forum .Net Development
    Replies: 11
    Last Post: August 12th, 2008, 11:11 AM
  4. FTP Class Library?
    By Wolffy in forum .Net Development
    Replies: 6
    Last Post: May 30th, 2008, 10:26 AM
  5. Class library
    By Shem in forum .Net Development
    Replies: 11
    Last Post: May 22nd, 2008, 07:01 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO