I have some items in a listbox and a try catch. I need it where if the program catches an error with the current listbox item to move on to the next one.
Does any one now if this is possible
Printable View
I have some items in a listbox and a try catch. I need it where if the program catches an error with the current listbox item to move on to the next one.
Does any one now if this is possible
what do you mean by catches an error?
Increase the SelectedIndex property by 1 in the Catch clause of the Try-Catch.
Be careful not to go beyond the max index.
Ok so my program uses email and I have 2 or 3 email accounts in a listbox
if one does not work it moves on to the next
here is the code but i cant get it to workCode:Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim strSplit() As String
Dim strResult() As String
'Sets server and port for smtp server
Dim Smtp As New System.Net.Mail.SmtpClient("smtp.gmail.com", "587")
'Enables SS1 Encryption.
Smtp.EnableSsl = True
Me.ListBox1.SelectedIndex = 0
Try
Try
strSplit = Split(ListBox1.SelectedIndex, ":")
'Sets username and password for account
Smtp.Credentials = New System.Net.NetworkCredential(strSplit(0), strSplit(1))
MsgBox("worked")
Catch ex As Exception
ListBox1.SelectedIndex = ListBox1.SelectedIndex + 1
End Try
Catch ex As Exception
End
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.ListBox1.Items.Add(Me.TextBox1.Text)
End Sub
End Class
What exactly, is not working? And why do you have two nested Try-Catch blocks?
ok i got it working but i have to click the button each time how do i get it to cycle through them?
Code:Dim strSplit() As String
Dim strResult() As String
'Sets server and port for smtp server
Dim Smtp As New System.Net.Mail.SmtpClient("smtp.gmail.com", "587")
Dim Message = New System.Net.Mail.MailMessage("[email protected]", "[email protected]", "worked", "worked")
'Enables SS1 Encryption.
Smtp.EnableSsl = True
Me.ListBox1.SelectedItem = 0
Try
Try
strSplit = Split(ListBox1.SelectedItem, ":")
'Sets username and password for account
Smtp.Credentials = New System.Net.NetworkCredential(strSplit(0), strSplit(1))
Smtp.Send(Message)
Catch ex As Exception
MsgBox("invalid")
ListBox1.SelectedIndex = ListBox1.SelectedItem + 1
End Try
Catch ex As Exception
End
End Try
You'd use a loop.
try this:
vb Code:
'Sets server and port for smtp server Dim Smtp As New System.Net.Mail.SmtpClient("smtp.gmail.com", 587) Dim Message As New System.Net.Mail.MailMessage("[email protected]", "[email protected]", "worked", "worked") 'Enables SS1 Encryption. Smtp.EnableSsl = True For Each Item As String In ListBox1.Items Try Dim strSplit() As String = Split(Item, ":") 'Sets username and password for account Smtp.Credentials = New System.Net.NetworkCredential(strSplit(0), strSplit(1)) Smtp.Send(Message) Catch ex As Exception MsgBox("invalid") End Try Next
that works, but doesnt solve the original problem which is. The program uses the current email until it catches an error and when it catches an error it moves on to the next, I've searched for a long time trying to find this solution
Just to clarify, I have a couple of questions. Do you want to catch the error and then examine it later, or just catch it and move on so that your application doesn't lock up?Quote:
Originally Posted by Tddupre
Just to move on so like:
Program Is Sending Email and If for some reason the next email it tries to send the email/password it is using doesn't work it uses the next email/password in the list.
Oh. Okay. Well, if you just wanted it to move on, then you wouldn't need to put anything after the catch line. The application would catch the error, throw it away and then move on to the next section of code.
In your case, I thought moving to the next line in the ListBox would work. Though, if you go to the next line in the catch part of the try statement, it shouldn't work.
Doing it that way, says for the application to do the loop, if it errors, move the focus to the next line, but not continue the loop. If I'm not mistaken, that is.
You'd probably have to resubmit the loop on error. Or, if there is a "fails" method to System.Net.Mail namespace, then you could use conditional logic to check the state of the message that way.
I'll look it up in MSDN and see what it says.
OK, I understand that, but I know no other way of moving to the next email/password then continue sending the email. I have searched everything that i could think of
I can't imagine that there wouldn't be something in the System.Net.Mail namespace that verifies if a mail message has been sent.
I haven't got a chance to research and verify it, but from a quick search on MSDN, I found a SmtpFailedRecipientException Class.
MSDN explains the class in this way:This sounds exactly like what you're looking for. I would suggest you do some research into this class and how to utilize it.Quote:
Originally Posted by MSDN
i still don't understand:confused:
the code i posted will loop through each item in the listbox, + try to send your message. if theres an error it pops up a msgbox, then moves on to the next listbox item.
i can't see how that isn't doing what you requested?
The problem with that code paul is that it will send the email from each email/password in the list
i found this on msdn:
vb Code:
Imports System Imports System.Net Imports System.Net.Mail Imports System.Net.Mime Imports System.Threading Imports System.ComponentModel Namespace Examples.SmptExamples.Async Public Class SimpleAsynchronousExample Shared mailSent As Boolean = False Private Shared Sub SendCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) ' Get the unique identifier for this asynchronous operation. Dim token As String = DirectCast(e.UserState, String) If e.Cancelled Then Console.WriteLine("[{0}] Send canceled.", token) End If If e.Error IsNot Nothing Then Console.WriteLine("[{0}] {1}", token, e.Error.ToString()) Else Console.WriteLine("Message sent.") End If mailSent = True End Sub Public Shared Sub Main(ByVal args As String()) ' Command line argument must the the SMTP host. Dim client As New SmtpClient(args(0)) ' Specify the e-mail sender. ' Create a mailing address that includes a UTF8 character ' in the display name. Dim from As New MailAddress("[email protected]", "Jane " & CChar(&Hd8) & " Clayton", System.Text.Encoding.UTF8) ' Set destinations for the e-mail message. Dim to As New MailAddress("[email protected]") ' Specify the message content. Dim message As New MailMessage(from, to) message.Body = "This is a test e-mail message sent by an application. " ' Include some non-ASCII characters in body and subject. Dim someArrows As New String(New Char() {"←"c, "↑"c, "→"c, "↓"c}) message.Body += Environment.NewLine + someArrows message.BodyEncoding = System.Text.Encoding.UTF8 message.Subject = "test message 1" & someArrows message.SubjectEncoding = System.Text.Encoding.UTF8 ' Set the method that is called back when the send operation ends. AddHandler client.SendCompleted, AddressOf SendCompletedCallback ' The userState can be any object that allows your callback ' method to identify this send operation. ' For this example, the userToken is a string constant. Dim userState As String = "test message1" client.SendAsync(message, userState) Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit.") Dim answer As String = Console.ReadLine() ' If the user canceled the send, and mail hasn't been sent yet, ' then cancel the pending operation. If answer.StartsWith("c") AndAlso mailSent = False Then client.SendAsyncCancel() End If ' Clean up. message.Dispose() Console.WriteLine("Goodbye.") End Sub End Class End Namespace