简体   繁体   English

通过asp.net发送电子邮件

[英]Sending email through asp.net

This is my Design page : 这是我的设计页面:

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

this is my code page: 这是我的代码页:

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

Service not available, closing transmission channel. 服务不可用,关闭传输通道。 The server response was: Cannot connect to SMTP server 100.64.243.189 (100.64.243.189:25), connect error 10061 Description: An unhandled exception occurred during the execution of the current web request. 服务器响应为:无法连接到SMTP服务器100.64.243.189(100.64.243.189:25),连接错误10061说明:执行当前Web请求期间发生未处理的异常。 Please review the stack trace for more information about the error and where it originated in the code. 请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。 Exception Details: System.Net.Mail.SmtpException: Service not available, closing transmission channel. 异常详细信息:System.Net.Mail.SmtpException:服务不可用,关闭传输通道。 The server response was: Cannot connect to SMTP server 100.64.243.189 (100.64.243.189:25), connect error 10061 服务器响应为:无法连接到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:
  1. Make sure that you have a smtp server setup. 确保您具有smtp服务器设置。 add smtp server info in web.config. 在web.config中添加smtp服务器信息。 then try this. 然后尝试这个。

you need to mention the smtp server url and the port number. 你需要提到smtp服务器url和端口号。 SMTp SMTP

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

then now call obj.send . 然后现在调用obj.send

There may be two cause: 可能有两个原因:

  1. This usually results from trying to connect to a service that is inactive on the foreign host. 这通常是因为尝试连接到在外部主机上处于非活动状态的服务。

  2. Sometimes a 10061 error is caused by either a firewall or anti-virus software presence on the local computer or network connection. 有时,本地计算机或网络连接上的防火墙或防病毒软件存在会导致10061错误。

Either one may be blocking the ports needed to make a successful connection to the server. 任何一个都可能阻塞成功连接到服务器所需的端口。 Either you went to the wrong host, or the server application you're trying to contact is not executing. 要么你去了错误的主机,要么你正在尝试联系的服务器应用程序没有执行。 Check the destination address you are using. 检查您使用的目标地址。 Check, if you used a hostname, did it resolve to the correct address or not.. 如果您使用了主机名,请检查是否已解析为正确的地址。

Make sure to set up smtp on web config file 确保在Web配置文件上设置smtp

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

You can apply SMTP settings in web.config by putting this section inside: 您可以通过将此部分放在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>

This example shows configuration for Gmail SMTP server. 此示例显示Gmail SMTP服务器的配置。

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);

You have to give credentials.. check the below code once 您必须提供凭据..请检查以下代码一次

        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);

You have to add below namespaces 您必须添加以下命名空间

using System.Net.Mail;
using System.Net;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM