Click here to Skip to main content
15,310,017 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:

<pre>  [HttpPost]
        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("", "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.Credentials = new System.Net.NetworkCredential("", "password");
                    smtp.EnableSsl = true;
                    smtp.UseDefaultCredentials = false;
                    smtp.Port = 587;
                    smtp.Host = "";
                    return RedirectToAction("Confirm", "Account", new { Email = user.Email });

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

smtp.Credentials = new System.Net.NetworkCredential("", "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("", "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.
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
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 have to find out what settings the smtp server you are using needs and adapt your code accordingly.
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]
Richard MacCutchan 6-Feb-20 9:22am
"Client was not authenticated to send anonymous mail"
Please take more notice of the messages you receive. These are not programming errors - you need to check the rules of the email providers.
gcogco10 6-Feb-20 9:26am

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

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