簡體   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