System.Net.Mail problem

why is my script not sending an email to everyone in my comma delimited list?  It sends the email only to the first email address I specified (sss1@sss.com), not the rest:
 (p.s. I am using sss for privacy purposes)

 SendEmail("sss Report", "Please click here: <a href=""\\sss\inetpub\wwwroot\sss\ssss\sss\sss"" style=""color: #435838;"">\\ssss\inetpub\wwwroot\sss\ssrs\sss\sss</a> <br><br>sss", "sss Report", "sss1@sss.com, sss2@sss.com, sss3@sss.com, sss4@sss.com")

    Private Sub SendEmail(ByVal message_title As String, ByVal message_body As String, ByVal message_subject As String, ByVal recipients As String)

        Dim fromAddress As New MailAddress("sss@sss.com")
        Dim toAddress As New MailAddress(recipients)
        Dim message As New MailMessage(fromAddress, toAddress)

        Dim mailSender As System.Net.Mail.SmtpClient
        mailSender = New SmtpClient("sss")

        message.Subject = message_subject
        message.IsBodyHtml = True
        message.Body = "<table style=""background-color: #DFE5DC; border= 1px solid #000000; padding: 2px; font-family: arial; color: #000000;"" cellpadding=""0"" celspacing=""0"" width=""100%"">" & _
                       "<tr><td style=""font-weight: bold; background-color: #000000; color: #FFFFFF;""><b>" & message_title & "</b></td></tr>" & _
                       "<tr><td style=""padding: 5px;""><b>" & message_body & "</b></td></tr>" & _
                       "</table>"
        Try
            mailSender.Send(message)
        Catch ex As Exception
            SendPPAEmail("sss Error", "ss Error during Email", ex.ToString, "sss@sss.com")
        End Try

    End Sub
LVL 1
dba123Asked:
Who is Participating?
 
protenCommented:
I am not trying to make you crazy, promise.  I am just keeping an eye on your correspondance.  Did you try my suggestion?  

In the previous post instead of
        Dim message As New MailMessage
        message.From = New MailAddress("sss@sss.com")
        message.To.Add(recipients)

use
      Dim message As New MailMessage("sss@sss.com", recipients)

The rest of the code is the same.
0
 
GavinMannionCommented:
At a guess I think it is meant to be semi-colon delimited and not comma;

Try changing it and see what happens
0
 
dba123Author Commented:
no, I need to use MailAddressCollection but can't get it right in the syntax.  I've tried this but it's not there yet:

        Dim fromAddress As New MailAddress("sss@sss.com")
        Dim toAddress As New MailAddressCollection
        toAddress.Add(recipients)
        Dim message As New MailMessage(fromAddress, toAddress)

        Dim mailSender As System.Net.Mail.SmtpClient
        mailSender = New SmtpClient("sss")

        message.Subject = message_subject
        message.IsBodyHtml = True
        message.Body = "<table style=""background-color: #DFE5DC; border= 1px solid #000000; padding: 2px; font-family: arial; color: #000000;"" cellpadding=""0"" celspacing=""0"" width=""100%"">" & _
                       "<tr><td style=""font-weight: bold; background-color: #000000; color: #FFFFFF;""><b>" & message_title & "</b></td></tr>" & _
                       "<tr><td style=""padding: 5px;""><b>" & message_body & "</b></td></tr>" & _
                       "</table>"
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
GavinMannionCommented:
Try do a simple split on recipients and then add each one individually.

foreach( string s in recipients.split(',') )
{
       toAddress.Add(recipients);
}

That would be the C# pseudo code, hopefully you get the point?
0
 
Carl TawnSystems and Integration DeveloperCommented:
Instead of:

        Dim fromAddress As New MailAddress("sss@sss.com")
        Dim toAddress As New MailAddress(recipients)
        Dim message As New MailMessage(fromAddress, toAddress)

Try:

        Dim fromAddress As New MailAddress("sss@sss.com")
        Dim message As New MailMessage()
        message.From.Add(fromAddress)
        message.To.Add(recipients)
0
 
protenCommented:
I have my code slightly different and it works for me:

Instead Of:
        Dim fromAddress As New MailAddress("sss@sss.com")
        Dim toAddress As New MailAddress(recipients)
        Dim message As New MailMessage(fromAddress, toAddress)

I am using:

