c# - 为什么现在阻止我的 IP 从托管在 Azure 中的 ASP.NET Core 应用程序通过 Mailkit 发送电子邮件?
问题描述
在我的 ASP.NET Core Web 中,我为我的用户使用了身份验证。他们可以重置密码,并在创建帐户时收到确认电子邮件。为此,我使用Mailkit。当我在本地运行我的应用程序时,它工作正常并且可以毫无问题地发送电子邮件,当我将它部署到 Azure 时它曾经可以工作,但有一天它停止了。
然后,我收到一封电子邮件发送失败收据,内容如下:
someuser@hotmail.co.uk:来自远程服务器的 SMTP 错误,用于 MAIL FROM 命令,主机:eur.olc.protection.outlook.com (104.47.12.33) 原因:550 5.7.1 服务不可用,客户端主机 [82.165.159.37]使用 S pamhaus 阻止。要请求从此列表中删除,请参阅 https://www.spamhaus.or g/query/ip/82.165.159.37 (AS3130)。[DB3EUR04FT052.eop-eur04.prod.protection.outlook.com]
因此,我的应用程序已被https://www.spamhaus.org列入黑名单。给出的理由是:
此 SBL 中列出的 IP 地址由 1&1 使用,仅用于传递已在内部(由 1&1)分类为垃圾邮件的电子邮件。
我联系了我的服务提供商,他们不知道这意味着什么,也不知道如何解锁任何东西。因此,出于良好秩序的原因,我认为检查我的设置和代码以查看在这种情况下我可能做错了什么并尝试修复它是个好主意。虽然这可能不会删除任何黑名单,但它可能有助于将来防止它。
这是我对 mailkit 的设置,从 appsettings 开始。您会注意到我使用端口 465 通过 SSL 发送,这是我理解的需要。
应用设置.json
{
"EmailConfiguration": {
"SmtpServer": "smtp.ionos.co.uk",
"SmtpPort": 465,
"SmtpUsername": "myemail@mydomain.co.uk",
"SmtpPassword": "xxx",
"PopServer": "pop.ionos.co.uk",
"PopPort": 993,
"PopUsername": "myemail@mydomain.co.uk",
"PopPassword": "xxx"
},
"AllowedHosts": "*"
}
区域/页面/帐户/管理/ForgotPassword.cshtml.cs
public async Task<IActionResult> OnPostAsync()
{
if (ModelState.IsValid)
{
var user = await _userManager.FindByEmailAsync(Input.Email);
if (user == null || !(await _userManager.IsEmailConfirmedAsync(user)))
{
// Don't reveal that the user does not exist or is not confirmed
return RedirectToPage("./ForgotPasswordConfirmation");
}
var code = await _userManager.GeneratePasswordResetTokenAsync(user);
code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
var callbackUrl = Url.Page(
"/Account/ResetPassword",
pageHandler: null,
values: new { area = "Identity", code },
protocol: Request.Scheme);
var callbackEncoded = HtmlEncoder.Default.Encode(callbackUrl);
//============================
// Send email
//============================
EmailMessage accountEmail = new EmailMessage
{
FromAddresses = {
new EmailAddress {
Name = "Password Reset",
Address = "myemail@mydomain.co.uk" }
},
ToAddresses = {
new EmailAddress {
Name = Input.Email,
Address = Input.Email }
},
Subject = "Password Reset"
};
//Pass to email action
SendEmail(accountEmail, callbackEncoded);
return RedirectToPage("./ForgotPasswordConfirmation");
}
return Page();
}
上面的代码几乎是 OOTB 密码重置,其中包含填充在“发送电子邮件”评论下的我的 mailkit 信息。从那里,信息被传递给我的SendEmail()
操作进行处理。
区域/页面/帐户/管理/ForgotPassword.cshtml.cs
public void SendEmail(EmailMessage emailMessage, string callbackUrl)
{
//============================
// Setup email
//============================
var message = new MimeMessage();
message.To.AddRange(emailMessage.ToAddresses.Select(x => new MailboxAddress(x.Name, x.Address)));
message.From.AddRange(emailMessage.FromAddresses.Select(x => new MailboxAddress(x.Name, x.Address)));
message.Subject = emailMessage.Subject;
//============================
// Use body builder for HTML and plain text
//============================
var builder = new BodyBuilder();
//============================
// Embed my logo
//============================
var image = builder.LinkedResources.Add(@"wwwroot/images/logo.png");
image.ContentId = MimeUtils.GenerateMessageId();
//Set the plain-text version of the email
builder.TextBody = @"Oops! it looks like you've forgotten your password, you can reset it by clicking on the following link: " + callbackUrl;
//Set the HTML version of the message
builder.HtmlBody = string.Format(@"Oops! it looks like you've forgotten your password, you can reset it by clicking on the following link: " + callbackUrl);
message.Body = builder.ToMessageBody();
//Be careful that the SmtpClient class is the one from Mailkit not the framework!
using (var emailClient = new SmtpClient())
{
//The last parameter here is to use SSL (Which you should!)
try
{
emailClient.Connect(_emailConfiguration.SmtpServer, _emailConfiguration.SmtpPort, true);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
//Remove any OAuth functionality as we won't be using it.
emailClient.AuthenticationMechanisms.Remove("XOAUTH2");
emailClient.Authenticate(_emailConfiguration.SmtpUsername, _emailConfiguration.SmtpPassword);
try
{
emailClient.Send(message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
emailClient.Disconnect(true);
}
}
我的设置是否不适合我正在尝试做的事情,无论如何我可以改进这一点吗?被列入黑名单令人沮丧,但我不明白如何才能解决它。
解决方案
推荐阅读
- reactjs - 使用 Semantic UI React 保留标签的形状
- sql - Ruby on Rails 数据库表操作
- javascript - 从表单 HTML/JS 捕获用户输入
- c++ - 尽管写入组合,但 PCI 总线读取性能低
- python - Python 和雪花无法将转换为 JSON 的数据帧插入到雪花上的变体字段中
- prolog - Prolog - 无法按预期运行程序
- laravel - 404 Not Found nginx /1.18.0 aws laravel 项目
- python - 如何使用 CSS 和 Bootstrap 扩展文本宽度
- android - 填充并发出 StateFlow 列表
- java - 找到矩阵的行列式