Trouble sending email: ASP.NET MVC

Discussion in 'ASP.NET / ASP.NET Core' started by knoxy, Mar 8, 2011.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. Okay, firstly I'd like to say I'm using localhost :)

    I've raised a support ticket and everything seems to be fine re: telnet

    So I'm guessing it has to be my code? It doesn't error and redirects to the correct view but I never receive the email... I've pasted the code below, I don't know if anyone can take a quick look and let me know if I'm doing anything stupid please? ;)

    [HttpPost]
    public ActionResult Index(RSVPFormModel model)
    {
    if (ModelState.IsValid) {
    try {
    var mail = new MailMessage();
    mail.To.Add("[email protected]");
    mail.From = new MailAddress("[email protected]");
    mail.Subject = "RSVP Notification!";
    mail.Body = "test";
    mail.IsBodyHtml = true;
    var smtp = new SmtpClient();
    smtp.Host = "localhost";
    smtp.Send(mail);
    mail.Dispose();

    }
    catch (Exception) {
    return View(model);
    }
    }

    return RedirectToAction("ThankYou");
    }
     
  2. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    Try removing [HttpPost].
     
  3. Didn't work I'm afraid

    It was getting through to the "return RedirectToAction("ThankYou");" line anyway and then rendering that view - seems to indicate a successful send as it's firing "smtp.Send(...);" though?
     
  4. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    The only thing I can suggest trying is instead of using an implicitly typed variable var, declare explicitly to see if that works:

    MailMessage mail = new MailMessage();
    SmtpClient smtp = new SmtpClient();
     
  5. Afraid that didn't work either... would any unsent messages be left in a folder/queue that can be checked by the support team?
     
  6. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    You would probably receive a bounce. The only thing I can think of at this moment is that your code is not executing the try block. You should try to isolate the problem by trying to send an email message outside of the try/catch block. If that works, then you should revisit the logic of your code.
     
  7. I'm also having a problem sending email for some reason. I'm using MVC, but my email class is not inside a controller directly. I'm also using localhost in basically the same way as above and I'm not receiving emails.

    Code:
    try
                {
                    var mail = new MailMessage();
                    SmtpClient smtp = new SmtpClient(ConfigurationManager.AppSettings["SMTPServer"]);
                    mail.From = new MailAddress(ConfigurationManager.AppSettings["SMTPEmail"]);
                    mail.To.Add(new MailAddress(toEmail));
                    mail.Subject = "Verify your registration;
                    mail.Body = "Dear Blah blah"
                
                    smtp.Send(mail);
                    return true;
                }
                catch (Exception ex)
                {
                    logger.WarnFormat("Exception occurred while registering", ex.Message);
                    return false;
                }
    
    
     
  8. Just wanted to post an update to this - as suspected, was due to my code :)

    My model state was invalid - a really stupid miss on my part - came back to it and found the issue inside of 20 seconds!

    Emails went out fine after that :) Sorry for wasting your time Tasslehoff!
     
  9. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    No problem. Glad you got it fixed. ;)
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page