Dim msg As New MailMessage("from address as string, "list of recipients as string")

I am pretty sure that when you define a mailaddress it is only one address per variable.
0
 
dba123Author Commented:
message.From.Add(fromAddress)

'Add' is not a member of 'System.Net.Mail.MailAddress'.
0
 
dba123Author Commented:
Yea, I think you're right proten, I think I need to use the MailAddressCollection class but not sure hot to form it right in mysituation

http://msdn2.microsoft.com/en-us/library/system.net.mail.mailaddresscollection(VS.80).aspx
0
 
Carl TawnSystems and Integration DeveloperCommented:
Sorry, that should have been:

    message.From = New MailAddress(fromAddress)
0
 
protenCommented:
Just use instead of the previous 3 lines of code:

Dim msg As New MailMessage("sss@sss.com", recipients)

0
 
dba123Author Commented:
still not working...I have:


 SendEmail("sss Report", "Please click here: <a href=""\\sss\inetpub\wwwroot\sss\ssss\sss\sss"" style=""color: #435838;"">\\ssss\inetpub\wwwroot\sss\ssrs\sss\sss</a> <br><br>sss", "sss Report", "sss1@sss.com, sss2@sss.com, sss3@sss.com, sss4@sss.com")

....
        Dim fromAddress As New MailAddress("sss@sss.com")
        Dim message As New MailMessage()
        message.From = New MailAddress(fromAddress.ToString)


        message.To.Add(recipients.ToString)
        Dim mailSender As System.Net.Mail.SmtpClient
        mailSender = New SmtpClient("sss")

IT sends an email to me (which is the first in the string but not anyone else after the commas).  I also added .ToString to the fromAddress and recipients
0
 
protenCommented:
Use:
       Dim message As New MailMessage("sss@sss.com", "sss1@sss.com, sss2@sss.com")

        Dim mailSender As System.Net.Mail.SmtpClient
        mailSender = New SmtpClient("sss")
etc

0
 
protenCommented:
I have posted the code that I use that is working correctly:

        Dim email As New SmtpClient("mail.server.com")
        Dim msg As New MailMessage("from@somewhere.com", "to1@somewhere.com, to2@somewhere.com"))
        Dim mAttach As Attachment

        msg.Priority = MailPriority.Normal

        msg.Subject = sSubject

        msg.Body = sBody

        msg.IsBodyHtml = True

        mAttach = New Attachment(sAttachment)
        msg.Attachments.Add(mAttach)
 
        email.Send(msg)
0
 
dba123Author Commented:
I don't want that, I want to dynamically state the recipients as a function parameter string
0
 
dba123Author Commented:
that's why I have the string as a function parameter, I do not hard code my recipients in the function itself, it defeats the entire purpose of reuse of a function!
0
 
protenCommented:
Same thing, that was just an example for the code in your function.  I use variables and parameters too:

SendEmail("sss Report", "Please click here: <a href=""\\sss\inetpub\wwwroot\sss\ssss\sss\sss"" style=""color: #435838;"">\\ssss\inetpub\wwwroot\sss\ssrs\sss\sss</a> <br><br>sss", "sss Report", "sss1@sss.com, sss2@sss.com, sss3@sss.com, sss4@sss.com")

    Private Sub SendEmail(ByVal message_title As String, ByVal message_body As String, ByVal message_subject As String, ByVal recipients As String)

       Dim email As New SmtpClient("sss")
       Dim msg As New MailMessage("sss@sss.com", recipients)

        msg.Priority = MailPriority.Normal

        msg.Subject = message_subject
        msg.Body = "<table style=""background-color: #DFE5DC; border= 1px solid #000000; padding: 2px; font-family: arial; color: #000000;"" cellpadding=""0"" celspacing=""0"" width=""100%"">" & _
                       "<tr><td style=""font-weight: bold; background-color: #000000; color: #FFFFFF;""><b>" & message_title & "</b></td></tr>" & _
                       "<tr><td style=""padding: 5px;""><b>" & message_body & "</b></td></tr>" & _
                       "</table>"
       

        msg.IsBodyHtml = True

        Try
                    email.Send(msg)
        Catch ex As Exception
            SendPPAEmail("sss Error", "ss Error during Email", ex.ToString, "sss@sss.com")
        End Try

    End Sub
0
 
Carl TawnSystems and Integration DeveloperCommented:
Put a breakpoint in your code and use the locals window to check the count of addresses in the "To" property.
0
 
dba123Author Commented:
what's the locals window?
0
 
Carl TawnSystems and Integration DeveloperCommented:
Its one of the debug windows that allows you to interogate the values of the objects in your code. Either that or just hover the cursor over the "message" variable and use the popup view to view the objects data.
0
 
dba123Author Commented:
it's weird if I do this

MsgBox(recipients)

it returns the first and third address and then after I close that prompte I get another msg showing the first only again
0
 
dba123Author Commented:
How do I use that, how do I set that up...the locals window that is
0
 
dba123Author Commented:
Dim toAddress As New MailAddress(recipients.ToString)

'MailAddress' is a type and cannot be used as an expression.      
0
 
Carl TawnSystems and Integration DeveloperCommented:
The locals window should be available automatically when you run your app under the debugger. Assuming you are using Visual Studio; look at the bottom left of the IDE when you run your app under the debugger. There should be a tabe named "Locals"
0
 
