使用ASP.NET MVC 5在部署网站后无法发送电子邮件

发布于 2025-01-28 22:44:18 字数 4266 浏览 2 评论 0 原文

我正在尝试使用ASP.NET MVC 5发送电子邮件,我使用存储库来进行电子邮件,但部署后,它在我错的地方行不通吗? 和 发件人电子邮件ID的安全应用程序访问较低 网站是: www.upgradnagpur.com (弹出窗口上的形式)

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Index(ContactViewModel contact)
{
   if (ModelState.IsValid)
       email.SendEmail(contact.Name, contact.Mobile, contact.Email, contact.Course);
        
    return View() ;
}

​部署后

An unhandled exception occurred while processing the request.
SmtpCommandException: 5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbu
5.7.14 eI_d36d7bp1NZFngrnFGXwwLoYdTokjB3-G6FBS7FlFcxcaluThbeE-YTpCODNN4GNJ8N
5.7.14 CjZxjJ5E066rJOCuVf3xYy2g-AjXr4kaoQfcSbZY2kJxiVYmLHh6-A4kclD4BtVQ>
5.7.14 Please log in via your web browser and then try again.
5.7.14 Learn more at
5.7.14 https://support.google.com/mail/answer/78754 jl18-20020a17090775d200b006f3ef214da6sm1862389ejc.12 - gsmtp
Unknown location
AuthenticationException: 534: 5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbu
5.7.14 eI_d36d7bp1NZFngrnFGXwwLoYdTokjB3-G6FBS7FlFcxcaluThbeE-YTpCODNN4GNJ8N
5.7.14 CjZxjJ5E066rJOCuVf3xYy2g-AjXr4kaoQfcSbZY2kJxiVYmLHh6-A4kclD4BtVQ>
5.7.14 Please log in via your web browser and then try again.
5.7.14 Learn more at
5.7.14 https://support.google.com/mail/answer/78754 jl18-20020a17090775d200b006f3ef214da6sm1862389ejc.12 - gsmtp
MailKit.Net.Smtp.SmtpClient.AuthenticateAsync(Encoding encoding, ICredentials credentials, bool doAsync, CancellationToken cancellationToken</h2>

电子邮件存储库

public void SendEmail(string Name, string Mobile, string Email, string Course)
{
    var email = new MimeMessage();
    email.From.Add(MailboxAddress.Parse("[email protected]"));
    email.To.Add(MailboxAddress.Parse(Email));
    email.Subject = "upGrad";
    email.Body = new TextPart(TextFormat.Html)
    {
        Text = $"<div class=\"msg\"style=\"display: block; width:50%;margin-left:auto;margin-right:auto;font-size:20px;background:#f3f3f3;border-radius:5px;box-shadow:2px 2px #ddd; padding:15px; \">" +
        "<div>" +

        "<img style=\"height:100px; margin-left:20px;\" src=\"https://asset.brandfetch.io/idvsI-ggxm/idtgSNSCEv.png\"/>" +
        "<h3 style=\"color:Black; box-shadow:2px 2px red; text-align:center;\">Student Details</h3>" +
        $"<h6>Name   : {Name}</h6>" +
        $"<h6>Mobile : {Mobile}</h6>" +
        $"<h6>Email  : {Email}</h6>" +
        $"<h6>Course : {Course}</h6>" +
        "</div>"
    };

    // send email
    using var smtp = new MailKit.Net.Smtp.SmtpClient();
    smtp.Connect("smtp.gmail.com", 587, SecureSocketOptions.StartTls);
    smtp.Authenticate("[email protected]", "hexahexa@4321");
    smtp.Send(email);
    smtp.Disconnect(true);
    smtp.Dispose();
    email.Dispose();

}

program.cs

using newupgradnagpur.Repositories;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddSingleton<IEmailService, EmailService>();
var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    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.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

I am trying to send an email using asp.net MVC 5 I use the number of repositories for email it's working on local but after deployment, it's not working where I am wrong??
and
Less secure app access is ON of Sender Email Id
web site is : www.upgradnagpur.com (form on popup)

controller

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Index(ContactViewModel contact)
{
   if (ModelState.IsValid)
       email.SendEmail(contact.Name, contact.Mobile, contact.Email, contact.Course);
        
    return View() ;
}

this Error is after deployment

An unhandled exception occurred while processing the request.
SmtpCommandException: 5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbu
5.7.14 eI_d36d7bp1NZFngrnFGXwwLoYdTokjB3-G6FBS7FlFcxcaluThbeE-YTpCODNN4GNJ8N
5.7.14 CjZxjJ5E066rJOCuVf3xYy2g-AjXr4kaoQfcSbZY2kJxiVYmLHh6-A4kclD4BtVQ>
5.7.14 Please log in via your web browser and then try again.
5.7.14 Learn more at
5.7.14 https://support.google.com/mail/answer/78754 jl18-20020a17090775d200b006f3ef214da6sm1862389ejc.12 - gsmtp
Unknown location
AuthenticationException: 534: 5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbu
5.7.14 eI_d36d7bp1NZFngrnFGXwwLoYdTokjB3-G6FBS7FlFcxcaluThbeE-YTpCODNN4GNJ8N
5.7.14 CjZxjJ5E066rJOCuVf3xYy2g-AjXr4kaoQfcSbZY2kJxiVYmLHh6-A4kclD4BtVQ>
5.7.14 Please log in via your web browser and then try again.
5.7.14 Learn more at
5.7.14 https://support.google.com/mail/answer/78754 jl18-20020a17090775d200b006f3ef214da6sm1862389ejc.12 - gsmtp
MailKit.Net.Smtp.SmtpClient.AuthenticateAsync(Encoding encoding, ICredentials credentials, bool doAsync, CancellationToken cancellationToken</h2>

email repository

public void SendEmail(string Name, string Mobile, string Email, string Course)
{
    var email = new MimeMessage();
    email.From.Add(MailboxAddress.Parse("[email protected]"));
    email.To.Add(MailboxAddress.Parse(Email));
    email.Subject = "upGrad";
    email.Body = new TextPart(TextFormat.Html)
    {
        Text = 
quot;<div class=\"msg\"style=\"display: block; width:50%;margin-left:auto;margin-right:auto;font-size:20px;background:#f3f3f3;border-radius:5px;box-shadow:2px 2px #ddd; padding:15px; \">" +
        "<div>" +

        "<img style=\"height:100px; margin-left:20px;\" src=\"https://asset.brandfetch.io/idvsI-ggxm/idtgSNSCEv.png\"/>" +
        "<h3 style=\"color:Black; box-shadow:2px 2px red; text-align:center;\">Student Details</h3>" +
        
quot;<h6>Name   : {Name}</h6>" +
        
quot;<h6>Mobile : {Mobile}</h6>" +
        
quot;<h6>Email  : {Email}</h6>" +
        
quot;<h6>Course : {Course}</h6>" +
        "</div>"
    };

    // send email
    using var smtp = new MailKit.Net.Smtp.SmtpClient();
    smtp.Connect("smtp.gmail.com", 587, SecureSocketOptions.StartTls);
    smtp.Authenticate("[email protected]", "hexahexa@4321");
    smtp.Send(email);
    smtp.Disconnect(true);
    smtp.Dispose();
    email.Dispose();

}

program.cs

using newupgradnagpur.Repositories;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddSingleton<IEmailService, EmailService>();
var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    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.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文