尝试发送SMTP电子邮件-连接被拒绝

时间:2020-05-19 15:35:39

标签: c# asp.net-core

我正在尝试向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);
        }
}

0 个答案:

没有答案