Carl TawnSystems and Integration DeveloperCommented:
Heres the sample i've been using:

    private void SendMail(string recipients)
    {
        System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
        msg.From = new System.Net.Mail.MailAddress("test@test.com");
        msg.To.Add(recipients);

        System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.server.name");
        smtp.Send(msg);
    }
0
 
dba123Author Commented:
carl you aren't using .net 2.0 though....are you?  I am not using the smtp option.  I do it this way          mailSender = New SmtpClient("servername")
0
 
dba123Author Commented:
nevermind, just syntax differences from C# to VB carl that was confusing me..
0
 
Carl TawnSystems and Integration DeveloperCommented:
I am using 2.0.

This:

    System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.server.name");

Is the same as your:

    mailSender = New SmtpClient("servername")

Its just that I have explicitly referenced the namespaces. Without the namespaces it becomes:

    SmtpClient smtp = new SmtpClient("smtp.server.name");
0
 
dba123Author Commented:
It doesn't like this syntax either.

        Dim message As New MailMessage
        message.From.Address("sss@sss.com")
        message.To.Add(recipients)

        Dim mailSender As System.Net.Mail.SmtpClient
        mailSender = New SmtpClient("servername")
0
 
Carl TawnSystems and Integration DeveloperCommented:
Oops, sorry forgot it was VB ;o)
0
 
Carl TawnSystems and Integration DeveloperCommented:
My sample, in the correct language this time:


    Private Sub SendMail(ByVal recipients As String)

        Dim msg As New MailMessage
        msg.From = New MailAddress("test@test.com")
        msg.To.Add(recipients)

        Dim smtp As New SmtpClient("smtp.server.name")
        smtp.Send(msg)

    End Sub
0
 
dba123Author Commented:
This doesn't work.  Still it sends only me an email and ignores the rest of the comma delimited recipient string:


    Private Sub SendEmail(ByVal message_title As String, ByVal message_body As String, ByVal message_subject As String, ByVal recipients As String)

        Dim message As New MailMessage
        message.From = New MailAddress("sss@sss.com")
        message.To.Add(recipients)

        Dim mailSender As System.Net.Mail.SmtpClient
        mailSender = New SmtpClient("sss")

        message.Subject = message_subject
        message.IsBodyHtml = True
        message.Body = "<table style=""background-color: #DFE5DC; border= 1px solid #000000; padding: 2px; font-family: arial; color: #000000;"" cellpadding=""0"" celspacing=""0"" width=""100%"">" & _
                       "<tr><td style=""font-weight: bold; background-color: #000000; color: #FFFFFF;""><b>" & message_title & "</b></td></tr>" & _
                       "<tr><td style=""padding: 5px;""><b>" & message_body & "</b></td></tr>" & _
                       "</table>"
        Try
            mailSender.Send(message)
        Catch ex As Exception
            SendEmail("sss", "sss", ex.ToString, "sss@sss.com")
        End Try

    End Sub
0
 
Carl TawnSystems and Integration DeveloperCommented:
Did you say that you got your messagebox twice ? That might suggest that its throwing an exception since you recall your sub from the Catch block.

Try putting a break point on the line:

    SendEmail("sss", "sss", ex.ToString, "sss@sss.com")


If it gets there, then see what ex.ToString says.
0
 
dba123Author Commented:
well no, but after I state why none of this sh$$ is working you may feel that I'm driving you nuts.   What was happening is that all this time, it was working!...at lease some of your recommondations...just that I was running a copy of my exe from my console app which happened to be an older compiled version....therefore I was not running it from the bin direcgtory or shortcut from there whdich is why it wasn't running my most updated code!

ahhhhh!

thanks all!!
0
 
dba123Author Commented:
Ok, now after testing the right exe, this was actually the one that worked:

 

  Private Sub SendEmail(ByVal message_title As String, ByVal message_body As String, ByVal message_subject As String, ByVal recipients As String)

        Dim message As New MailMessage
        message.From = New MailAddress("sss@sss.com")
        message.To.Add(recipients)

        Dim mailSender As System.Net.Mail.SmtpClient
        mailSender = New SmtpClient("sss")

        message.Subject = message_subject
        message.IsBodyHtml = True
        message.Body = "<table style=""background-color: #DFE5DC; border= 1px solid #000000; padding: 2px; font-family: arial; color: #000000;"" cellpadding=""0"" celspacing=""0"" width=""100%"">" & _
                       "<tr><td style=""font-weight: bold; background-color: #000000; color: #FFFFFF;""><b>" & message_title & "</b></td></tr>" & _
                       "<tr><td style=""padding: 5px;""><b>" & message_body & "</b></td></tr>" & _
                       "</table>"
        Try
            mailSender.Send(message)
        Catch ex As Exception
            SendEmail("sss", "sss", ex.ToString, "sss@sss.com")
        End Try

    End Sub
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.