PDA

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


tatton777
04-07-2006, 03:02 AM
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(postmaster@myschoolfees.com,
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 = "validemailaddress@email.com";
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.";
}

bruce
04-07-2006, 06:08 AM
do you get any error? or email just doesn't get deliever.

Bruce

DiscountASP.NET
www.DiscountASP.NET (http://www.DiscountASP.NET)

tatton777
04-08-2006, 07:13 AM
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.