0

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?

TC_Guy
  • 127
  • 9
  • check comments, NOT php code. https://stackoverflow.com/questions/13798988/mail-function-not-working-for-personal-domain#comment18981099_13798988 – Mate Nov 10 '22 at 04:04
  • You didn't read the documentation. The `SendCompleted` event is specifically raised "when an asynchronous email send operation completes". You are not performing an asynchronous send so you should expect no event raised. – jmcilhinney Nov 10 '22 at 04:07
  • try smt.SendAsync() instead of smtp.Send(). According to MS, the SendAsync() method allows the caller to pass an object to the method that is invoked when the operation completes – jedipi Nov 10 '22 at 04:14
  • Even stripping away the "smtp_completed" event, the simple fact is that the email is never sent and the call fails silently. I don't know how to instrument the call (apart from a try/catch, which still doesn't throw an error) to figure out WHY this code isn't working. – TC_Guy Nov 10 '22 at 04:32
  • How do you confirm that `the email never sent`? which smtp server or credentials of it you are using here? – Chetan Nov 10 '22 at 06:13
  • As I said in the post, the network credentials are stored in the web.config file. I know that there is communication with the SMTP server, because if I change the password or username in the credentials to something that isn't valid then I get an error message. But otherwise, it executes without error and never actually sends the email. My only thought now is there's something happening on the SMTP server side where it is accepting the email but for some reason choosing not to send it. – TC_Guy Nov 10 '22 at 12:20

1 Answers1

0

You need to specify smtp client credentials and your host name like so:

var smtpClient = new SmtpClient("smtp.gmail.com")
{
    Port = 587,
    Credentials = new NetworkCredential("username", "password"),
    EnableSsl = true,
};    
smtpClient.Send("email", "recipient", "subject", "body");

Then it should send correctly. In your code it can look like this:

     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.gmail.com")
                {
                   Port = 587,
                   Credentials = new NetworkCredential("username", "password"),
                   EnableSsl = true,
                };    
                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 ( ) );
            }
        }
Almaran
  • 142
  • 1
  • 1
  • 13
  • I tried this. Same result. Thank you for offering, but it doesn't fix the problem. I'm wondering if the SMTP server is disallowing emails but isn't throwing a message about it. – TC_Guy Nov 10 '22 at 12:23
  • @TC_Guy try to use SendAsync, because you have in smtp_SendCompleted "AsyncCompletedEventargs". This might help, but I am not sure. – Almaran Nov 10 '22 at 13:54