繁体   English   中英

为什么我的 IP 现在被阻止通过 Mailkit 从托管在 Z3A580FZB4A39EFAB23731C7EBCZ 的 ASP.NET 核心应用程序中发送 email?

[英]Why is my IP now blocked from sending email via Mailkit from my ASP.NET Core application hosted in Azure?

在我的 ASP.NET Core web 中,我为我的用户使用了身份验证。 他们可以重置密码,并在创建帐户时收到确认电子邮件。 为此,我使用Mailkit 当我在本地运行我的应用程序时,它工作正常并且可以毫无问题地发送电子邮件,当我将它部署到 Azure 时它曾经可以工作,但有一天它停止了。

然后我收到了 email 失败的交货收据,说明如下:

someuser@hotmail.co.uk: SMTP error from remote server for MAIL FROM command, host: eur.olc.protection.outlook.com (104.47.12.33) reason: 550 5.7.1 Service unavailable, Client host [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 发送,这是我理解的需要。

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来删除您的 ip

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM