通过 C# 发送电子邮件时出现内部异常,端口 25 没有进程 运行

Inner Exception given during sending email Via C#, Port 25 has no Process running

我正在尝试通过 C#.NET code 发送电子邮件,但出现内部异常:

An attempt was made to access a socket in a way forbidden by its access permissions

发送电子邮件的代码是,请告知我有什么问题(代码将 运行 在我们自己网络下的本地域中):

MailMessage mail = new MailMessage("email1@yahoo.com","email2@gmail.com"); 
smtpClient client = new SmtpClient(); 
client.Port = 25; 
client.DeliveryMethod = SmtpDeliveryMethod.Network; 
client.UseDefaultCredentials = false; 
client.Host = "SecretIP of SMPT Server OR IP"; 
mail.Subject = "This is a test email."; 
mail.Body = "This is my test email body"; 
bool mailSent = false; 
bool serverListening = false; 

try 
{ 
    Ping p = new Ping(); 
    PingReply pr = p.Send(client.Host, 5000); 
    if(pr.Status == IPStatus.Success) 
    { 
        serverListening = !(serverListening); 
        client.Send(email); 
        emailSent = !(emailSent); 
    } 
}
catch (Exception ex)
{
    Exception ex2 = ex;
    string errorMessage = string.Empty;
    while (ex2 != null)
    {
        errorMessage += ex2.ToString();
        ex2 = ex2.InnerException;
    }
}

第 1 步:

首先,从 cmd.exe

ping 你的 email server's IP/Alias-Name

在你的情况下 email-server 的 IP 是 SecretIP of SMPT Server OR IP
(即 client.Host 的值)
所以你的命令将是:

ping IP
//OR
ping Alias Name

第 2 步:

既然您已确认 IP 是否被成功 ping 成功,请按照以下说明操作:

  • 如果服务器 IP 未被 ping 通,那么您应该咨询您的内部 IT 部门团队并询问安装 Microsoft Exchange Server 的服务器 IP。
  • 如果服务器 IP 被 ping 成功,则意味着您正在 ping 的 IP 可以访问,但是
    • Microsoft Server Exchange Server 未安装在 server/host 上并且您在 client.Host 行中使用的
    • 由于此 IP 上未安装 Microsoft Exchange Server,原因是 mail-failure.
    • 向 IT 部门询问正确的 IP 或者如果您在 Outlook 中为您自己的域配置了电子邮件,则转到 Outlook - 文件 - 信息 - 帐户设置 - E-mail TAB.... 双击它并复制别名。此别名是拥有您的电子邮件的服务器,并对您的电子邮件进行大量处理:-)

帮助说明:

访问 Microsoft Exchange Server Link 了解更多信息。