当我将其上传到主机时(Godaddy-Windows主机)(ASP.NET CORE(.NET 5.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在得知我编写的代码没有在此主机上使用的代码后,我找到了一个新的简短代码,并且使用它没有问题。
我在这里添加一个代码可能对某人有用
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
您是否与他们的支持团队联系了您需要在代码上使用的电子邮件设置?
Have you contacted their support team about the email settings that you need to use on your code?