Link to home
Start Free TrialLog in
Avatar of dba123
dba123

asked on

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
Avatar of GavinMannion
GavinMannion

At a guess I think it is meant to be semi-colon delimited and not comma;

Try changing it and see what happens
Avatar of dba123

ASKER

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>"
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?
Avatar of Carl Tawn
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)
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.
Avatar of dba123

ASKER

message.From.Add(fromAddress)

'Add' is not a member of 'System.Net.Mail.MailAddress'.
Avatar of dba123

ASKER

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
Sorry, that should have been:

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

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

Avatar of dba123

ASKER

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
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

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)
Avatar of dba123

ASKER

I don't want that, I want to dynamically state the recipients as a function parameter string
Avatar of dba123

ASKER

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!
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
Put a breakpoint in your code and use the locals window to check the count of addresses in the "To" property.
Avatar of dba123

ASKER

what's the locals window?
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.
Avatar of dba123

ASKER

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
Avatar of dba123

ASKER

How do I use that, how do I set that up...the locals window that is
Avatar of dba123

ASKER

Dim toAddress As New MailAddress(recipients.ToString)

'MailAddress' is a type and cannot be used as an expression.      
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"
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);
    }
Avatar of dba123

ASKER

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")
Avatar of dba123

ASKER

nevermind, just syntax differences from C# to VB carl that was confusing me..
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");
Avatar of dba123

ASKER

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")
Oops, sorry forgot it was VB ;o)
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
Avatar of dba123

ASKER

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
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.
ASKER CERTIFIED SOLUTION
Avatar of proten
proten

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dba123

ASKER

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!!
Avatar of dba123

ASKER

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