I’ve had this question asked of me several times and I thought it would be a nice little snippet of code to provide to my colleagues today. The issue is how to parse or separate out a text field or text area which contains a bunch of emails separated by a delimiter of some type.
It’s actually not as hard as it seems, and I’ll even show you in this example how to add them to the CC portion of your email. So here we go.
// You'll need to put in your server name in place of the DEDM101
Dim obj As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient("DEDM101")
Dim Mailmsg As New System.Net.Mail.MailMessage
Mailmsg.To.Clear()
//Now we can take our text field or area and parse it out using a few common types of delimiters
Dim extraemail As String = Me.tbEmails.Text
//Let's make sure we have values in there
If extraemail.Length > 1 Then
Dim emaillist As String = extraemail
Dim emails As String() = Nothing
Dim sep(3) As Char
sep(0) = ","
sep(1) = ";"
sep(2) = "|"
emails = emaillist.Split(sep)
//Now Add the Emails to the Email Going Out
Dim s As String
For Each s In emails
Mailmsg.CC.Add(New System.Net.Mail.MailAddress(s))
Next s
End If
//Let's fill in the other information we need:
Mailmsg.From = New System.Net.Mail.MailAddress("yourname@yourdomain.com")
Mailmsg.Subject = "Your Subject Here"
Mailmsg.Body = "<strong>Start Title of Email Here</strong><p>Some text here</p>
Mailmsg.IsBodyHtml = True
//Now Send the Email
obj.Send(Mailmsg)
code>
And that's about all it takes. Now you can use this for many other types of applications to parse out or separate other values using asp.NET, but you get the idea. This example is in VB, but if you need it in C#, just let me know and I'll post that as well.
Happy Coding!







