0

这是我的设计页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="register.aspx.cs" Inherits="register" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table style="border:1px solid" align="center">
    <tr><td>Username:</td><td><asp:TextBox runat="server" ID="txtUsername"></td></tr>
    <tr><td>Password:</td><td><asp:TextBox runat="server" ID="txtPassword" TextMode="Password"></td></tr>
    <tr><td valign="top">Address:</td><td><asp:TextBox runat="server" ID="txtAdress" TextMode="MultiLine"></td></tr>
    <tr><td>Email-id:</td><td><asp:TextBox runat="server" ID="txtEmail"></td></tr>
    <tr><td colspan="2" align="center"><asp:Button runat="server" ID="btnRegister" 
            Text="Register" onclick="btnRegister_Click" /></td></tr>
    </table>

    </div>
    </form>
</body>
</html>

这是我的代码页:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

public partial class register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnRegister_Click(object sender, EventArgs e)
    {
        MailMessage msg = new MailMessage();
        msg.To.Add(txtEmail.Text);
        msg.From = new MailAddress("mysiteadmin@peers");
        msg.Subject="thanks for registraion";
        msg.Body="thanks for registraion";
        SmtpClient obj = new SmtpClient();
        obj.Host = "sri";obj.Send(msg);
        Response.Write("<h2>Registered</h2>");

    }

}

Here am getting an error below.... can you please help

服务不可用,正在关闭传输通道。服务器响应为:无法连接到 SMTP 服务器 100.64.243.189 (100.64.243.189:25),连接错误 10061 说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。异常详细信息:System.Net.Mail.SmtpException:服务不可用,正在关闭传输通道。服务器响应为:无法连接到 SMTP 服务器 100.64.243.189 (100.64.243.189:25),连接错误 10061

Source Error: 
Line 21:         msg.Body="thanks for registraion";
Line 22:         SmtpClient obj = new SmtpClient();
Line 23:         obj.Host = "sri";obj.Send(msg);
Line 24:         Response.Write("<h2>Registered</h2>");
Line 25:
4

6 回答 6

1
  1. 确保您有一个 smtp 服务器设置。在 web.config 中添加 smtp 服务器信息。然后试试这个。

您需要提及 smtp 服务器 url 和端口号。表面贴装

   SmtpClient obj = new SmtpClient(yourSmtpServerURL);
 NetworkCredential myCreds = new NetworkCredential("youremail@gmail.com", "YourPassword", "");           
            obj.Credentials = myCreds;

然后现在打电话obj.send

于 2013-03-04T09:26:12.663 回答
1

可能有两个原因:

  1. 这通常是由于尝试连接到在外部主机上不活动的服务造成的。

  2. 有时 10061 错误是由本地计算机或网络连接上存在防火墙或防病毒软件引起的。

任何一个都可能阻止成功连接到服务器所需的端口。要么您访问了错误的主机,要么您尝试联系的服务器应用程序没有执行。检查您使用的目标地址。检查,如果您使用了主机名,它是否解析为正确的地址..

于 2013-03-04T09:30:17.773 回答
0

确保在 Web 配置文件上设置 smtp

<system.net>
<mailSettings>
  <smtp>
    <network host="smtp@server" port="25" userName="user" password="password" defaultCredentials="true" />
  </smtp>
</mailSettings>
 </system.net>
于 2013-03-04T09:27:39.817 回答
0

您可以在 web.config 中应用 SMTP 设置,方法是将此部分放入:

    <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="...">
        <network host="smtp.gmail.com" port="587" defaultCredentials="false" userName="" password="" enableSsl="true" />
      </smtp>
    </mailSettings>
  </system.net>

此示例显示 Gmail SMTP 服务器的配置。

于 2013-03-04T09:29:24.890 回答
0
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.Subject = "Title";
message.From = new System.Net.Mail.MailAddress(yourEmailExpeditor);
message.Body = "content message here";

//Initialize the smtp
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(adress,int.Parse(port));
//Send the message
smtp.Send(message);
于 2013-03-04T09:29:53.357 回答
0

您必须提供凭据..检查以下代码一次

        MailAddress to = new MailAddress("Email Id");

        MailAddress from = new MailAddress("Email Id");

        MailMessage mail = new MailMessage(from, to);

        mail.Subject = "";
        mail.Body = "";


        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;

        smtp.Credentials = new NetworkCredential(
            "Email Id", "Password");
        smtp.EnableSsl = true;

        smtp.Send(mail);

您必须添加以下命名空间

using System.Net.Mail;
using System.Net;
于 2013-03-04T09:39:14.233 回答