如何解决我正在尝试使用 smtp 从 angular 2.0 的 asp.net core web api 向多个用户发送电子邮件但它抛出的 CORS 错误
下面是我的错误:
Access to XMLHttpRequest at 'https://localhost:44359/api/ClientCandidateBulkStatusUpdate' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
我在 startup.cs 中进行了如下设置:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
// .SetIsOriginAllowed(origin => true)//allow any origin
.AllowCredentials());
// .Build());
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app,IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json",optional: true,reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json",optional: true);
builder.AddEnvironmentvariables();
builder.Build();
app.UseCors("CorsPolicy");
////app.UseCors(builder1 => builder1
//// .AllowAnyOrigin()
//// .AllowAnyMethod()
//// .AllowAnyHeader()
//// .SetIsOriginAllowed(origin => true)//allow any origin
//// .AllowCredentials());// allow credentials
app.UseHttpsRedirection();
app.UseMvc();
}
我像在控制器上启用了 CORS
[Route("api/[controller]")]
[ApiController]
[EnableCors("CorsPolicy")]
public class ClientCandidateBulkStatusUpdateController : ControllerBase
{}
public void Sendmail(String MailFrom,String Mailto,String Subject,String Body,String CC = "",Boolean copy = false)
{
DataTable EmailSettingsDT = new DataTable();
EmailSettingsDT = GetEmailSettings();
if (EmailSettingsDT.Rows.Count > 0)
{
string a = EmailSettingsDT.Rows[0]["SMTPHost"].ToString();
int port = Convert.ToInt32(EmailSettingsDT.Rows[0]["SMTPPort"]);
using (SmtpClient smtp = new SmtpClient(EmailSettingsDT.Rows[0]["SMTPHost"].ToString()))
{
var basicCredential = new NetworkCredential(EmailSettingsDT.Rows[0]["SMTPUsername"].ToString(),EmailSettingsDT.Rows[0]["SMTPPassword"].ToString());
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = Convert.ToBoolean(EmailSettingsDT.Rows[0]["IS_SSL"].ToString());
smtp.UseDefaultCredentials = false;
smtp.Credentials = basicCredential;
smtp.Port = Convert.ToInt32(EmailSettingsDT.Rows[0]["SMTPPort"].ToString());
var mail = new MailMessage();
mail.From = new MailAddress(EmailSettingsDT.Rows[0]["SMTPUsername"].ToString(),EmailSettingsDT.Rows[0]["displayName"].ToString());
mail.To.Add(Mailto);
if (copy == true)
{
mail.CC.Add(CC);
}
mail.IsBodyHtml = true;
mail.Subject = Subject;
mail.Body = Body;
smtp.Send(mail);
mail.dispose();
smtp.dispose();
}
//var smtp = new SmtpClient(EmailSettingsDT.Rows[0]["SMTPHost"].ToString());
}
}
SMTP PORT:587,HOST:smtp.gmail.com
我的角度代码是:
UpdateClientReviewCandidateStatus(Data: any) {
const header = new Headers({ 'Content-type': 'application/json' });
return this._http.post(this.apiUrl + 'ClientCandidateBulkStatusUpdate',Data,httpOptions).pipe(map((data: Response) => <any>data))
}
我尝试使用注释代码也仍然没有解决问题。 SMTP 限制发送 100 封电子邮件。我还没有达到限制,但它在发送批量电子邮件时仍在抛出。 一些电子邮件,如 5 或 10 正在发送,但其他电子邮件结构会出现上述 CORS 错误。 我也尝试使用 MailKit 和 MailChimp 发送邮件,但没有找到解决方案。 请问谁能帮我解决这个错误。
提前致谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。