Posted By: System | Aug 14th, 2007 @ 9:08 AM
page 1 of 1
Comments: 6 | Views: 2395

I have a problem with sending an email from my web app. It seems to send two copies of the mail and I am not sure why.

I am using:

Protected Sub Wizard1_FinishButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles Wizard1.FinishButtonClick

SendMail("Senders Email Address", "Body")

End Sub

 

Private Sub SendMail(ByVal from As String, ByVal body As String)

Dim mailServerName As String = "smtp.mail.yahoo.co.uk"

Dim message As MailMessage = New MailMessage(from, "Email Address", "Subject", body)

Dim mailClient As SmtpClient = New SmtpClient

mailClient.Host = mailServerName

mailClient.Send(message)

message.Dispose()

End Sub


What I cant seem to understand is why would SendMail get called twice when the Finish Button on the ASP Wizard is only pressed once?

Does this only happen in Debug? Other alternatives - is Autopostback set to true?

No it happens during debug and when I deploy it to my webserver.

As for autopostback, I didnt even know the wizard control had an autopostback property? I'll have a look - but if it does, I do not remeber setting it to true.

littleguru
littleguru
<3 Seattle
Set a breakpoint in the method. See when it got hit. Look at the stack trace (there is a debug window that shows the stack trace, found in the debug menu of VS) and you should know what happens.

Well thats the thing guru; I did set breakpoints - specificly on the SendMail Method and on mailClient.Send.

It hits the SendMail call and the stack trace shows what I expect to see:

App_Web_zohyct4o.dll!Trade.Wizard1_FinishButtonClick(Object sender = {System.Web.UI.WebControls.Wizard}, System.Web.UI.WebControls.WizardNavigationEventArgs e = {System.Web.UI.WebControls.WizardNavigationEventArgs}) Line 14 Basic

It then continues to send the message and on mailClient.Send it breaks again and the stack trace shows:

App_Web_vyfvxpgc.dll!Trade.SendMail(String from = "emailAddress", String body = "body") Line 33 Basic
  App_Web_vyfvxpgc.dll!Trade.Wizard1_FinishButtonClick(Object sender = {System.Web.UI.WebControls.Wizard}, System.Web.UI.WebControls.WizardNavigationEventArgs e = {System.Web.UI.WebControls.WizardNavigationEventArgs}) Line 15 + 0x293 bytes Basic

Again, everything looks ok to me. But then, it calls SendMail again and the process repeats - including identicle stack traces.

jsampsonPC
jsampsonPC
SampsonBlog.com SampsonVideos.com
Have you tried raising a message box or something just before it sends; check to see if you get two of those along with two emails?
Whay are you explicitly calling dispose?

One another note, interesting convention MS is using for MSDN sample these days aye/

I found this comment in some MSDN code will looking up big fellas problem.

MSDN Sample wrote:


   // what about the namespace for ContentDisposition ?
  // 1) of no consequence to this example 
// 2) google, do you use it?
  // 3) take questions to forums or newsgroups



Nice attitude...

http://msdn2.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx
page 1 of 1
Comments: 6 | Views: 2395
Microsoft Communities