c# - 尝试发送 SMTP 电子邮件 - 连接被拒绝
问题描述
我正在尝试向 MailTrap.io 发送一封电子邮件,它是一个用于检查电子邮件的免费 smtp 门户,但我收到以下错误。
我正在使用 MailKit 发送电子邮件
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);
}
}
解决方案
推荐阅读
- git - How to tell the date time that a git branch is pushed to remote?
- javascript - DayJS 格式秒分钟和小时
- python-3.x - 在 Visual Studio 代码中调试 python 库
- jsf - PrimeFaces ImageCropper 显示裁剪区域尺寸
- javascript - 是否可以控制 HTML Razor 组件的渲染顺序?
- r - 使用 R 遍历来自 csv 的 URL 列表,打开 URL,并评估这些站点是否包含某个文本字符串
- groovy - 用户一直在说“十二三十四”而不是“一二三四”
- react-native - componentWillReceiveProps 被调用不在当前屏幕反应原生
- python - NameError 提示玩家输入猜测后
- sql - 更新语句在预填充的列上生成空违规