问题描述
我正在为我的项目发送电子邮件验证。它在我的本地主机上工作,但是当我在 smartasp.net 服务器上部署我的项目时,它不工作并给我这个错误。 异常:system.invalidOperationException:SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.7.0 Authentication required。 我在不同平台以及堆栈溢出中搜索了很多解决方案,但没有一个解决方案适用于我的项目。请帮我解决这个问题。
我的项目代码:
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","environmentvariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
}
},"SendEmailProject": {
"commandName": "Project","dotnetRunMessages": "true","applicationUrl": "https://localhost:5001;http://localhost:5000","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();
});
}
}
}
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("RegisterConfirmation",new { email = Input.Email,returnUrl = returnUrl });
}
else
{
await _signInManager.SignInAsync(user,isPersistent: false);
return LocalRedirect(returnUrl);
}
}
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty,error.Description);
}`enter code here`
}
enter code here
// If we got this far,something Failed,redisplay form
return Page();
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)