如何解决为什么我的 IP 现在被阻止通过 Mailkit 从我托管在 Azure 中的 ASP.NET Core 应用程序发送电子邮件?
在我的 ASP.NET Core Web 中,我为我的用户使用了身份验证。他们可以在创建帐户时重置密码并收到确认电子邮件。为此,我使用 Mailkit。当我在本地运行我的应用程序时,它运行良好并且可以毫无问题地发送电子邮件,它曾经在我将其部署到 Azure 时运行但有一天停止了。
someuser@hotmail.co.uk:来自远程服务器的 MAIL SMTP 错误 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.prote ction.outlook.com]
因此,我的应用程序已被 https://www.spamhaus.org 列入黑名单。给出的理由是:
此 SBL 中列出的 IP 地址仅被 1&1 用于 发送已在内部(按 1&1)分类为的电子邮件 垃圾邮件。
我联系了我的服务提供商,他们不知道这意味着什么,也不知道如何解除阻止。因此,出于良好秩序的原因,我认为检查我的设置和代码以查看在这种情况下我可能做错了什么并尝试修复它是个好主意。虽然这可能不会删除任何黑名单,但它可能有助于防止将来发生。
这是我对 mailkit 的设置,从 appsettings 开始。您会注意到我使用端口 465 通过 SSL 发送数据,这正是我理解的需要。
appsettings.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);
}
}
我的设置是否对我正在尝试做的事情不正确,无论如何我可以改进?被列入黑名单令人沮丧,但我不明白我该如何解决。
解决方法
有时会发生。您可以通过向 spamhaus.org 发送电子邮件 https://www.spamhaus.org/sbl/query/SBL275659
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。