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.