Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem sending duplicate emails from System.Net.Mail

Status
Not open for further replies.

ShonGill

Programmer
May 20, 2008
44
US
Hey guys (& gals),

I'm having a small and very annoying problem with emails sent from the System.Net.Mail.SMTPClient object on one of my pages. I have a very simple script that sends an email when the contact page is filled out and submitted. Some validation for postback not much else (all is working so far).

Here's the problem; When the form is filled out and submitted, an email is sent to the correct email address with all the fields filled out. Also, a second email is sent at that exact moment that has with blank fields. The problem is the second email. It should not be sent out, and the form is not even filled out in that email.

I set my SMTP details in my web.config file, not the page coding. The hosting is not set to forward emails. The page is set to send directly to the receiving email address and use the global configuration to find the SMTP server.

It's a basic problem, probably a very simple solution. Any ideas?
 
I'd start by debugging your way through the code and making sure that you're certain it is an ASP.NET problem. If it is, show us the code that is causing the second email to be sent.


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
Good thinking. I haven't added any debugging yet. I'm not sure what I would be looking for specifically. Like I said, the code is already very simple. I just started building the page. I haven't even added login/logout which I was planning to do next. Thanks, I'll post the code later when I'm in front of it. Just need an idea of where to start looking.
 
If it is, show us the code that is causing the second email to be sent.

So, here is the code. The first is used in my global config file. I think I just now see a problem calling the ctPgEmail to pull the info from the submitted form, and then declaring the SMTPClient to send the email. Is there a problem with doing it this way? Should I just declare either a SMTPClient or MailMessage to send, and not both?

Code:
  <system.net>
    <mailSettings>
      <smtp from="example1">
        <network host="example2" password="example3" port="example4"
          userName="example5" />
      </smtp>
    </mailSettings>   
  </system.net>

Code:
    Protected Sub sendEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submitButton.Click

        Dim ctPgFrom As New Net.Mail.MailAddress("example1")
        Dim ctPgTo As New Net.Mail.MailAddress("example6")
        Dim ctPgBCC As New Net.Mail.MailAddress("example6")

        Dim ctPgEmail As New Net.Mail.MailMessage(ctPgFrom, ctPgTo)

        Dim sMsgBody As String

        ctPgEmail.Bcc.Add(ctPgBCC)

        sMsgBody = ctFirstNameField.Text & " " & ctLastNameField.Text & " sent a contact request from the Contact Us page at OURWEBSITE.INFO" & vbCrLf & vbCrLf & "See below for contact information:" & vbCrLf
        sMsgBody += "First Name: " & ctFirstNameField.Text & vbCrLf
        sMsgBody += "Last Name: " & ctLastNameField.Text & vbCrLf
        sMsgBody += "Street Address: " & ctAddressField1.Text & ", " & ctAddressField2.Text & vbCrLf
        sMsgBody += "City: " & ctCityField.Text & ", State: " & ctStateField.Text & ", Zip Code: " & ctZipField.Text & vbCrLf
        sMsgBody += "Home Phone: " & ctPhoneField1.Text & vbCrLf
        sMsgBody += "Alt Phone: " & ctPhoneField2.Text & vbCrLf
        sMsgBody += "Email: " & ctEmailField.Text & vbCrLf
        sMsgBody += "Comments: " & ctCommentsField.Text & vbCrLf
        ctPgEmail.Subject = "You have a new contact request!"
        ctPgEmail.Body = sMsgBody
        ctPgEmail.IsBodyHtml = False

        Dim smtp As New Net.Mail.SmtpClient

        smtp.Send(ctPgEmail)

        doStatus()
    End Sub
 
the event may be firing twice. what does doStatus() do?

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
also i don't see where the client side is validating before sending the email.

it should looks something like this
Code:
if(!IsValid) return;
or
Code:
if(!IsValid) 
{
  //create message and send email
}



Jason Meckley
Programmer
Specialty Bakers, Inc.
 
JMeckley,

The 'doStatus()' just returns a message on the page that the email was sent. It's very simple. Then it clears all form fields.

I have validation on the page which is locked into the postback. The form will not submit until the validation requirements are met.

Mark, I noticed that the sample code you have posted on your site uses NetworkCredentials class on the page. Is it better to use?
 
I'm not sure if it's better as such, but it does give you more control when debugging to make sure everything is correct. You could try copying that function into your application and just making a call to it when you want to send an email.


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
That's what I was trying to do with the what I have. There is only one SMTP server. 'Net.Mail' is still pretty new to me. What would be the best way to go about debugging?
 
What would be the best way to go about debugging?
F5, some relevant breakpoints and F11 to step through a line at a time (if you are using Visual Studio).


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
Nope. Actually, I'm using Visual Web Developer. I think I need to add trace into the page before I can start debugging anything. I'll try what you recommend and post whether it works or not.
 
This issue was resolved with the code found below. Thanks to Mark and Jason for the help. I had to do a little research on the System.Net.Mail namespace and the SMTP Credentials class, and found out that it can be either programmaticly coded on the page or to the web application, not both. Something I ran into suggested that anything that is not in the web application config files would be picked up from the page... WRONG! It's one or the other.

So I wanted to post what I found. Here it is. Thanks again guys.

Code:
        Dim smtp As New SmtpClient("127.0.0.1")
        Dim NetCred As New NetworkCredential

        NetCred.GetCredential("HOST.DOMAIN.COM", Port Integer, "AUTHENTICATION TYPE")
        NetCred.UserName = "USERNAME"
        NetCred.Password = "PASSWORD"

        smtp.Credentials = NetCred


Shon
If it gets any easier.. something is wrong.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top