I am working on a C# Web Forms app, and I am having trouble with SMTP. The code executes without errors, but the email doesn't send either. Figuring I should keep it simple, I wrote a page that has a textbox and button for sending a test email, along with a label for displaying a result. The code-behind looks like so:
protected void btnGo_Click ( object sender, EventArgs e ) {
var msg = new MailMessage();
msg.To.Add ( "tampasportslover@gmail.com" );
msg.From = new MailAddress ( "someemail@address.com","Email Test" );
msg.IsBodyHtml = true;
msg.Subject = "test";
msg.Body = tbMessage.Text;
msg.Priority = MailPriority.High;
var smtp = new SmtpClient();
smtp.SendCompleted += new SendCompletedEventHandler ( smtp_SendCompleted );
smtp.Send ( msg );
lblStatus.Text = "Message Sent!";
}
void smtp_SendCompleted ( object sender, System.ComponentModel.AsyncCompletedEventArgs e ) {
if ( e.Cancelled == true || e.Error != null ) {
throw new Exception ( e.Cancelled ? "EMail sedning was canceled." : "Error: " + e.Error.ToString ( ) );
}
}
The "SendCompleted" event never fires (I put a breakpoint on it to test and it is never hit), but the button code completes and displays the "Message Sent!" message. This is failing silently, and I don't know why. The network credentials are stored in the web.config file, by the way. I know the app is communicating with the email server, because if I change the username or password for the credentials to a bad value, I get an error message back. But if the credentials are correct, the code runs and the email never gets sent. Any ideas?