Send email to multiple addresses.

drew4663

Well-known member
Joined
May 3, 2007
Messages
62
Programming Experience
1-3
I am using vs 2005. I have a form that has a list of 15 checkboxes that have emails attached to them. I can post more code but I have some of it right here:

'This is everyone
Dim techs As String
techs = "email1@server.org;"
ToAddress.AppendText(techs)

'Tech1 you get the idea!
Dim tech1 As String
tech1 = "email2@server.org;"
ToAddress.AppendText(tech1)

Dim tech2 As String
tech2 = "email@server.com;"
ToAddress.AppendText(tech2)

It rights the text in there but it doesn't accept either format...

email1.server.org; email2@server.org;
email1.server.org;email2@server.org;

If I do either it gives me an exception error but if I only have one email address it's ok. FYI - I can't even have the semi-colon after the e-mail address or it will throw an exception error.

here is the error when debugging:

The specified string is not in the form required for an e-mail address. FormatException was unhandled.
Dim <begin highlighted error>mailmsg As new MailMessage(From.Text.Trim, ToAddress.Text.Trim)<end highlighted error>


Any help or ideas that I am easily overlooking would be great. Thank you.
 
I looked at the link but I'm not sure that's the answer I need. It kind of defeats the purpose of my goal not only the problem I was having with manually entering the email addresses. See, not only will the help desk be able to select pre-entered email addresses they need to have the ability to manually type it in a textbox. Not sure how the link you provided will help in either situation. Thanks for trying though.
 
So your goal is not sending a mail to multiple recipients? There is always string manipulation you can use you know, say if you have a semi-colon delimited string list of addresses you can divide it and pump in all the recips. It will also have the benefit of independent validation of each address, and possibly a meaningful error message to the user about this.
 
The process is like this....

The help desk enters in data for a work order and then chooses who receives the work order. When the user hits complete it saves the data and sends the email.

There is a To: and Cc: text box that the user can either enter in the email addresses manually or select a check box assigned to a specific technician. The example you showed me was a predefined selection of emails on the back end. I need the help desk person to be able to select the email addresses not have them pre-defined in the code.

Apart from that fact I am still not able to manually type in mulitple addresses. I can manaully type in a single address and it goes through just fine but as soon as I type in a semi-colon the whole thing shuts down. Here is some more code....

VB.NET:
    Private Sub Send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Send.Click

        If ToAddress.Text = "" Or From.Text = "" Then
            MsgBox("You must enter both To and From email addresses.")
            Exit Sub
        End If

        Dim sb As New StringBuilder()

        sb.Append("The following email was sent to you from the Send Mail " & _
            "sample application:")
        sb.Append(vbCrLf)
        sb.Append(vbCrLf)
        sb.Append("SUBJECT: ")
        sb.Append(Trim(Subject.Text))
        sb.Append(vbCrLf)
        sb.Append(vbCrLf)
        sb.Append("MESSAGE: ")
        sb.Append(Trim(Body.Text))
        sb.Append(vbCrLf)

        Dim mailMsg As New MailMessage(From.Text.Trim, ToAddress.Text.Trim)
        With mailMsg
            If Not String.IsNullOrEmpty(CC.Text) Then
                .CC.Add(New MailAddress(CC.Text.Trim))
            End If

            If Not String.IsNullOrEmpty(BCC.Text) Then
                .Bcc.Add(New MailAddress(BCC.Text.Trim))
            End If

            .Subject = Subject.Text.Trim
            .Body = sb.ToString

            If Not IsNothing(arlAttachments) Then
                Dim mailAttachment As Attachment
                For Each mailAttachment In arlAttachments
                    .Attachments.Add(mailAttachment)
                Next
            End If
        End With

        Try
            Dim client As New SmtpClient("XXX.XX.XX.X")
            client.Send(mailMsg)
            Attachments.Items.Clear()
            Attachments.Items.Add("(No Attachments)")

            MessageBox.Show("Your email has been successfully sent!", _
                "Email Send Status", MessageBoxButtons.OK, _
                MessageBoxIcon.Information)
        Catch exp As Exception
            MessageBox.Show("The following problem occurred when attempting to " & _
                "send your email: " & exp.Message, _
                Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
 
Last edited by a moderator:
I would write a function like this to validate a mail address:
VB.NET:
Private Function IsValidMailAddress(ByVal address As String) As Boolean
    Try
        Dim m As New Net.Mail.MailAddress(address)
        Return True
    Catch ex As Exception
        Return False
    End Try
End Function
then use it to validate the textbox, this way the application user would know and fix recipient(s) before attempting to start sending the mail, notice that by doing this you can notify user specifically which address is erroneous:
VB.NET:
Private Sub tbTo_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles tbTo.Validating
    If tbTo.Text.Trim = "" Then
        e.Cancel = True
        'you could use an ErrorProvider here to notify mandatory field
    Else
        Dim addresses() As String = tbTo.Text.Split(";".ToCharArray, StringSplitOptions.RemoveEmptyEntries)
        For Each address As String In addresses
            If Not IsValidMailAddress(address) Then
                e.Cancel = True
                MsgBox(address & " is a not valid mail address.")
                Exit Sub
            End If
        Next
    End If
End Sub
for sending you can use a method like this that will add all addresses to given mailmessage (knowing by now all addresses are valid):
VB.NET:
Private Sub addRecipients(ByVal m As Net.Mail.MailMessage)
    Dim addresses() As String = tbTo.Text.Split(";".ToCharArray, StringSplitOptions.RemoveEmptyEntries)
    For Each address As String In addresses
        m.To.Add(address)
    Next
End Sub
 
I think either you or me is missing the mark here and I pretty sure it is me. Is there a fully coded working version of email using vs2005? This would allow me to gain knowledge on the entire picture instead of pieces of the puzzle. I don't want free hand outs or to offend anyone as you see I am willing to work but I can only go as far as my brain will take me.
 
In programming you put all the pieces you need together to create different applications. The documentation for System.Net.Mail namespace of the .Net Framework library plus the resource site I linked to should give you more than enough information to enable you to send a mail to multiple recipents. Of course you also need basic VB.Net programming knowledge, most people get this from the first learning book they read on the subject.
 
Ok, I am sure this wasn't the best way but I have 15 hidden textboxes.

If Not String.IsNullOrEmpty(ToAddress2.Text) Then
.To.Add(New MailAddress(ToAddress2.Text.Trim))
End If

If Not String.IsNullOrEmpty(ToAddress3.Text) Then
.To.Add(New MailAddress(ToAddress3.Text.Trim))
End If

So on and so forth. Then I have....

ToAddress2.Text = "fake@email.org"
If CheckBox2.Checked = False Then
ToAddress2.Text = ""
End If

As sloppy as I am sure this is, it is giving me the results I desired. Thank you for all your help.
 
Back
Top