0

I'm trying to figure out how to create a simple contact page in order to send an email. Right now I'm just testing but i'm not getting any error message.

Here is my code:

protected void btnSend_Click(object sender, EventArgs e)
{
   MailMessage msg = new MailMessage();
   msg.To.Add(txtFrom.Text);
   msg.Subject = txtSubject.Text;
   msg.Body = txtMessage.Text;

   SmtpClient smtp = new SmtpClient("localhost");
   smtp.Send(msg);

   Response.Write("Your email was sent");
}
Brian
  • 5,069
  • 7
  • 37
  • 47
Troy Bryant
  • 994
  • 8
  • 29
  • 60
  • And what's happening/wrong? – mnsr Nov 04 '13 at 23:25
  • 2
    My guess is `localhost` is not a real SMTP server or relay point. Replace that with a real SMTP server address and it should work. – James Nov 04 '13 at 23:27
  • Either what @James said or you might need some kind of credentials. Look at [this SO question](http://stackoverflow.com/questions/2766928/how-to-set-username-and-password-for-smtpclient-object-in-net) – Icemanind Nov 04 '13 at 23:29
  • @icemanind if credentials were needed it should throw an exception. – logixologist Nov 04 '13 at 23:31
  • @logixologist - True, but wouldn't it also throw an exception if "localhost" was not a real SMTP server? – Icemanind Nov 04 '13 at 23:32
  • 1
    But if localhost had SMTP running but it rejected the email depending on the settings I have seen it go into oblivion or it gets stuck in the IIS's "Could Not Send" box. – logixologist Nov 04 '13 at 23:33
  • Both `MailMessage` and `SmtpClient` implement the `IDisposable` interface and should therefore be wrapped in `using` statements for deterministic disposal of the unmanaged resources they contain. – Jesse C. Slicer Nov 04 '13 at 23:34
  • 1
    @JesseC.Slicer SmtpClient is not IDisposable pre .net4.0. Just want to point that out in case this user is using an older version. – JClaspill Nov 04 '13 at 23:56
  • @JClaspill excellent observation. – Jesse C. Slicer Nov 05 '13 at 00:47
  • You should have an exception based on the missing From address. Have you confirmed `btnSend_Click` is getting hit at all? – Chris Schiffhauer Nov 05 '13 at 02:11

3 Answers3

2

Do you have an Smtp service running on your localhost? If not you can use Gmail's smtp service for testing.

SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.Credentials = new NetworkCredential("<your gmail login>", "<your gmail password>");
client.EnableSsl = true;
Scott
  • 81
  • 4
0

As others have pointed out, you probably don't have the localhost as a real mail server. To get a better idea, turn Tracing on and change your code to something more like this:

protected void btnSend_Click(object sender, EventArgs e) {
    Trace.Write("btnSend_Click initialized");
    string resp = "An unknown error occured.";
    using (MailMessage msg = new MailMessage()) {
        try {
            msg.To.Add(txtFrom.Text);
            msg.Subject = txtSubject.Text;
            msg.Body = txtMessage.Text;
            SmtpClient smtp = new SmtpClient("localhost");
            Trace.Write("smtp client created.");
            smtp.Send(msg);
            Trace.Write("smtp message sent.");
            resp = "Your message was sent.";
        } catch (Exception ex) {
            Trace.Warn("Smtp Error", ex.Message);
            resp = "There was an error sending your message.";
        }
    }
    Response.Write(resp);
    Trace.Write("btnSend_Click completed");
}
JClaspill
  • 1,725
  • 19
  • 29
  • enabled page tracing and reworked my code but still not getting an error. – Troy Bryant Nov 05 '13 at 00:46
  • @TroyBryant Hmm. Assuming you know how to read the trace log, that perhaps means the function is never firing? Try adding a `Trace.Write("function call");` to right under the `protected void` line and run it again. Not sure I've ever seen a silent smtp failure, so maybe it just isn't getting called. – JClaspill Nov 05 '13 at 01:22
  • @TroyBryant Added more tracing to the example above to potentially figure out where it might be dying. – JClaspill Nov 05 '13 at 01:31
  • I dont think the button is being called. I'm not that famaliar with tracing and reading up on it. But its just strange I expected an error to be throw by my smptClient("localhost") but that or anything else that I've coded up isnt thrown an error. – Troy Bryant Nov 05 '13 at 13:36
0

You must specify a From address like this:

msg.From = new MailAddress("noreply@noreply"); // Use a real address, of course

Because you are not getting an exception, I wonder if btnSend_Click is getting hit at all...

Other things to look at:

  1. Is your txtFrom.Text being overwritten on postback? You might hardcode your email address during debugging.
  2. Try 127.0.0.1 instead of localhost.
Chris Schiffhauer
  • 17,102
  • 15
  • 79
  • 88