.Net SMTP客户端无法与Linux上的postfix smtp服务器协同工作。

问题描述 投票:0回答:1

我使用的是.Net Core 3.1,下面是我的简单代码。

static async System.Threading.Tasks.Task Main(string[] args)
{
    var smtpClient = new SmtpClient()
    {
        Host = "leadsearch.localdomain",
        Port = 25,
        EnableSsl = true,
        UseDefaultCredentials = true
    };
    var msg = new MailMessage("[email protected]", "[email protected]");
    msg.Subject = "Sent from Abdul Moiz";
    msg.Body = "<h1>Moiz Body of msg</h1>";
    msg.IsBodyHtml = true;
    await smtpClient.SendMailAsync(msg);
    Console.WriteLine("Done!!!");
}

然而,我的postfix配置正确,并且正在发送邮件,我使用下面的命令进行了测试。

echo "This is the body of the email" | mail -s "This is the subject line" [email protected]

然后,我进入我的收件箱,发现邮件在垃圾邮件文件夹中,我试着从终端运行它的各种时间,它的工作完全正常,从终端。但是当我尝试使用上面给出的c#代码发送时,我得到了这个错误。

Unhandled exception. System.Net.Mail.SmtpException: Failure sending mail.
 ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (00000005, 0xFFFDFFFF): Name or service not known
   at System.Net.Dns.InternalGetHostByName(String hostName)
   at System.Net.Dns.ResolveCallback(Object context)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(Exception source)
   at System.Net.Dns.HostResolutionEndHelper(IAsyncResult asyncResult)
   at System.Net.Dns.EndGetHostAddresses(IAsyncResult asyncResult)
   at System.Net.Sockets.Socket.DoDnsCallback(IAsyncResult result, MultipleAddressConnectAsyncResult context)
   at System.Net.Sockets.Socket.DnsCallback(IAsyncResult result)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(Exception source)
   at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
   at System.Net.Sockets.TcpClient.EndConnect(IAsyncResult asyncResult)
   at System.Net.Mail.SmtpConnection.ConnectAndHandshakeAsyncResult.InitializeConnectionCallback(IAsyncResult result)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(Exception source)
   at System.Net.Mail.SmtpConnection.ConnectAndHandshakeAsyncResult.End(IAsyncResult result)
   at System.Net.Mail.SmtpTransport.EndGetConnection(IAsyncResult result)
   at System.Net.Mail.SmtpClient.ConnectCallback(IAsyncResult result)
   --- End of inner exception stack trace ---
   at MailSenderConsoleApp.Program.Main(String[] args) in C:\Users\moiz.baig\source\repos\MailSenderConsoleApp\MailSenderConsoleApp\Program.cs:line 25
   at MailSenderConsoleApp.Program.<Main>(String[] args)
Aborted (core dumped)

当我检查我的localhost SMTP服务器的主机名时,如上所述 此处 我得到了以下结果

postconf -d myhostname
myhostname = leadsearch.localdomain

我的Linux操作系统的细节,我的构建是部署在哪里。

Distributor ID: Ubuntu
Description:    Ubuntu 18.04.3 LTS
Release:        18.04
Codename:       bionic

我不知道我做错了什么,但这根本行不通,求你了

c# linux .net-core smtp
1个回答
0
投票

你的错误输出说 at System.Net.Dns.InternalGetHostByName(String hostName).

这意味着你运行这段代码的机器不知道什么IP地址对应于 铅搜索.本地域名. 当然在linux上是这样的,它是自己。

所以有两个选择。配置一个DNS来响应leadsearch.localdomain的正确地址,或者在你的本地主机文件中定义它。

你的本地主机文件在 C:\Windows\System32\drivers\etc\hosts

© www.soinside.com 2019 - 2024. All rights reserved.