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\wwwro ot\sss\sss s\sss\sss" " style=""color: #435838;"">\\ssss\inetpub\ wwwroot\ss s\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
(p.s. I am using sss for privacy purposes)
SendEmail("sss Report", "Please click here: <a href=""\\sss\inetpub\wwwro
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
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>"
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?
foreach( string s in recipients.split(',') )
{
toAddress.Add(recipients);
}
That would be the C# pseudo code, hopefully you get the point?
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(fromAddre ss)
message.To.Add(recipients)
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(fromAddre
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.
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.
ASKER
message.From.Add(fromAddre ss)
'Add' is not a member of 'System.Net.Mail.MailAddre ss'.
'Add' is not a member of 'System.Net.Mail.MailAddre
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
http://msdn2.microsoft.com/en-us/library/system.net.mail.mailaddresscollection(VS.80).aspx
Sorry, that should have been:
message.From = New MailAddress(fromAddress)
message.From = New MailAddress(fromAddress)
Just use instead of the previous 3 lines of code:
Dim msg As New MailMessage("sss@sss.com", recipients)
Dim msg As New MailMessage("sss@sss.com",
ASKER
still not working...I have:
SendEmail("sss Report", "Please click here: <a href=""\\sss\inetpub\wwwro ot\sss\sss s\sss\sss" " style=""color: #435838;"">\\ssss\inetpub\ wwwroot\ss s\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.To String)
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
SendEmail("sss Report", "Please click here: <a href=""\\sss\inetpub\wwwro
....
Dim fromAddress As New MailAddress("sss@sss.com")
Dim message As New MailMessage()
message.From = New MailAddress(fromAddress.To
message.To.Add(recipients.
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
Dim message As New MailMessage("sss@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.co m")
Dim msg As New MailMessage("from@somewher e.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(mAttac h)
email.Send(msg)
Dim email As New SmtpClient("mail.server.co
Dim msg As New MailMessage("from@somewher
Dim mAttach As Attachment
msg.Priority = MailPriority.Normal
msg.Subject = sSubject
msg.Body = sBody
msg.IsBodyHtml = True
mAttach = New Attachment(sAttachment)
msg.Attachments.Add(mAttac
email.Send(msg)
ASKER
I don't want that, I want to dynamically state the recipients as a function parameter string
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\wwwro ot\sss\sss s\sss\sss" " style=""color: #435838;"">\\ssss\inetpub\ wwwroot\ss s\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
SendEmail("sss Report", "Please click here: <a href=""\\sss\inetpub\wwwro
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",
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.
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.
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
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
ASKER
How do I use that, how do I set that up...the locals window that is
ASKER
Dim toAddress As New MailAddress(recipients.ToS tring)
'MailAddress' is a type and cannot be used as an expression.
'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.MailMessag e msg = new System.Net.Mail.MailMessag e();
msg.From = new System.Net.Mail.MailAddres s("test@te st.com");
msg.To.Add(recipients);
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient ("smtp.ser ver.name") ;
smtp.Send(msg);
}
private void SendMail(string recipients)
{
System.Net.Mail.MailMessag
msg.From = new System.Net.Mail.MailAddres
msg.To.Add(recipients);
System.Net.Mail.SmtpClient
smtp.Send(msg);
}
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")
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.ser ver.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.na me");
This:
System.Net.Mail.SmtpClient
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.na
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")
Dim message As New MailMessage
message.From.Address("sss@
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.na me")
smtp.Send(msg)
End Sub
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.na
smtp.Send(msg)
End Sub
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
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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!!
ahhhhh!
thanks all!!
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
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
Try changing it and see what happens