Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Mates

I cant regiter new users, so my Send(mailMessage) does not allow this to happen. What could be an issue. I followed all the steps from gmail domain to set this up and i am expect my web app to work. Please assist me mates, Here is my Register controller class to do this.

What I have tried:

C#
<pre>  [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser() { UserName = model.UserName };
                user.Email = model.Email;
                user.ConfirmedEmail = false;
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage(
                        new System.Net.Mail.MailAddress("mandelacampus@gmail.com", "Web Registration"),
                        new System.Net.Mail.MailAddress(user.Email));
                    m.Subject = "Email confirmation";
                    m.Body = string.Format("Dear {0}<BR/>Thank you for your registration, please click on the below link to complete your registration: <a href=\"{1}\" title=\"User Email Confirm\">{1}</a>", user.UserName, Url.Action("ConfirmEmail", "Account", new { Token = user.Id, Email = user.Email }, Request.Url.Scheme));
                    m.IsBodyHtml = true;
                    
                    System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com");
                    smtp.Credentials = new System.Net.NetworkCredential("ggcobani@gmail.com", "password");
                    smtp.EnableSsl = true;
                    smtp.UseDefaultCredentials = false;
                    smtp.Port = 587;
                    smtp.Host = "smtp.gmail.com";
                    smtp.Send(m);
                    return RedirectToAction("Confirm", "Account", new { Email = user.Email });
                }
                else
                {
                    AddErrors(result);
                }
            }

            // If we got this far, something failed, redisplay form  478216
            return View(model);
        }
Posted
Updated 5-Feb-20 23:50pm

smtp.Credentials = new System.Net.NetworkCredential("ggcobani@gmail.com", "password");
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;


Setting UseDefaultCredentials to false clears any credentials you have previously set, so swap those statements around

smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("ggcobani@gmail.com", "password");
smtp.EnableSsl = true;


Sending email through gmail is probably one of the most frequently asked questions there is. There are countless code samples and troubleshooting guides out there, no matter what your problem you'll find a solution by googling it.
 
Share this answer
 
Comments
gcogco10 6-Feb-20 5:09am    
@F-ES Site i would have gone this forum if wasnt google this issue and yet i raised this as well to stackoverflow. I have tried the steps, still even set up my gmail security to allow apps to secure be on. What could be other issue?
F-ES Sitecore 6-Feb-20 6:19am    
Everything in gmail is attached to the "from" address. The gmail account you use in the from address is the one you need to configure to allow access, and that's the account whose credentials you use on the SmtpClient. You can't sent "from" a different account via gmail. As I've already told you, your best bet is to use the smtp server provided by your network, don't use gmail for sending email

https://forums.asp.net/post/5825785.aspx
gcogco10 6-Feb-20 7:21am    
Hi F-ES Site, you raising something new now. What i have experience and tested based on this blog you sent me. If i am using outlook, i am still getting similar but error now is "Client was not authenticated to send anonymous mail during MAIL FROM [CT2P275CA0042.ZAFP275.PROD.OUTLOOK.COM]
F-ES Sitecore 6-Feb-20 7:41am    
The solution is the same....you have to find out what settings the smtp server you are using needs and adapt your code accordingly.
 
Share this answer
 
v2
Comments
gcogco10 6-Feb-20 5:59am    
@Richard, i have just recheck under Google Account Setting, its has been set for securelessapp including password. My Question is must i used same credentials when registering for new users on my web app?
Richard MacCutchan 6-Feb-20 6:15am    
I assume yes. However in order to do this properly you should stop using SmtpClient, and use proper Google authentication. Alternatively use a different email provider (and definitely a different username) for the registration process.
gcogco10 6-Feb-20 8:34am    
Richard MacCuthan, please give me default example surely can work around.
Richard MacCutchan 6-Feb-20 9:18am    
There is no default. As I have already shown you, Google has very strict security on its email system. You need to use a different provider that will allow you to send messages from a Web service.
gcogco10 6-Feb-20 9:20am    
SO far i am trying to use this provider Outlook. I am getting Microsoft error;
Client was not authenticated to send anonymous mail during MAIL FROM [CT2P275CA0035.ZAFP275.PROD.OUTLOOK.COM]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900