无法连接到C#mail程序中的本地服务器

无法连接到C#mail程序中的本地服务器,c#,visual-studio,email,smtp,C#,Visual Studio,Email,Smtp,我无法使用以下代码连接到frontier邮件服务器。我收到消息“无法连接到远程服务器”。我正在本地计算机上使用C#运行该程序 try { MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient("smtp.frontier.com"); mail.From = new Mail

我无法使用以下代码连接到frontier邮件服务器。我收到消息“无法连接到远程服务器”。我正在本地计算机上使用C#运行该程序

         try
          {
              MailMessage mail = new MailMessage();
              SmtpClient SmtpServer = new SmtpClient("smtp.frontier.com");

              mail.From = new MailAddress(emailaddress);
              mail.To.Add("xxxx@frontier.com");
              mail.Subject = thistitle;
              mail.Body = thisdescription;

              System.Net.Mail.Attachment attachment;
              attachment = new System.Net.Mail.Attachment(thisimage);
              mail.Attachments.Add(attachment);

              SmtpServer.Port = 25;
              SmtpServer.Credentials = new System.Net.NetworkCredential("username", "xxxxxxx");
              SmtpServer.EnableSsl = true;

              SmtpServer.Send(mail);
              MessageBox.Show("Mail sent");
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.ToString(), "Email Error Message");
          }

有人能告诉我边境邮件的参数是否正确吗?我知道他们使用雅虎,但我也尝试过,但没有成功。难道不能从我的本地计算机上运行邮件服务器吗?非常感谢您的帮助。

请尝试删除此代码
SmtpServer.EnableSsl=true

只需尝试删除此代码
SmtpServer.EnableSsl=true

您的ISP是否阻止SMTP通信?(非商业账户通常如此)

如果不是。。。重新编写代码,使其更接近于此:

    try
    {
        using (var attachment = new Attachment(thisimage))
        using (var mail = new MailMessage())
        {
            mail.From = new MailAddress(emailaddress);
            mail.To.Add("xxxx@frontier.com");
            mail.Subject = thistitle;
            mail.Body = thisdescription;
            mail.Attachments.Add(attachment);

            using (var client = new SmtpClient("smtp.frontier.com"))
            {
                client.Port = 25;
                client.Credentials = new System.Net.NetworkCredential("username", "xxxxxxx");
                client.EnableSsl = true;
                client.Send(mail);
            }
        }

        MessageBox.Show("Mail sent");
    }
    catch (SmtpException ex)
    {
        // go read through https://msdn.microsoft.com/en-us/library/swas0fwc(v=vs.110).aspx
        // go read through https://msdn.microsoft.com/en-us/library/system.net.mail.smtpexception(v=vs.110).aspx
    }
#if DEBUG
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString(), "Email Error Message");
    }
#endif
}

…并在调试器中运行它,查看SmtpException报告的内容。连接失败的原因有很多。

您的ISP是否阻止SMTP通信?(非商业账户通常如此)

如果不是。。。重新编写代码,使其更接近于此:

    try
    {
        using (var attachment = new Attachment(thisimage))
        using (var mail = new MailMessage())
        {
            mail.From = new MailAddress(emailaddress);
            mail.To.Add("xxxx@frontier.com");
            mail.Subject = thistitle;
            mail.Body = thisdescription;
            mail.Attachments.Add(attachment);

            using (var client = new SmtpClient("smtp.frontier.com"))
            {
                client.Port = 25;
                client.Credentials = new System.Net.NetworkCredential("username", "xxxxxxx");
                client.EnableSsl = true;
                client.Send(mail);
            }
        }

        MessageBox.Show("Mail sent");
    }
    catch (SmtpException ex)
    {
        // go read through https://msdn.microsoft.com/en-us/library/swas0fwc(v=vs.110).aspx
        // go read through https://msdn.microsoft.com/en-us/library/system.net.mail.smtpexception(v=vs.110).aspx
    }
#if DEBUG
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString(), "Email Error Message");
    }
#endif
}

…并在调试器中运行它,查看SmtpException报告的内容。连接失败的原因有很多。

我无法发表评论,因此我将键入我的评论作为答案。您是否可以使用ImapClient而不是SmtpClient?使用Imap,您可以执行一些身份验证过程。可能是问题所在,只是看起来您正在登录。对于Imap,我执行以下操作:

 using (var clientTest = new ImapClient())
 {
    clientTest.Connect("xxxx@frontier.com");
    clientTest.AuthenticationMechanisms.Remove("XOAUTH");
    clientTest.Authenticate(eMail, pw);
    bIsConnected = clientTest.IsConnected;

     if (bIsConnected == true)
     {
        /// Insert Code here
     }
 }

我无法发表评论,因此我将键入我的评论作为答案。您是否可以使用ImapClient而不是SmtpClient?使用Imap,您可以执行一些身份验证过程。可能是问题所在,只是看起来您正在登录。对于Imap,我执行以下操作:

 using (var clientTest = new ImapClient())
 {
    clientTest.Connect("xxxx@frontier.com");
    clientTest.AuthenticationMechanisms.Remove("XOAUTH");
    clientTest.Authenticate(eMail, pw);
    bIsConnected = clientTest.IsConnected;

     if (bIsConnected == true)
     {
        /// Insert Code here
     }
 }

他们必须响应不同的消息,如果响应的某个消息意味着是同一个exception。您可以检查一下端口25上的internet访问主机smtp.frontier.com吗?您可以使用
telnet
客户端进行尝试,使用此命令“telnet-open smtp.frontier.com 25”如果未响应此消息,如
220 relay03.roch.ny.frontiernet.net ESMTP Postfix
,这意味着您无法访问端口25上的stmp.frontier.com。如果响应的某个消息是同一个exction,则必须响应不同的消息。您可以检查一下端口25上的internet访问主机smtp.frontier.com吗?您可以使用
telnet
客户端进行尝试,使用此命令“telnet-open smtp.frontier.com 25”如果未响应此消息,如
220 relay03.roch.ny.frontiernet.net ESMTP Postfix
,这意味着您无法访问端口25上的stmp.frontier.com。我收到上述代码的以下错误:message“System.Net.Mail.SmtpException:发送邮件失败。-->System.Net.WebException:无法连接到远程服务器-->System.Net.Sockets.SocketException:连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机未能响应199.224.64.207:25\r\n如何确定我的ISP是否阻止SMPT通信?我使用端口587再次运行该连接并禁用了EnableSs1,并收到消息“发件人地址被拒绝:访问被拒绝”。我收到了以下错误和上述代码:message“System.Net.Mail.SmtpException:发送邮件失败。-->System.Net.WebException:无法连接到远程服务器-->System.Net.Sockets.SocketException:连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机未能响应199.224.64.207:25\r\n如何确定我的ISP是否阻止SMPT通信?我使用端口587再次运行该连接,并禁用了EnableSs1,并收到消息“发件人地址已拒绝:拒绝访问”。