当我将其上传到主机时(Godaddy-Windows主机)(ASP.NET CORE(.NET 5.0))时,我的电子邮件代码无法工作。

发布于 2025-01-29 19:34:35 字数 4123 浏览 3 评论 0原文

在我使用IIS Express的时间里,我没有任何发送邮件的问题。 上传到主机后,在测试时继续加载,然后返回HTTP 500错误。查看数据库时会创建用户,但会出现错误,因为它无法发送邮件。我应该怎么办? 同时,我使用ASP.NET Core,.NET版本5.0,我使用的主机是GoDaddy Windows主机,

这些是我的电子邮件代码

using System.Threading.Tasks;

namespace gamesellMVC.EmailServices
{
    public interface IEmailSender
    {
        Task SendEmailAsync(string email, string subject, string htmlMessage);
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Threading.Tasks;

namespace gamesellMVC.EmailServices
{
    public class SmtpEmailSender : IEmailSender
    {
        private string _host;
        private int _port;
        private bool _enableSSL;
        private string _username;
        private string _password;
        public SmtpEmailSender(string host, int port, bool enableSSL, string username, string password)
        {
            this._host = host;
            this._port = port;
            this._enableSSL = enableSSL;
            this._username = username;
            this._password = password;
        }
        public Task SendEmailAsync(string email, string subject, string htmlMessage)
        {
            var client = new SmtpClient(this._host, this._port)
            {
                Credentials = new NetworkCredential(_username, _password),
                EnableSsl = this._enableSSL
            };
            return client.SendMailAsync(
                new MailMessage(this._username, email, subject, htmlMessage)
                {
                    IsBodyHtml = true
                });
        }
    }
}

我的启动代码


services.AddScoped<IEmailSender, SmtpEmailSender>(i =>
                        new SmtpEmailSender(
                            _configuration["EmailSender:Host"],
                            _configuration.GetValue<int>("EmailSender:Port"),
                            _configuration.GetValue<bool>("EmailSender:EnableSSL"),
                            _configuration["EmailSender:UserName"],
                            _configuration["EmailSender:Password"]
                            ));

我的appsetting.json代码


"EmailSender": {
    "Host": "smtp.office365.com",
    "Port": 587,
    "EnableSSL": true,
    "UserName": "my microsft email",
    "Password": "my microsft password"
  },

和我用来发送邮件确认的代码部分

[AllowAnonymous]
        [HttpPost]
        public async Task<IActionResult> Register(RegisterModelF model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            var user = new User()
            {
                UserName = model.NickName,
                Email = model.Email,
                Dob = model.Dob
            };
            var result = await _userManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                await _userManager.AddToRoleAsync(user, "Customer");
                var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                var url = Url.Action("ConfirmEmail", "Account", new
                {
                    userId = user.Id,
                    token = code
                });

                await _emailSender.SendEmailAsync(model.Email, "Hesabı təstiqləyin",
                    "<!DOCTYPE html>" +
                    "<html>" +
                    "<body style=\"background-color:#ff7f26; text-align:center;\">" +
                    "<h2 style=\"color:#051a80;\">Confirm your mail</h2>" +
                    $"<label style=\"color:orange;font-size:100px;border:5px dotted;\"><a href='https://localhost:44348{url}'>Confirm</a></label>" +
                    "</body>" +
                    "</html>"
                    );

                return RedirectToAction("Login", "Account");

            }

