思归,你在吗,救我啊,带星星的哥哥姐姐快来啊(邮件问题 急急急!!!)

Philippy 2003-12-12 11:16:54
我想在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;
}
}
}


请各位指点迷津!!谢谢
...全文
27 10 打赏 收藏 举报
写回复
10 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Philippy 2003-12-13
自己解决了,谢谢各位!!!
  • 打赏
  • 举报
回复
雪狼1234567 2003-12-13
我的mail程序就是参考这几个地方做的,不错
http://www.c-sharpcorner.com/asp/Code/SendMailDCHK.asp
http://www.c-sharpcorner.com/Internet/MailingAppMG.asp
http://www.codeproject.com/csharp/httpmail.asp?target=mail
  • 打赏
  • 举报
回复
雪狼1234567 2003-12-13
第二种方式:
private void btnSend_Click(object sender, System.EventArgs e)
{
// change the cursor to hourglass
Cursor appCursor = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;

string sendString;

byte [] dataToSend;

string receiveData;

try
{
// creating an instance of the TcpClient class
TcpClient smtpServer = new TcpClient(txtSmtpServer.Text, 25);

lstLog.Items.Add("Connection Established with " + txtSmtpServer.Text);

// creating stream classes for communication
NetworkStream writeStream = smtpServer.GetStream();
StreamReader readStream = new StreamReader(smtpServer.GetStream());

// receiving connection success
receiveData = readStream.ReadLine();
lstLog.Items.Add(receiveData);

// sending From Email Address
sendString = "MAIL FROM: " + "<" + txtFrom.Text + ">\r\n";
dataToSend = Encoding.ASCII.GetBytes(sendString);
writeStream.Write(dataToSend,0,dataToSend.Length);

receiveData = readStream.ReadLine();
lstLog.Items.Add(receiveData);

// sending To Email Address
sendString = "RCPT TO: " + "<" + txtTo.Text + ">\r\n";
dataToSend = Encoding.ASCII.GetBytes(sendString);
writeStream.Write(dataToSend,0,dataToSend.Length);

receiveData = readStream.ReadLine();
lstLog.Items.Add(receiveData);

// sending data
sendString = "DATA " + "\r\n";
dataToSend = Encoding.ASCII.GetBytes(sendString);
writeStream.Write(dataToSend,0,dataToSend.Length);

receiveData = readStream.ReadLine();
lstLog.Items.Add(receiveData);

// sending Message Subject and Text
sendString = "SUBJECT: " + txtSubject.Text + "\r\n" + txtMessage.Text + "\r\n" + "." + "\r\n";
dataToSend = Encoding.ASCII.GetBytes(sendString);
writeStream.Write(dataToSend,0,dataToSend.Length);

receiveData = readStream.ReadLine();
lstLog.Items.Add(receiveData);

// sending Disconnect from Server
sendString = "QUIT " + "\r\n";
dataToSend = Encoding.ASCII.GetBytes(sendString);
writeStream.Write(dataToSend,0,dataToSend.Length);

receiveData = readStream.ReadLine();
lstLog.Items.Add(receiveData);

// closing all open resources
writeStream.Close();
readStream.Close();

smtpServer.Close();
}
catch(SocketException se)
{
MessageBox.Show("SocketException:" + se.ToString());
}
catch(Exception excep)
{
MessageBox.Show("Exception:" + excep.ToString());
}

// restoring the cursor state
Cursor.Current = appCursor;

}

