0

As the title above, I have created a project with simple log-in and registration. Inside the program, I have use System.Net.Mail.SmtpException to send the email verification to the user email. I have done created the email but when I click on create a new account, I met this problem as well.

System.Net.Mail.SmtpException: 'The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required

From the information I search, I found out someone say I need to make my email 2-step verification. I did it but the problem still there.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;
using Food_Founder.Models;

namespace Food_Founder.Controllers
{
    public class UserController : Controller
    {
        //Registration
        [HttpGet]
        public ActionResult Registration()
        {
            return View();
        }

        //Post Registration
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Registration([Bind(Exclude = "IsEmailVerified,ActivationCode")]User user)
        {
            //Model Validation
            bool Status = false;
            string message = "";

            //Email is already exist
            if (ModelState.IsValid)
            {
                #region Email is already exist
                var isExist = IsEmailExist(user.Email);
                if (isExist)
                {
                    ModelState.AddModelError("EmailExist", "Email already exist");
                    return View(user);
                }
                #endregion

                #region Generate Activation Code
                user.ActivationCode = Guid.NewGuid();
                #endregion

                #region Password Hashing
                user.Password = Crypto.Hash(user.Password);
                user.ConfirmPassword = Crypto.Hash(user.ConfirmPassword);
                #endregion

                user.IsEmailVerified = false;

                #region Save Data to Database
                using (myDatabaseEntities myDatabase = new myDatabaseEntities())
                {
                    myDatabase.Users.Add(user);
                    myDatabase.SaveChanges();

                    //Send Email to User
                    SendVerificationLinkEmail(user.Email, user.ActivationCode.ToString());
                    message = "Registration successfully done. Account activation link" +
                        "has been send to your Email:" + user.Email + "Please go check and activate your account";
                    Status = true;
                }
                #endregion

            }
            else
            {
                message = "Invalid Request";
            }

            ViewBag.Message = message;
            ViewBag.Status = Status;
            return View(user);
        }

        //Verify Email


        //Verify Email Link


        //Login


        //Login POST


        //Logout



        [NonAction]
        public Boolean IsEmailExist(string email)
        {
            using (myDatabaseEntities myDatabase = new myDatabaseEntities())
            {
                var v = myDatabase.Users.Where(a => a.Email == email).FirstOrDefault();
                return v != null;
            }
        }

        [NonAction]
        public void SendVerificationLinkEmail(string email, string activationCode)
        {
            var verifyUrl = "/User/VerifyAccount/" + activationCode;
            var link = Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, verifyUrl);

            var fromEmail = new MailAddress("[email protected]", "yukwokyao");
            var toEmail = new MailAddress(email);
            var fromEmailPassword = "********";
            string subject = "Your account is successfully created!";

            string body = "<br/><br/>We are excited to tell you that your FoodFounder account is" +
                "successfully created. Please click the below link to verify your FoodFounder account" +
                "<a href = '" + link + "' >" + link + "</a>";

            var smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(fromEmail.Address, fromEmailPassword)
            };

            using (var message = new MailMessage(fromEmail, toEmail)
            {
                Subject = subject,
                Body = body,
                IsBodyHtml = true
            })
            smtp.Send(message);
        }
    }
}

The code above is the controller class I created. The problem I found is there so that I didn't put any other design html code in here. The most weird part is even I met this problem, the data I register still can key-in into the database but this annoying problem still exist there. Anywhere, if anything I miss put, please inform me ya.. Thank you.

0

1 Answer 1

0

If you are using your Gmail account as SMTP server you need to allow access from what Google calls "less secure apps". You can enable it from your Google settings.

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

2 Comments

Try here link
I have enable it but the error still there.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.