无法从传输层读取数据(内部异常)

时间:2012-07-02 19:50:07

标签: .net email

我通过gmail帐户发送电子邮件。但是当我运行程序时,发生了smtp异常。当我发送时    打开详细信息.it显示innerexception - “{”无法从传输连接中读取数据:    远程主机强行关闭现有连接。“}”

**.aspx code:**
     your Email assdress:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

   <asp:Label ID="Label3" runat="server" Text="Labe3"></asp:Label>
   <asp:button ID="Button1" runat="server" text="Button" OnClick="button1_click" />
     **.aspx.cs**
        public void button1_click(object sender, EventArgs e)
        {

             SmtpClient smtpClient = new SmtpClient();
        MailMessage message = new MailMessage();
        MailAddress fromAddress = new MailAddress(TextBox1.Text);

            // You can specify the host name or ipaddress of your server
            // Default in IIS will be localhost 
            smtpClient.Host = "smtp.gmail.com";
            smtpClient.EnableSsl = true;
            //Default port will be 25
            smtpClient.Port = 25;

            //From address will be given as a MailAddress Object
            message.From = fromAddress;

            // To address collection of MailAddress
            message.To.Add("irfan.dilwaley@gmail.com");
            message.Subject = "Feedback";

            // CC and BCC optional
            // MailAddressCollection class is used to send the email to various users
            // You can specify Address as new MailAddress("admin1@yoursite.com")


            // You can specify Address directly as string

            //Body can be Html or text format
            //Specify true if it  is html message
            message.IsBodyHtml = false;

            // Message body content


            // Send SMTP mail
            smtpClient.Send(message);

          Label3.Text = "Email successfully sent.";
        }

        }

1 个答案:

答案 0 :(得分:0)

我看不到您使用凭证可能是问题

// Credentials are necessary if the server requires the client 
// to authenticate before it will send e-mail on the client's behalf.
smtpClient.UseDefaultCredentials = true;

OR

smtpClient.Credentials = CredentialCache.DefaultNetworkCredentials;

确保SMTP服务器配置正确的另一个有用的方法 关于链接的好例子:http://codebetter.com/petervanooijen/2006/04/05/using-localhost-as-mailserver-5-7-1-unable-to-relay-for-xxx/