Thursday, March 22, 2012

SmtpMail stalling

My problem is that if I appempt to send multiple emails, the program stalls out at the SmtpMailSend(msg) line. There is additional code after this loop that does not execute, Not in Debug or Release.

In this example, I pull some information from a database and attempt to email
each record. No error messages as it would have been caught by the error handler. I'm wondering if I'm pushing the limitations of the SMTP server or if I'm missing something.

Any help or suggestions would be most appreciated!


Dim x
For x = 0 To tInfo.Rows.Count - 1
Dim msg As New MailMessage()
'Set To property
msg.To = em.Rows(0)("Email").ToString()

'Set From property
msg.From = "myemailaddress"

'Set Subject line
msg.Subject = "Ticket: " & tInfo.Rows(x)("Ticketnumber")

'Create body of message
msg.Body = "Email Example"

'Set smtp server
SmtpMail.SmtpServer = "mysmtpserver"

Try
'Send message
SmtpMail.Send(msg)
msg = Nothing
Catch ex As Exception
lblStatusMessages.Text = ex.Message
End Try
Next

Is it working with just 1 email address (i.e. just going through the loop once)?

It could be that it's not stalling, as such, it's just taking a long time, particularly if you've got a lot of records.
Thanks for the reply,

That's what I thought at first. In my test runs there are a total of 5 records to do. I can get as many as 3 emails sent then the routine just stops executing. I know it sound odd but
it never gets though the entire loop.

When the routine first starts, I disable the button and at the end I re-enable it. The button never re-enables. When I run it in debug mode and step through it I get into the 3rd loop, Hit the send method line and it just quits and sends me back to the program.

Here is the entire routine:


Private Sub btnEmail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmail.Click

btnEmail.Enabled = False
lblStatusMessages.Text = "This may take a minute or two depending on the amount of tickets to send."

' Don't proceed if no date specified
If txtbxDate.Text = "" Then
txtbxDate.Text = "MM/DD/YY"
txtbxDate.Focus()
Exit Sub
End If

Dim db As New DataWork()
Dim tInfo As DataTable

Try
tInfo = db.GetMessageEmailInfo(cmbxForeman.SelectedItem, txtbxDate.Text)
Catch ex As Exception
Throw New Exception(ex.Message)
Finally

End Try

If Not tInfo Is Nothing And tInfo.Rows.Count > 0 Then
Dim em As DataTable
Try
em = db.GetForemanEmail(cmbxForeman.SelectedItem)
Catch ex As Exception
Throw New Exception(ex.Message)
End Try

Dim x
For x = 0 To tInfo.Rows.Count - 1
Dim msg As New MailMessage()
'Set To property
msg.To = em.Rows(0)("Email").ToString()

'Set From property
msg.From = "myemailaddress"

'Set Subject line
msg.Subject = "Ticket: " & tInfo.Rows(x)("Ticketnumber")

'Create body of message
msg.Body = "my information"

'Set smtp server
SmtpMail.SmtpServer = "mysmtpserver"

Try
'Send message
SmtpMail.Send(msg)
msg = Nothing
Catch ex As Exception
lblStatusMessages.Text = ex.Message
End Try
Next

'Send status message
lblStatusMessages.Text = "Email for: " & _
cmbxForeman.SelectedItem & " sent to: " & em.Rows(0)("Email")

'Release objects
em = Nothing
tInfo = Nothing
db = Nothing
Else
lblStatusMessages.Text = "No tickets assigned to: " & cmbxForeman.SelectedItem & " for the date of: " & txtbxDate.Text
End If

btnEmail.Enabled = True
End Sub

Thanks.
the error was caused by the order in which i wrote the parameters down, which was not in synch to when i called them in my assembly. weird, didn't know that it mattered, but i guess it does. live and learn.
Are you suggesting to re-structure the routine?

If only 1 record needs to be sent, it works the way it should and completes the routine.

I'm not entire familure with the SmtpMail.Send method. When called does it create a connection to the server for each call. If that is the case, would it be safe to assume that I'm maxing out the number of connections allowed to the server? Or am I just parranoid and impatient. Maybe the delay or "stall out" is a result of the messaging system being clogged.

Is there a way to batch email? Such as create an array of MailMessage's then loop through the array.

Is there another method of sending an email other than using SmtpMail? Maybe there's an article on sending commands to an SMTP server.
There's an articlehere about using .NET to query a POP3 server - it'll be a very similar story, I imagine, when contacting an SMTP server. I tink you'll be able to do it the same way.

I must admit, I had a similar issue with some VBA code that I wrote recently - that had to send a bunch of emails too. That would also do just so many and then crap out.

Are you using an external SMTP server? Or is it internal to your organisation. I only ask because I think a lot of commercial SMTP suppliers are cracking down on spammers, and prt of this is making their servers a bit edgy when it gets a whole load of emails from 1 machine in quick succession (i.e. as if that machine's been infected with a nasty), and so locks down the service for a time. I think that's what was happening to mine. Don't know if the same's happening to yours?

Let me know. Sounds interesting...
It's funny that you mention Pop3 because another part of my program connects to a pop3 server and does retreive and delete emails. I can get over 500 messages this way with out any issues but time of course - but the code continues to run.

I think your right about exteral servers cracking down, But the real question is - What is the work around? In today's electronic communication - Email is in part a very big slice of the pie and many companies need to deliver multiple emails for the purpose of good, of course.

I'm a life time SPAM hater.

Anyway - If I find an article on connecting, sending and disconnecting to an SMTP server
I'll post it.

From all of the research I've done, It's obvious that this problem is not unique to myself as lots of ppl are having similar issues and maybe it's time for Microsoft to deploy a rock solid solution.

Bill Gates - Do you have any comments on this???
Depending on your network and your machine, you could send all the mail from your local machine. I'm not sure about other versions of Windows, but WinXP Pro comes includes MS's SMTP Server, which you can use, rather than a remote server. The big bonus being that you can configure it how you like.

Can't say I know that much about using it, I just know that you can. If you're forced to use a commercial remote SMTP server, then s be it, but the option's there.

As to providing a solution that stops spammers, but lets legitimate mass mail through, it's an impossible thing to do. Not only is there the question of working around the system (no matter what credentials you want from a legitimate mailer, a spammer can forge them), there's also a slightly burrier issue of What constitutes spam. It's not an argument I really want to go into (apart from anything else it's wildly off topic), but one man's targeted email marketing is another man's spam.

Just a thought.

0 comments:

Post a Comment