通过asp.net发送电子邮件

时间:2013-03-04 09:23:03

标签: c# asp.net email

这是我的设计页面:

<%@ 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   server 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:

6 个答案:

答案 0 :(得分:1)

  1. 确保您已安装smtp服务器。在web.config中添加smtp服务器信息。然后尝试这个。
  2. 您需要提及smtp服务器URL和端口号。 SMTp

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

    然后现在拨打obj.send

答案 1 :(得分:1)

可能有两个原因:

  1. 这通常是因为尝试连接到的服务 在外国主机上不活跃。

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

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

答案 2 :(得分:0)

确保在网络配置文件

上设置smtp
<system.net>
<mailSettings>
  <smtp>
    <network host="smtp@server" port="25" userName="user" password="password" defaultCredentials="true" />
  </smtp>
</mailSettings>
 </system.net>

答案 3 :(得分: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服务器的配置。

答案 4 :(得分: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);

答案 5 :(得分: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;