在我使用 iis express 期間,我發送郵件沒有任何問題。上傳到主機后,加載繼續測驗,然后回傳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);
}
uj5u.com熱心網友回復:
得知自己寫的代碼在這臺主機上不行后,又找了個新的短代碼,用起來也沒問題。我在這里添加一個可能對某人有用的代碼
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);
}
uj5u.com熱心網友回復:
您是否就需要在代碼上使用的電子郵件設定聯系了他們的支持團隊?
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/479850.html
上一篇:在GemBox檔案ASP.Netc#中將粗體設定為段落
下一篇:我想從api獲取資料(姓名和電子郵件)。我的程式已成功編譯,但我在控制臺中遇到錯誤。https://randomuser.me/api
