I am stack in sending email verification for my project. It is working on my localhost, but when I deploy my project on smartasp.net server then it is not working and give me this error.
Exception: System.InvalidOperationException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required.
I was searching a lot to find out a solution in different platform aslo in stack overflow, but no one of the solutions is working for my project. Please, help me about that.
My project code:
appsettings.production.json
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=SQL5101.site4now.net;Initial Catalog=DB_A6E3FF_postemaildb;User Id=DB_A6E3FF_postemaildb_admin;Password=password;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Email": {
"Host": "smtp.gmail.com",
"Port": "587",
"MailFrom": "email",
"Password": "password"
}
}
launchSettings.json
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:54686",
"sslPort": 44381
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
}
},
"IISX-Production": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
}
},
"SendEmailProject": {
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
}
}
}
}
SendEmail.cs
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Threading.Tasks;
namespace SendEmailProject.Utility
{
public class SendEmail : IEmailSender
{
private readonly EmailSender _options;
public SendEmail(IOptions<EmailSender> options)
{
_options = options.Value;
}
public async Task SendEmailAsync(string SenderEmail, string Subject, string Message)
{
try
{
string HostAddress = _options.Host;
string FormEmailId = _options.MailFrom;
string Password = _options.Password;
int Port = _options.Port;
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(FormEmailId);
mailMessage.Subject = Subject;
mailMessage.Body = Message;
mailMessage.IsBodyHtml = true;
mailMessage.To.Add(new MailAddress(SenderEmail));
SmtpClient smtp = new SmtpClient();
smtp.Host = HostAddress;
smtp.EnableSsl = false;
NetworkCredential networkCredential = new NetworkCredential();
networkCredential.UserName = mailMessage.From.Address;
networkCredential.Password = Password;
smtp.UseDefaultCredentials = false;
smtp.Credentials = networkCredential;
smtp.Port = Convert.ToInt32(Port);
await smtp.SendMailAsync(mailMessage);
}
catch (Exception ex)
{
throw new InvalidOperationException(ex.Message);
}
}
}
}
EmailSender.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace SendEmailProject.Utility
{
public class EmailSender
{
public string Host { get; set; }
public int Port { get; set; }
public string MailFrom { get; set; }
public string Password { get; set; }
}
}
Startup.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SendEmailProject.DataAccess.Initializer;
using SendEmailProject.DataAccess.Migrations;
using SendEmailProject.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace SendEmailProject
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDatabaseDeveloperPageExceptionFilter();
services.AddIdentity<IdentityUser, IdentityRole>(options => {
options.SignIn.RequireConfirmedEmail = true;
}).AddDefaultTokenProviders()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.Configure<EmailSender>(Configuration.GetSection("Email"));
services.AddSingleton<IEmailSender, SendEmail>();
//Configuration for deployment
services.AddScoped<IDbInitializer, DbInitializer>();
services.ConfigureApplicationCookie(options =>
{
options.LoginPath = $"/Identity/Account/Login";
options.LogoutPath = $"/Identity/Account/Logout";
options.AccessDeniedPath = $"/Identity/Account/AccessDenied";
});
services.AddAuthentication().AddFacebook(options =>
{
options.AppId = "AppId ";
options.AppSecret = "AppSecret";
});
services.AddAuthentication().AddGoogle(options =>
{
options.ClientId = "ClientId ";
options.ClientSecret = "ClientSecret";
});
services.AddControllersWithViews().AddRazorRuntimeCompilation();
services.AddRazorPages();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IDbInitializer dbInitializer)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
dbInitializer.Initialize();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{area=Customer}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
}
}
}
Register function code
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
returnUrl ??= Url.Content("~/");
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
if (ModelState.IsValid)
{
var user = new IdentityUser { UserName = Input.Email, Email = Input.Email };
var result = await _userManager.CreateAsync(user, Input.Password);
if (result.Succeeded)
{
_logger.LogInformation("User created a new account with password.");
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
var callbackUrl = Url.Page(
"/Account/ConfirmEmail",
pageHandler: null,
values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl },
protocol: Request.Scheme);
//await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
// $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
var PathToFile = _webHostEnvironment.WebRootPath + Path.DirectorySeparatorChar.ToString()
+ "Templates" + Path.DirectorySeparatorChar.ToString() + "EmailTemplates"
+ Path.DirectorySeparatorChar.ToString() + "AccountConfirmation.html";
string body = string.Empty;
using (StreamReader streamReader = System.IO.File.OpenText(PathToFile))
{
body = streamReader.ReadToEnd();
}
body = body.Replace("{ConfirmationLink}", callbackUrl);
body = body.Replace("{UserName}", Input.Email);
await _emailSender.SendEmailAsync(Input.Email, "Confirm your account", body);
if (_userManager.Options.SignIn.RequireConfirmedAccount)
{
return RedirectToPage("RegisterC