1

I have switched to ASP.NET MVC recently. I want to send email confirmation when people sign up to my website.

So I uncomment the code that ASP.NET MVC has by default for this and add configuration in web.config but that doesn't work and I kept having this error:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

I created an asp.net webforms and tried to send email from that project and that worked. So I copied the code that I had in my page load for sending email in webforms and put it in register action in account controller but I had that error again.

I really can't understand why I get this error in ASP.NET MVC, but the exact same code works fine in webforms.

This is the code in ASP.NET MVC register action:

public async Task<ActionResult> Register(RegisterViewModel model)
{
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email, BirthDate=model.BirthDate };

            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

                // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                // Send an email with this link
                //string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                //await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                //create the mail message 
                MailMessage mail = new MailMessage();

                //set the addresses 
                mail.From = new MailAddress("[email protected]"); //IMPORTANT: This must be same as your smtp authentication address.
                mail.To.Add(user.Id);

                //set the content 
                mail.Subject = "This is an email";
                mail.Body = "This is from system.net.mail using C sharp with smtp authentication.";
                //send the message 
                SmtpClient smtp = new SmtpClient("mail.wwwebco.com");

                //IMPORANT:  Your smtp login email MUST be same as your FROM address. 
                NetworkCredential Credentials = new NetworkCredential("[email protected]", "MyPassWord");
                smtp.Credentials = Credentials;
                await smtp.SendMailAsync(mail);

                return RedirectToAction("Index", "Home");
            }
            AddErrors(result);
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

and the code that I had in the asp.net webforms app and works fine is:

protected void Page_Load(object sender, EventArgs e)
{
    //create the mail message 
    MailMessage mail = new MailMessage();

    //set the addresses 
    mail.From = new MailAddress("[email protected]"); //IMPORTANT: This must be same as your smtp authentication address.
    mail.To.Add("[email protected]");

    //set the content 
    mail.Subject = "This is an email";
    mail.Body = "This is from system.net.mail using C sharp with smtp authentication.";
    //send the message 
    SmtpClient smtp = new SmtpClient("mail.wwwebco.com");

    //IMPORANT:  Your smtp login email MUST be same as your FROM address. 
    NetworkCredential Credentials = new NetworkCredential("[email protected]", "MyPassWord");
    smtp.Credentials = Credentials;
    smtp.Send(mail); 

}

I am really stuck with this problem. Please help!

Thank you.

24
  • Have you tried setting the port number your SMTP server requires? Commented Dec 2, 2015 at 5:48
  • yes i had tried that. i have that in web.config configuration now. my port is 25. Commented Dec 2, 2015 at 6:10
  • On a side note, I hope that's not your real password in the second example you pasted there... Commented Dec 2, 2015 at 6:41
  • use a strong password for the senders mail.. Commented Dec 2, 2015 at 8:03
  • Is this error happening on your local development machine or when deployed? Commented Dec 2, 2015 at 8:09

1 Answer 1

1

UPDATE:

Why are you using user.Id and not model.Email?

If you have any blank or missing details, this can cause an error?

Original:

You should change the mail.From to be mail.Sender.

Are you loading the ASP.NET Web Forms and the MVC to the same host environment? Could one be blocking port 25, or requiring SMTP to be sent over HTTPS?

For HTTPS you would need to change your port number, usually port 465, but might be different, depending on your mail sender.

If your SMTP server has restrictions, try using SendGrid, which has a free account.

Sign up to request clarification or add additional context in comments.

7 Comments

actually when i saw i couldnt send email from MVC i built a subdomain and run the webform website on that. but i had the problem before webform comes into play.
So does the error still happen when the MVC app is deployed to the sub-domain?
and my server doesnt support HTTPS and the told me always use port 25 for sending mail from server. and i am in iran and cant use many of the providers becuase i recieved TIME/ZONE error.
why my sender is null? have i missed something?
You have set the From Address, not the Sender Address! See the MailMessage class: msdn.microsoft.com/en-us/library/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.