Two sets of code for sending mail, logic identical, one works, one fails... HELP!!

Discussion in 'ASP.NET 2.0' started by tatton777, Apr 7, 2006.

  1. I've got threepages on my website that send email, two are working great. The third isn't. I cut and past the code from the first example that works, as a model for the second, that doesn't send me an email. Can anyone spot where I'm goofing up. If it makes a difference, the first set of code is in a Page_Load() event, the second is in a Button_Click() event. I've include System.Net.Mail in both classes, just so we can get that out of the way.

    First Set of Code that works:



    try
    {
    string zBody = "User:" + zEmail + " Password:" + zPassword;
    MailMessage mailMessage = new MailMessage([email protected],
    zEmail, "Your MySchoolFees Password", zBody);
    SmtpClient client = new SmtpClient("localhost");
    client.Send(mailMessage);
    MailStatusLabel.Visible = true;
    MailStatusLabel.Text = "Your password has been sent to " + zEmail;
    }
    catch (Exception ex)
    {
    MailStatusLabel.Visible = true;
    MailStatusLabel.Text = "Failed to send message!! Error: " + ex.Message;
    }





    Second set of code that doesn't return an error, but I don't get an email:


    string zFrom = txtUserName.Text;
    string zTo = "[email protected]";
    string zSubject = "Request for Security Level Upgrade";


    try
    {
    string zBody = "REQUEST FOR SECRETARIAL SECURITY LEVEL UPGRADE : UserName: " +
    txtUserName.Text + " SchoolID: " + txtSchoolID.Text ;
    MailMessage mailMessage = new MailMessage(zFrom, zTo, zSubject, zBody);
    SmtpClient client = new SmtpClient("localhost");
    client.Send(mailMessage);
    MessageLabel.Visible = true;
    MessageLabel.Text = "Your request has been sent. You will receive a response " +
    "within 24 hours. PLEASE: Close this window to continue.";
    }
    catch (Exception ex)
    {
    MessageLabel.Visible = true;
    MessageLabel.Text = "Failed to send message!! Error: " + ex.Message + "." +
    "Please contact tech support at ***-***-****. " +
    "PLEASE: Close this window to continue.";
    }
     
  2. Bruce

    Bruce DiscountASP.NET Staff

    do you get any error? or email just doesn't get deliever.

    Bruce

    DiscountASP.NET
    www.DiscountASP.NET
     
  3. Instead of just copying and pasting the code. I rewrote the routine and renamed the variables. Now it works fine. Something must have gone wrong in the method or variable registration within VS2005.
     

Share This Page