DeveloperBarn Forums

DeveloperBarn

Programming & IT forum

Email Class

This is a discussion on Email Class within the .Net Code Samples forums, part of the .Net Development category; This is an email class that a few of us use where I work: Code: Imports System.Net.Mail Namespace MyApp.Utilities Public ...

Go Back   DeveloperBarn Forums > Programming & Scripting > .Net Development > .Net Code Samples

  #1  
Old July 7th, 2009, 08:43 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Real name: Jason
Location: Washington, D.C.
Posts: 1,953
Blog Entries: 8
Rep Power: 15
jmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud of
Default 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 with me... click the icon!
If my post solved your problem, click the button in the lower right-hand corner of the post.

If you like it here...throw us a few bones to help
support us.

Join our Folding team: DeveloperBarn Folding

Reply With Quote
  #2  
Old July 8th, 2009, 03:56 AM
micky's Avatar
Lazy Bum
 
Join Date: Jul 2008
Location: India
Posts: 566
Rep Power: 4
micky has a spectacular aura aboutmicky has a spectacular aura aboutmicky has a spectacular aura about
Default

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
__________________
Get the Mantra!
Reply With Quote
  #3  
Old July 8th, 2009, 07:49 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Real name: Jason
Location: Washington, D.C.
Posts: 1,953
Blog Entries: 8
Rep Power: 15
jmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud of
Default

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.
Reply With Quote
Reply

  DeveloperBarn Forums > Programming & Scripting > .Net Development > .Net Code Samples

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

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


All times are GMT -4. The time now is 10:21 PM.


Copyright ©2008-2010, DeveloperBarn

Content Relevant URLs by vBSEO 3.3.2