            ModelState.AddModelError("", "Error");
            return View(model);
        }

During the time I used iis express, I did not have any problems sending mail.
After uploading to the host, loading goes on while testing, and then returns an http 500 error. Creates a user when looking at the database but gives an error because it cannot send mail. what should I do?
In the meantime, I use asp.net core, .net version 5.0, the host I use is godaddy windows host

these were my email codes

using System.Threading.Tasks;

namespace gamesellMVC.EmailServices
{
    public interface IEmailSender
    {
        Task SendEmailAsync(string email, string subject, string htmlMessage);
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Threading.Tasks;

namespace gamesellMVC.EmailServices
{
    public class SmtpEmailSender : IEmailSender
    {
        private string _host;
        private int _port;
        private bool _enableSSL;
        private string _username;
        private string _password;
        public SmtpEmailSender(string host, int port, bool enableSSL, string username, string password)
        {
            this._host = host;
            this._port = port;
            this._enableSSL = enableSSL;
            this._username = username;
            this._password = password;
        }
        public Task SendEmailAsync(string email, string subject, string htmlMessage)
        {
            var client = new SmtpClient(this._host, this._port)
            {
                Credentials = new NetworkCredential(_username, _password),
                EnableSsl = this._enableSSL
            };
            return client.SendMailAsync(
                new MailMessage(this._username, email, subject, htmlMessage)
                {
                    IsBodyHtml = true
                });
        }
    }
}

my startup codes


services.AddScoped<IEmailSender, SmtpEmailSender>(i =>
                        new SmtpEmailSender(
                            _configuration["EmailSender:Host"],
                            _configuration.GetValue<int>("EmailSender:Port"),
                            _configuration.GetValue<bool>("EmailSender:EnableSSL"),
                            _configuration["EmailSender:UserName"],
                            _configuration["EmailSender:Password"]
                            ));

my appsetting.json codes


"EmailSender": {
    "Host": "smtp.office365.com",
    "Port": 587,
    "EnableSSL": true,
    "UserName": "my microsft email",
    "Password": "my microsft password"
  },

and the code section I used to send the mail confirmation

[AllowAnonymous]
        [HttpPost]
        public async Task<IActionResult> Register(RegisterModelF model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            var user = new User()
            {
                UserName = model.NickName,
                Email = model.Email,
                Dob = model.Dob
            };
            var result = await _userManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                await _userManager.AddToRoleAsync(user, "Customer");
                var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                var url = Url.Action("ConfirmEmail", "Account", new
                {
                    userId = user.Id,
                    token = code
                });

                await _emailSender.SendEmailAsync(model.Email, "Hesabı təstiqləyin",
                    "<!DOCTYPE html>" +
                    "<html>" +
                    "<body style=\"background-color:#ff7f26; text-align:center;\">" +
                    "<h2 style=\"color:#051a80;\">Confirm your mail</h2>" +
                    
quot;<label style=\"color:orange;font-size:100px;border:5px dotted;\"><a href='https://localhost:44348{url}'>Confirm</a></label>" +
                    "</body>" +
                    "</html>"
                    );

                return RedirectToAction("Login", "Account");

            }

            ModelState.AddModelError("", "Error");
            return View(model);
        }

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋叶绚丽 2025-02-05 19:34:35

在得知我编写的代码没有在此主机上使用的代码后,我找到了一个新的简短代码,并且使用它没有问题。
我在这里添加一个代码可能对某人有用

using (MailMessage mm = new MailMessage(" from @mail.com ", " to @mail.com "))
                {
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                    mm.Subject = " Your Title ";
                    mm.Body = "<!DOCTYPE html>" +  // your shot message or like my html page
                    "<html>" +
                    "<body style=\"background-color:#ff7f26; text-align:center;\">" +
                    "<h2 style=\"color:#051a80;\">Confirm your mail</h2>" +
                    $"<label style=\"color:orange;font-size:100px;border:5px dotted;\"><a href='https://mylink{url}'>Confirm</a></label>" +
                    "</body>" +
                    "</html>";
                    mm.IsBodyHtml = true;
                    SmtpClient smtp = new SmtpClient();
                    smtp.Host = " smtp link ";
                    smtp.EnableSsl = true; // (true or false)
                    NetworkCredential NetworkCred = new NetworkCredential(" from @mail.com ", " from mail's password");
                    smtp.UseDefaultCredentials = true; // (true or false)
                    smtp.Credentials = NetworkCred;
                    smtp.Port = 25; // (25,465,587 or ...)
                    smtp.Send(mm);
                }

After learning that the code I wrote did not work on this host, I found a new short code and there was no problem using it.
I'm adding a code here that might be useful to someone

using (MailMessage mm = new MailMessage(" from @mail.com ", " to @mail.com "))
                {
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                    mm.Subject = " Your Title ";
                    mm.Body = "<!DOCTYPE html>" +  // your shot message or like my html page
                    "<html>" +
                    "<body style=\"background-color:#ff7f26; text-align:center;\">" +
                    "<h2 style=\"color:#051a80;\">Confirm your mail</h2>" +
                    
quot;<label style=\"color:orange;font-size:100px;border:5px dotted;\"><a href='https://mylink{url}'>Confirm</a></label>" +
                    "</body>" +
                    "</html>";
                    mm.IsBodyHtml = true;
                    SmtpClient smtp = new SmtpClient();
                    smtp.Host = " smtp link ";
                    smtp.EnableSsl = true; // (true or false)
                    NetworkCredential NetworkCred = new NetworkCredential(" from @mail.com ", " from mail's password");
                    smtp.UseDefaultCredentials = true; // (true or false)
                    smtp.Credentials = NetworkCred;
                    smtp.Port = 25; // (25,465,587 or ...)
                    smtp.Send(mm);
                }
中二柚 2025-02-05 19:34:35

您是否与他们的支持团队联系了您需要在代码上使用的电子邮件设置?

Have you contacted their support team about the email settings that you need to use on your code?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文