?
Solved

System.Net.Mail problem

Posted on 2006-05-01
34
Medium Priority
?
336 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:dba123
  • 16
  • 10
  • 6
  • +1
34 Comments
 
LVL 15

Expert Comment

by:GavinMannion
ID: 16579570
At a guess I think it is meant to be semi-colon delimited and not comma;

Try changing it and see what happens
0
 
LVL 1

Author Comment

by:dba123
ID: 16579618
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
 
LVL 15

Expert Comment

by:GavinMannion
ID: 16579646
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16579650
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
 
LVL 5

Expert Comment

by:proten
ID: 16579674
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
 
LVL 1

Author Comment

by:dba123
ID: 16579684
message.From.Add(fromAddress)

'Add' is not a member of 'System.Net.Mail.MailAddress'.
0
 
LVL 1

Author Comment

by:dba123
ID: 16579698
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16579716
Sorry, that should have been:

    message.From = New MailAddress(fromAddress)
0
 
LVL 5

Expert Comment

by:proten
ID: 16579732
Just use instead of the previous 3 lines of code:

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

0
 
LVL 1

Author Comment

by:dba123
ID: 16579783
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
 
LVL 5

Expert Comment

by:proten
ID: 16579820
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
 
LVL 5

Expert Comment

by:proten
ID: 16579837
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
 
LVL 1

Author Comment

by:dba123
ID: 16579843
I don't want that, I want to dynamically state the recipients as a function parameter string
0
 
LVL 1

Author Comment

by:dba123
ID: 16579851
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
 
LVL 5

Expert Comment

by:proten
ID: 16579887
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16579888
Put a breakpoint in your code and use the locals window to check the count of addresses in the "To" property.
0
 
LVL 1

Author Comment

by:dba123
ID: 16579893
what's the locals window?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16579915
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
 
LVL 1

Author Comment

by:dba123
ID: 16579919
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
 
LVL 1

Author Comment

by:dba123
ID: 16579931
How do I use that, how do I set that up...the locals window that is
0
 
LVL 1

Author Comment

by:dba123
ID: 16579940
Dim toAddress As New MailAddress(recipients.ToString)

'MailAddress' is a type and cannot be used as an expression.      
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16579945
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16579954
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
 
LVL 1

Author Comment

by:dba123
ID: 16579984
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
 
LVL 1

Author Comment

by:dba123
ID: 16580006
nevermind, just syntax differences from C# to VB carl that was confusing me..
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16580008
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
 
LVL 1

Author Comment

by:dba123
ID: 16580015
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16580016
Oops, sorry forgot it was VB ;o)
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16580050
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
 
LVL 1

Author Comment

by:dba123
ID: 16580086
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16580109
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
 
LVL 5

Accepted Solution

by:
proten earned 2000 total points
ID: 16580137
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
 
LVL 1

Author Comment

by:dba123
ID: 16580192
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
 
LVL 1

Author Comment

by:dba123
ID: 16580262
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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question