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 ...
| |||||||
|
#1
| ||||
| ||||
| 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:
__________________ 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 |
|
#2
| ||||
| ||||
| 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
__________________ Get the Mantra! |
|
#3
| ||||
| ||||
| 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. |
![]() |
|
| Bookmarks |
| Thread Tools | |
| Display Modes | |
| |
| ||||
| 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 |