private void btnReceive_Click(object sender, System.EventArgs e)
{

// change the cursor to hourglass
Cursor appCursor = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;

string sendString;

byte [] dataToSend;

string receiveData;

try
{
TcpClient popServer = new TcpClient(txtPopServer.Text,110);

NetworkStream writeStream = popServer.GetStream();
StreamReader readStream = new StreamReader(popServer.GetStream());

// connect with the server
receiveData = readStream.ReadLine();
lstLog2.Items.Add(receiveData);

// sending username to the server
sendString = "USER " + txtUserName.Text + "\r\n";
dataToSend = Encoding.ASCII.GetBytes(sendString);
writeStream.Write(dataToSend,0,dataToSend.Length);

receiveData = readStream.ReadLine();
lstLog2.Items.Add(receiveData);

// sending password to the server
sendString = "PASS " + txtPassword.Text + "\r\n";
dataToSend = Encoding.ASCII.GetBytes(sendString);
writeStream.Write(dataToSend,0,dataToSend.Length);

receiveData = readStream.ReadLine();
lstLog2.Items.Add(receiveData);

// getting the number of emails on the server
sendString = "STAT" + "\r\n";
dataToSend = Encoding.ASCII.GetBytes(sendString);
writeStream.Write(dataToSend,0,dataToSend.Length);

receiveData = readStream.ReadLine();
lstLog2.Items.Add(receiveData);

// parse the returned number into integer
Match m = Regex.Match(receiveData,@"(\s\d*\s)");
int nMails = 0;

if (m.Success)
{
nMails = int.Parse(m.ToString());
}

// reading emails in a loop


for (int index = 1; index <= nMails; index++)
{
string szTemp;

// reading individual email
MessageBox.Show("Reading Email:" + index.ToString());
sendString = "RETR " + index.ToString() + "\r\n";
dataToSend = Encoding.ASCII.GetBytes(sendString);
writeStream.Write(dataToSend,0,dataToSend.Length);

receiveData = readStream.ReadLine();
szTemp = receiveData;

while(receiveData != ".")
{
receiveData = readStream.ReadLine();
szTemp = szTemp + "\r\n" + receiveData;
}

txtEmails.Text = txtEmails.Text +
index.ToString() + "\r\n" +
szTemp + "\r\n";
//lstMsgList.Items.Add(receiveData);

}



// Disconnect from the server
sendString = "QUIT" + "\r\n";
dataToSend = Encoding.ASCII.GetBytes(sendString);
writeStream.Write(dataToSend,0,dataToSend.Length);

receiveData = readStream.ReadLine();
lstLog2.Items.Add(receiveData);

// freeing all open resources
writeStream.Close();
readStream.Close();

popServer.Close();
}
catch(SocketException se)
{
MessageBox.Show("SocketException:" + se.ToString());
}
catch(Exception excep)
{
MessageBox.Show("Exception:" + excep.ToString());
}

// restoring the cursor state
Cursor.Current = appCursor;

}
}
}
  • 打赏
  • 举报
回复
雪狼1234567 2003-12-13
以前我在windows程序也可以的,重装了,不知道是不是配置的问题,Windows下面也不可以了,这是ASP.NET下面的代码,帮忙看看是否又问题,谢谢

public void SendEmail(string EmailAddress,string BugDescript)
{
MailMessage newMail=new MailMessage();
newMail.From = "webMaster@Collatus.com";
newMail.To = EmailAddress;
newMail.Subject = "Bug Notify !";
newMail.Body = BugDescript;
newMail.BodyFormat = MailFormat.Html;
SmtpMail.SmtpServer="smtp.163.com";
SmtpMail.Send(newMail);
}

同意楼上的 163的smtp是要验证的,

你可以使用SmtpMail.SmtpServer="localhost";
前题是,启动在你的iis中smtp服务器,在访问选项卡中的,中继,把本地ip添加
  • 打赏
  • 举报
回复
Philippy 2003-12-13
自己顶,怎么没有人说话啊
  • 打赏
  • 举报
回复
Philippy 2003-12-12
to:idiotzeng(白痴)
可以具体详细一点吗?老兄,帮忙具体一点

  • 打赏
  • 举报
回复
idiotzeng 2003-12-12
抄来的
  • 打赏
  • 举报
回复
idiotzeng 2003-12-12
This error is usually related with SMTP server setting. In some cases, it
is the wrong smtp server name or wrong DNS entry. Here I would suggest:
1. Double check the SMTP server is listen on port 25;
2. The SMTP grant permission to the ASP.NET application to send email. The
default security account for ASP.NET application is <ServerName>\ASPNET.
  • 打赏
  • 举报
回复
Philippy 2003-12-12
自己up
  • 打赏
  • 举报
回复
Philippy 2003-12-12
up
  • 打赏
  • 举报
回复
相关推荐
发帖
C#
加入

10.7w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2003-12-12 11:16
社区公告

让您成为最强悍的C#开发者