C# 为什么现在阻止我的IP从Azure中托管的ASP.NET核心应用程序通过Mailkit发送电子邮件?

C# 为什么现在阻止我的IP从Azure中托管的ASP.NET核心应用程序通过Mailkit发送电子邮件?,c#,asp.net,azure,mailkit,C#,Asp.net,Azure,Mailkit,在我的ASP.NET核心网站中,我对我的用户使用了身份验证。他们可以重置密码,并在创建帐户时接收确认电子邮件。为了实现这一点,我使用了。当我在本地运行我的应用程序时,它运行良好,可以毫无问题地发送电子邮件。当我将它部署到Azure时,它曾经工作过,但有一天停止了 然后,我收到了一封电子邮件,说明以下内容: someuser@hotmail.co.uk:来自邮件的远程服务器的SMTP错误 来自命令,主机:eur.olc.protection.outlook.com(104.47.12.33) 原因

在我的ASP.NET核心网站中,我对我的用户使用了身份验证。他们可以重置密码,并在创建帐户时接收确认电子邮件。为了实现这一点,我使用了。当我在本地运行我的应用程序时,它运行良好,可以毫无问题地发送电子邮件。当我将它部署到Azure时,它曾经工作过,但有一天停止了

然后,我收到了一封电子邮件,说明以下内容:

someuser@hotmail.co.uk:来自邮件的远程服务器的SMTP错误 来自命令,主机:eur.olc.protection.outlook.com(104.47.12.33) 原因:550 5.7.1服务不可用,客户端主机[82.165.159.37] 使用S pamhaus阻止。要请求从此列表中删除,请参阅 g/query/ip/82.165.159.37(AS3130)。 [DB3EUR04FT052.eop-eur04.prod.protection.outlook.com]

所以,我的申请被列入了黑名单。理由是:

本SBL中列出的IP地址仅由1&1用于 发送内部(由1&1)分类为 垃圾邮件

我联系了我的服务提供商,他们不知道这意味着什么,也不知道如何解除任何封锁。因此,出于良好秩序的原因,我认为最好检查一下我的设置和代码,看看在这种情况下我可能做错了什么,并尝试修复它。虽然这可能不会删除任何黑名单,但可能有助于防止将来出现这种情况

这是我的mailkit设置,从appsettings开始。您会注意到,我使用端口465通过SSL进行发送,我知道这是需要的

appsettings.json

区域/页面/帐户/管理/放弃密码.cshtml.cs


我的设置是否不适合我正在尝试做的事情,并且我是否可以在这方面进行改进?被列入黑名单是令人沮丧的,但我不明白我如何才能摆脱它。

有时会发生这种情况。您可以通过向spamhaus.org发送电子邮件来删除您的ip,问题不会是您的代码或SmtpClient设置出错。问题很可能是一些垃圾邮件算法决定了你的电子邮件与看起来像垃圾邮件的文本模式相匹配。
{
  "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": "*"
}
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();
}
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);
        }

    }