思归,你在吗,救我啊,带星星的哥哥姐姐快来啊(邮件问题 急急急!!!)
我想在Aspnet中发邮件,Smtp服务是缺省本机,老提示错误,
错误提示如下:
Server Error in '/WebSendMail' Application.
--------------------------------------------------------------------------------
The transport failed to connect to the server.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Runtime.InteropServices.COMException: The transport failed to connect to the server.
Source Error:
The source code that generated this unhandled exception can only be shown when compiled in debug mode. To enable this, please follow one of the below steps, then request the URL:
1. Add a "Debug=true" directive at the top of the file that generated the error. Example:
<%@ Page Language="C#" Debug="true" %>
or:
2) Add the following section to the configuration file of your application:
<configuration>
<system.web>
<compilation debug="true"/>
</system.web>
</configuration>
Note that this second technique will cause all files within a given application to be compiled in debug mode. The first technique will cause only that particular file to be compiled in debug mode.
Important: Running applications in debug mode does incur a memory/performance overhead. You should make sure that an application has debugging disabled before deploying into production scenario.
我的代码:
------------------------------------------
<%@ Import Namespace="System.Web.Mail" %>
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebSendMail.WebForm1" Debug="true" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<H3>Email From ASP.NET</H3>
<form id="Form1" method="post" runat="server">
<asp:Label id="Label1" style="LEFT: 100px; POSITION: absolute; TOP: 100px" runat="server">From: </asp:Label>
<asp:TextBox id="txtFrom" style="LEFT: 200px; POSITION: absolute; TOP: 100px" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator id="FromValidator1" style="LEFT: 100px; POSITION: absolute; TOP: 375px" runat="server" ErrorMessage="Please Enter the Email From." Width="200px" Height="23px" ControlToValidate="txtFrom"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator id="FromValidator2" style="LEFT: 100px; POSITION: absolute; TOP: 400px" runat="server" ErrorMessage="Please Enter a Valid From Email address" ControlToValidate="txtFrom" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
<asp:Label id="Label2" style="LEFT: 100px; POSITION: absolute; TOP: 125px" runat="server">To:
</asp:Label>
<asp:TextBox id="txtTo" style="LEFT: 200px; POSITION: absolute; TOP: 125px" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator id="ToValidator1" style="LEFT: 100px; POSITION: absolute; TOP: 425px" runat="server" ErrorMessage="Please Enter the Email To." Width="200px" Height="23px" ControlToValidate="txtTo"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator id="ToValidator2" style="LEFT: 100px; POSITION: absolute; TOP: 450px" runat="server" ErrorMessage="Please Enter a Valid To Email address" ControlToValidate="txtTo" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
<asp:Label id="Label3" style="LEFT: 100px; POSITION: absolute; TOP: 150px" runat="server">Subject</asp:Label>
<asp:TextBox id="txtSubject" style="LEFT: 200px; POSITION: absolute; TOP: 150px" runat="server"></asp:TextBox>
<asp:Label id="Label4" style="LEFT: 100px; POSITION: absolute; TOP: 175px" runat="server">Mail: </asp:Label>
<TEXTAREA runat="server" id="txtContent" style="LEFT: 200px; WIDTH: 400px; POSITION: absolute; TOP: 175px; HEIGHT: 125px" rows="7" cols="24" NAME="txtContent">
</TEXTAREA>
<asp:Button id="btnSend" style="LEFT: 200px; POSITION: absolute; TOP: 350px" runat="server" Text="Send" onClick="btnSend_Click"></asp:Button>
<asp:Label id="lblStatus" style="LEFT: 250px; POSITION: absolute; TOP: 350px" runat="server"></asp:Label>
</form>
</body>
</HTML>
<script language="c#" runat="server">
private void btnSend_Click(object sender, System.EventArgs e)
{
MailMessage msg = new MailMessage();
msg.To = txtTo.Text;
msg.From = txtFrom.Text;
msg.Subject = txtSubject.Text;
msg.Body = txtContent.Value;
lblStatus.Text = "Sending...";
SmtpMail.SmtpServer = "localhost" ;
SmtpMail.Send(msg);
lblStatus.Text = "Sent email (" + txtSubject.Text + ") to " +txtTo.Text;
}
</script>
--------------C# code
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Mail;
namespace WebSendMail
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.TextBox txtFrom;
protected System.Web.UI.WebControls.RequiredFieldValidator FromValidator1;
protected System.Web.UI.WebControls.RegularExpressionValidator FromValidator2;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.TextBox txtTo;
protected System.Web.UI.WebControls.RequiredFieldValidator ToValidator1;
protected System.Web.UI.WebControls.RegularExpressionValidator ToValidator2;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.TextBox txtSubject;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.Button btnSend;
protected System.Web.UI.WebControls.Label lblStatus;
protected System.Web.UI.HtmlControls.HtmlTextArea txtContent;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnSend_Click(object sender, System.EventArgs e)
{
MailMessage msg = new MailMessage();
msg.To = txtTo.Text;
msg.From = txtFrom.Text;
msg.Subject = txtSubject.Text;
msg.Body = txtContent.Value;
lblStatus.Text = "Sending...";
SmtpMail.SmtpServer = "localhost" ;
SmtpMail.Send(msg);
lblStatus.Text = "Sent email (" + txtSubject.Text + ") to " + txtTo.Text;
}
}
}
请各位指点迷津!!谢谢