0

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

**.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.";
        }

        }
4

1 回答 1

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/

于 2012-07-02T21:12:06.350 回答