当我将它上传到主机 (godaddy- windows 主机) (Asp.net 核心 (.net 5.0)) 时,我的电子邮件代码不起作用

My email code doesn't work when I upload it to the host (godaddy- windows host) (Asp.net core (.net 5.0))

我在使用iis express的这段时间里,发邮件没有任何问题。 上传到主机后,一边加载一边测试,然后returns一个http 500错误。查看数据库时创建用户,但由于无法发送邮件而出错。我应该怎么办? 同时,我使用asp.net core,.net 5.0版本,我使用的主机是godaddy windows host

这些是我的电子邮件代码

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);
        }

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

得知我写的代码在这台主机上不起作用,我找了一个新的短代码,使用没有问题。 我在这里添加可能对某人有用的代码

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);
                }