电子邮件在C#中发送远程主机无响应

时间:2013-05-23 08:29:57

标签: c# email smtp

我正在尝试通过C#创建一个电子邮件客户端,但我一直遇到问题而且我不知道如何继续。当我使用gmail和yahoo作为smtp主机时,我没有遇到任何问题。当我使用谷歌发送邮件到公司邮件时,我也没有遇到问题。

每当我使用公司电子邮件地址发送电子邮件时,问题都会出现。您可以在浏览器上处理该电子邮件。

我也没有使用与公司访问互联网分开的私人互联网连接

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net;

namespace email
{
    public partial class Form1 : Form
    {
        SmtpClient client = new SmtpClient();
        MailMessage message = new MailMessage();

        public Form1()
        {
            InitializeComponent();
        }

        private void send_Click(object sender, EventArgs e)
        {

            System.Net.NetworkCredential networkcreds = new System.Net.NetworkCredential("username", "password");

            try
            {
                //setup for smtp host
                client.Host = "mail.company.com";
                client.Port = 25;
                client.UseDefaultCredentials = false;
                client.Credentials = networkcreds;
                client.EnableSsl = true;

                //convert strings to mailaddress
                MailAddress to = new MailAddress("username@company.com");
                MailAddress froms = new MailAddress("username@company.com");

                //set up message settings
                message.Subject = "TEST SUBJECT";
                message.Body = "TEST BODY";
                message.To.Add(to);
                message.From = froms;

                client.Send(message);
                MessageBox.Show("Message Sent");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                MessageBox.Show("Message Not sent");
            }
        }
    }
}

以下是我在try catch中找到的错误:

System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 203.82.36.171:25
   at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
   --- End of inner exception stack trace ---
   at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6)
   at System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback)
   at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback)
   at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
   at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
   at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)
   at System.Net.Mail.SmtpClient.GetConnection()
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   --- End of inner exception stack trace ---
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   at email.Form1.send_Click(Object sender, EventArgs e) in C:\Users\Jake Andrew\documents\visual studio 2010\Projects\email\email\Form1.cs:line 50

2 个答案:

答案 0 :(得分:0)

尝试ping邮件服务器:

ping 203.82.36.171

如果有效,还有其他几个选项需要检查。例如,我的公司不允许每个客户端使用SMTP协议发送邮件 - 询问您是否有权通过SMTP从您的计算机发送邮件。

答案 1 :(得分:0)

尝试使用其他端口。

client.Port = 465;
相关问题