using System.Web.Mail;...
pulbic class MyEmailService
{
.....
public void SendMail(string from, string to, string bcc, string subject, string body)
{
MailMessage mail=new MailMessage();
mail.From=from;
mail.To=to;
if( bcc!=string.Empty)
mail.Bcc=bcc;
mail.Subject=subject;
mail.Body=body;
SmtpMail.SmtpServer=smtpServer; //smtpServer是你的smtp的邮件服务器地址
try
{
SmtpMail.Send(mail);
}
catch
{
//配置错误处理
}
}
...
}//以上代码是我项目中的片断,供参考
pulbic class MyEmailService
{
.....
public void SendMail(string from, string to, string bcc, string subject, string body)
{
MailMessage mail=new MailMessage();
mail.From=from;
mail.To=to;
if( bcc!=string.Empty)
mail.Bcc=bcc;
mail.Subject=subject;
mail.Body=body;
SmtpMail.SmtpServer=smtpServer; //smtpServer是你的smtp的邮件服务器地址
try
{
SmtpMail.Send(mail);
}
catch
{
//配置错误处理
}
}
...
}//以上代码是我项目中的片断,供参考
解决方案 »
- C#:结束受保护的外部软件 ---- (在线等)
- 请教下两段同样功能的程序效果不同,哪里不一样
- [C高手指点] C头文件中的typedef 在C#中如何表达
- 如何控制粘贴到freetextbox编辑器的图片的宽度呢!
- 有代码(短)~~~求修改```!!
- Update 无法找到 TableMapping['Table'] 或 DataTable“Table”
- 请问哪里可以下载Visio 2003 for Enterprise Architects?
- 通信端口问题(不知道这个问法对否)
- 请推荐学习C#的好书
- 我用SERVER来传递页面参数值 可以看到传递值成功但是提示错误
- 怎样在dataGrid中用enter代替tab
- 老问题,怎样从一个窗口实例化类里控制另一个窗口实例化类里的控件?
Asp.net通过使用SmtpMail对象来发送邮件。SmtpMail对象发送邮件是通过以下几步来完成的。
# 导入与mail相关的名称空间。
# 建立一个message对象并设置属性。
# 通过SmtpMail对象实例的'Send'方法来发送邮件。 现在让我们一步步的来了解从asp.net页面发送邮件的过程。
一. 导入名称空间
在asp.net页面中导入System.Web.Util名称空间,这个名称空间包含了所有发送邮件所必须的对象。这些对象是:
对象 简介
SmtpMail 声明邮件系统(mail system)来发送邮件。
MailMessage 声明一个消息(message),包含发送地址,接收地址等属性。
MailFormat 声明消息的格式-Html,Text 等等。
MailAttachment 声明邮件的附件。
MailEncoding enum 声明加密方式:Base64 or UUencode.
MailPriority enum 设置邮件的优先级别:值:高,底,一般
<% @Import Namespace = "System.Web.Util" %> 二。初始化MailMessage 对象
使用以下语句可以实现初始化MailMessage对象。
Dim mailObj AS new MailMessage
MailMessage对象具有以下属性。属性 描述
From 发送方的email地址
To 接收方的email地址
Subject Email的主题
Body Email的内容
CC List of recipients copied on the email
BCC List of recipients blind-copied on the email
Priority 邮件的优先级别
高,底,一般
BodyEncoding 加密邮件的内容
Base64 或 UUencode
BodyFormat 内容的格式
Html 或文本
Attachments 附件列表 以下几行代码演示了如何使用MailMessage对象的属性的方法。他描述了我们将要创建的消息Message, 而这个消息(message)是用SmtpMail对象来发送的。在我们的这个例子中,mailObj就是MailMeessage对象的实例。 mailObj.From = "[email protected]"
mailObj.To = Request.Form ("to")
mailObj.Subject = "subject of the mail"
mailObj.Body = "Message of the mail"三。发送邮件
最后通过SmtpMail对象的'send'方法来发送邮件。下面的代码就是用来发送邮件的。
SmtpMail.Send(mailObj)
最后我们把以上的这些应用到一个完整的例子当中来。
下面是用Asp.net+C#编写的。<%@page language="C#" %>
<%@Import Namespace="System.Web.Util" %>
<HTML><BODY>
<SCRIPT LANGUAGE="C#" RUNAT="server">
// This method is called on the server when the submit
// button is clicked on the client and when the page
// posts back to itself
public void SendMail (Object Obj, EventArgs E)
{
MailMessage mailObj = new MailMessage(); // 设置email的'from'和'to'的地址
mailObj.From = Request.Form("From");
mailObj.To = Request.Form("To"); mailObj.Subject = "Subject Of the Mail";
mailObj.Body = "Body of the Mail"; // 可选: 使用html格式的Email
mailObj.BodyFormat = MailFormat.Html; // 可选: 对邮件进行加密
mailObj.BodyEncoding = MailFormat.Base64; // 可选: 设置邮件的优先级别为高
mailObj.Priority = MailPriority.High; // 可选: 附件
// 注意这里我们创建了一个MailAttachment对象来附加一个文件到email。
mailObj.Attachments.Add(new MailAttachment("c:\\test.doc")); // 使用SmtpMail对象来发送邮件。
SmtpMail.Send(mailObj);
}
</SCRIPT><asp:label ID="Headingmsg" Text="Enter Your Email Address:" RUNAT="server"/><FORM METHOD="post" RUNAT="server">Email Recipient: <INPUT TYPE="text" NAME="to"> <br>
Email Sender: <INPUT TYPE="text" NAME="from"><INPUT TYPE="submit" NAME="Submit" VALUE="Send Mail" RUNAT="server" OnServerClick="SendMail"></FORM>
</BODY>
MailMessage aa=new MailMessage();
private void Button1_Click(object sender, System.EventArgs e)
{
aa.To=ToTextBox.Text;
aa.Cc=CcTextBox.Text;
aa.Bcc=BccTextBox.Text;
aa.From=FromTextBox.Text;
aa.Subject=SubjectTextBox.Text;
aa.Body=BodyTextBox.Text;
//SmtpMail.SmtpServer="smtp.163.com";
SmtpMail.Send(aa);
}
如果加了SmtpMail.SmtpServer="smtp.163.com"则报错
“/NLFOFFICE2.0_1”应用程序中的服务器错误。
--------------------------------------------------------------------------------The message could not be sent to the SMTP server. The transport error code was 0x800ccc15. The server response was not available
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Runtime.InteropServices.COMException: The message could not be sent to the SMTP server. The transport error code was 0x800ccc15. The server response was not available 源错误:
行 97: aa.Body=TextBox8.Text;
行 98: “/NLFOFFICE2.0_1”应用程序中的服务器错误。
--------------------------------------------------------------------------------The message could not be sent to the SMTP server. The transport error code was 0x800ccc15. The server response was not available
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Runtime.InteropServices.COMException: The message could not be sent to the SMTP server. The transport error code was 0x800ccc15. The server response was not available 源错误:
行 97: aa.Body=TextBox8.Text;
行 98: SmtpMail.SmtpServer="smtp.163.com";
行 99: SmtpMail.Send(aa);
行 100:
行 101: }
行 99: SmtpMail.Send(aa);
行 100:
行 101: }
如果不加SmtpMail.SmtpServer="smtp.163.com";则发送成功但是,邮箱中并没有邮件。
希望大虾们指点一二!
我已经安装了smtpservice ,我用cdo组件在windows窗体中可以发送,但是在web窗体中不行?
http://www.lionsky.net/club/dispbbs.asp?boardID=3&ID=1133
个人专栏:http://www.csdn.net/develop/author/netauthor/lihonggen0/
------------------------------------------------------------------
谢谢大虾,我用了你的方法可以发送了,但是你的另一个方法可以在windows窗体下接收
但是不能在web 窗体下接收?希望你帮我看看?不胜感激!
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.Net;
using System.Net.Sockets;
using System.IO;
namespace NLFOFFICE2._0.测试用
{
/// <summary>
/// 接收邮件 的摘要说明。
/// </summary>
public class 接收邮件 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.CheckBox BackupChBox;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.TextBox PopServer;
protected System.Web.UI.WebControls.TextBox Username;
protected System.Web.UI.WebControls.TextBox Password;
protected System.Web.UI.WebControls.TextBox Message;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.TextBox MailNum;
protected System.Web.UI.WebControls.Button Connect;
protected System.Web.UI.WebControls.Button Disconnect;
protected System.Web.UI.WebControls.Button Retrieve;
protected System.Web.UI.WebControls.ListBox Status;
protected System.Web.UI.WebControls.Label Label6;
public TcpClient Server;
public NetworkStream NetStrm;
public StreamReader RdStrm;
public string Data;
public byte[] szData;
public string CRLF = "\r\n";
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
Server = new TcpClient(PopServer.Text,110);
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Connect.Click += new System.EventHandler(this.Connect_Click);
this.Disconnect.Click += new System.EventHandler(this.Disconnect_Click);
this.Retrieve.Click += new System.EventHandler(this.Retrieve_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Connect_Click(object sender, System.EventArgs e)
{
//Cursor cr = Cursor.Current;
//Cursor.Current = Cursors.WaitCursor; // create server POP3 with port 110
Server = new TcpClient(PopServer.Text,110);
Status.Items.Clear(); try
{
// initialization
NetStrm = Server.GetStream();
RdStrm= new StreamReader(Server.GetStream());
Status.Items.Add(RdStrm.ReadLine()); // Login Process
Data = "USER "+ Username.Text+CRLF;
szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length);
Status.Items.Add(RdStrm.ReadLine()); Data = "PASS "+ Password.Text+CRLF;
szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length);
Status.Items.Add(RdStrm.ReadLine()); // Send STAT command to get information ie: number of mail and size
Data = "STAT"+CRLF;
szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length);
Status.Items.Add(RdStrm.ReadLine()); // change enabled - disabled button
Connect.Enabled = false;
Disconnect.Enabled = true;
Retrieve.Enabled = true; // back to normal cursor
//Cursor.Current = cr;
}
catch(InvalidOperationException err)
{
Status.Items.Add("Error: "+err.ToString());
}
} private void Disconnect_Click(object sender, System.EventArgs e)
{
// change cursor into wait cursor
//Cursor cr = Cursor.Current;
//Cursor.Current = Cursors.WaitCursor; // Send QUIT command to close session from POP server
Data = "QUIT"+CRLF;
szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length);
Status.Items.Add(RdStrm.ReadLine()); //close connection
NetStrm.Close();
RdStrm.Close(); // change enabled - disabled button
Connect.Enabled = true;
Disconnect.Enabled = false;
Retrieve.Enabled = false; // back to normal cursor
//Cursor.Current = cr; } private void Retrieve_Click(object sender, System.EventArgs e)
{
//Cursor cr = Cursor.Current;
//Cursor.Current = Cursors.WaitCursor;
string szTemp;
Message.Text="";
try
{
// retrieve mail with number mail parameter
Data = "RETR "+ MailNum.Text+CRLF;
szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length); szTemp = RdStrm.ReadLine();
if(szTemp[0]!='-')
{
while(szTemp!=".")
{
Message.Text += szTemp;
szTemp = RdStrm.ReadLine();
}
}
else
{
Status.Items.Add(szTemp);
}
// back to normal cursor
//Cursor.Current = cr; }
catch(InvalidOperationException err)
{
Status.Items.Add("Error: "+err.ToString());
} }
}
}运行时报错如下
编译错误
说明: 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。 编译器错误信息: CS0117: “System.Net.Sockets.TcpClient”并不包含对“ScriptTimeout”的定义源错误: 行 50: ASP.接收邮件_aspx.__intialized = true;
行 51: }
行 52: this.Server.ScriptTimeout = 30000000;
行 53: }
行 54:
源文件: C:\WINNT\Microsoft.NET\Framework\v1.0.3705\Temporary ASP.NET Files\nlfoffice2.0_1\de230125\40ae4307\l5hh3lcl.0.cs 行: 52
jmail.POP3 email = new jmail.POP3();
email.Logging=true;
email.Connect(UserName.Text, Password.Text, PopServer.Text, 110);
if (email.Count > 0)
{ Messages mm=email.Messages;
try
{
this.savemail(mm);
}
catch(Exception ex)
{
Message.Text=ex.Message;
}
return ;
}
email.Disconnect();