首页 > 解决方案 > 尝试发送 SMTP 电子邮件 - 连接被拒绝

问题描述

我正在尝试向 MailTrap.io 发送一封电子邮件,它是一个用于检查电子邮件的免费 smtp 门户,但我收到以下错误。

我正在使用 MailKit 发送电子邮件

https://mailtrap.io

 public async Task SendEmailAsync(string email, string subject, string message)
 {
        try
        {
            var emailMessage = new MimeMessage();

            emailMessage.From.Add(new MailboxAddress("David", "davidnoreply@gmail.com"));
            emailMessage.To.Add(new MailboxAddress("", email));
            emailMessage.Subject = subject;
            emailMessage.Body = new TextPart("HTML") { Text = message };

            using (var client = new SmtpClient())
            {
                client.LocalDomain = "http://localhost:52101";
                await client.ConnectAsync("smtp.mailtrap.io", 584, true);
                client.Authenticate("16729fdfa173b4", "0e6de61c67b069");
                client.Send(emailMessage);
                //await client.emailMessage(emailMessage).ConfigureAwait(false);
                //await client.DisconnectAsync(true).ConfigureAwait(false);
            }

        }
        catch (Exception ex)
        {
            // TODO: handle exception
            throw new InvalidOperationException(ex.Message);
        }
    }

但是我在端口 25 上尝试过以下错误,但随后出现关于 ssl 的错误。

由于目标机器主动拒绝,无法建立连接。127.0.0.1:25

If I change it to port 25 I get the following.

An error occurred while attempting to establish an SSL or TLS connection.

This usually means that the SSL certificate presented by the server is not trusted by the system for one or more of
the following reasons:

1. The server is using a self-signed certificate which cannot be verified.
2. The local system is missing a Root or Intermediate certificate needed to verify the server's certificate.
3. A Certificate Authority CRL server for one or more of the certificates in the chain is temporarily unavailable.
4. The certificate presented by the server is expired or invalid.

Another possibility is that you are trying to connect to a port which does not support SSL/TLS.

It is also possible that the set of SSL/TLS protocols supported by the client and server do not match.

See https://github.com/jstedfast/MailKit/blob/master/FAQ.md#SslHandshakeException for possible solutions.

编辑 2

我删除了 ssl 但仍然出现上述相同的错误。

public async Task SendEmailAsync(string email, string subject, string message)
{
        try
        {
            var emailMessage = new MimeMessage();

            emailMessage.From.Add(new MailboxAddress("David", "david@gmail.com"));
            emailMessage.To.Add(new MailboxAddress("", email));
            emailMessage.Subject = subject;
            emailMessage.Body = new TextPart("HTML") { Text = message };

            using (var client = new SmtpClient())
            {

                await client.ConnectAsync("smtp.mailtrap.io", 25, true);
                client.Authenticate("16729fdfa173b4", "0e6de61c67b069");
                client.Send(emailMessage);
                //await client.emailMessage(emailMessage).ConfigureAwait(false);
                //await client.DisconnectAsync(true).ConfigureAwait(false);
            }

        }
        catch (Exception ex)
        {
            // TODO: handle exception
            throw new InvalidOperationException(ex.Message);
        }
}

标签: c#asp.net-core

解决方案


推荐阅读