总结:.用asp.net 2.0发送邮件非常的方便,只需要用using System.Net.Mail命名空间下的类就可以完成发送邮件的功能,发送邮件的服务器可以有以下几种情况:
1.本地的smtp服务器
2.网络上的smtp服务器(smtp.163.com等)
以下是我测试发送邮件的范例,基本上都考虑到的所有的情况,如果还有什么没有考虑到的请给我留言,我接续完善其功能.
1.新建一网站名字叫SentEmailText.
2.添加一个新的页面default.aspx
代码如下:
<
form
id
="Form1"
runat
="server"
method
="post"
>
<
div
id
="content"
>
<
div
id
="msg"
>
<
div
id
="msg_title"
>
Send Email Test
</
div
>
<
div
id
="msg_prompt"
></
div
>
<
asp:ValidationSummary
id
="msg_alarm"
runat
="server"
></
asp:ValidationSummary
>
</
div
>
<
div
id
="form"
>
<
div
>
<
label
for
="SmtpServerText"
>
Sever name:
</
label
>
<
input
type
="text"
id
="SmtpServerText"
name
="SmtpServerText"
runat
="server"
/>
</
div
>
<
div
>
<
label
for
="SmtpServerPort"
>
Sever Port:
</
label
>
<
input
type
="text"
id
="SmtpServerPort"
name
="SmtpServerPort"
runat
="server"
/>
</
div
>
<
div
>
<
label
for
="UserName"
>
User Name:
</
label
>
<
input
type
="text"
name
="UserName"
id
="UserName"
runat
="server"
/>
</
div
>
<
div
>
<
label
for
="Pwd"
>
Pwd:
</
label
>
<
input
type
="text"
name
="Pwd"
runat
="server"
id
="Pwd"
/>
</
div
>
<
div
>
<
label
for
="FromAddress"
>
From Address:
</
label
>
<
input
type
="text"
name
="FromAddress"
runat
="server"
id
="FromAddress"
/>
</
div
>
<
div
>
<
label
for
="SendAddress"
>
Send Address:
</
label
>
<
input
type
="text"
name
="SendAddress"
runat
="server"
id
="SendAddress"
/>
</
div
>
<
div
>
<
label
for
="attachment"
>
Attachment:
</
label
>
<
input
type
="file"
runat
="server"
name
="attachment"
id
="attachment"
/>
</
div
>
<
div
>
<
label
for
="txtSubject"
>
Email Subject:
</
label
>
<
input
type
="text"
name
="txtSubject"
runat
="server"
id
="txtSubject"
/>
</
div
>
<
div
>
<
label
for
="txtBody"
>
Email Body:
</
label
>
<
textarea
name
="txtBody"
runat
="server"
rows
="10"
cols
="50"
id
="txtBody"
></
textarea
>
</
div
>
<
div
>
<
input
type
="submit"
value
="Send Email"
class
="prim"
id
="btnSend"
runat
="server"
onserverclick
="btnSend_ServerClick"
/>
</
div
>
</
div
>
</
div
>
</
form
>
3.新建一个style的文件夹,添加样式页面,主要功能是表单的对齐样式,比较通用的设置(见附件)
4.添加一个mail的类文件,注意用于发送邮件考虑的几种情况(见附件)
using
System;
using
System.Text;
using
System.Net.Mail;
using
System.Net.Sockets;
using
System.IO;
public
class
Mail

{

"Custom var"#region "Custom var"
private string _User;
private string _Pwd;
private string _Host;
private int _Port;
#endregion


"Property"#region "Property"
public int Port

{
get

{
return _Port;
}
set

{
_Port = value;
}
}
public string User

{
get

{
return _User;
}
set

{
_User = value;
}
}
public string Pwd

{
get

{
return _Pwd;
}
set

{
_Pwd = value;
}
}
public string Host

{
get

{
return _Host;
}
set

{
_Host = value;
}
}
#endregion


"OverLoad"#region "OverLoad"
public Mail(string user, string pwd,string host,int port)

{//other smtp server including username and pwd
this._User = user;
this._Pwd = pwd;
this._Host = host;
this._Port = port;
}
public Mail(string host,int port)

{//other smtp server but username and pwd
this._Port = port;
this._Host = host;
}
public Mail()

{//local smtp server and port=25
this._Port = 25;
this._Host = "127.0.0.1";
}
#endregion


"Send Emial including groups and attachment"#region "Send Emial including groups and attachment"

/**//// <summary>
/// Sent emails including attachment
/// </summary>
/// <param name="toUser">From Address</param>
/// <param name="toMail">To Address</param>
/// <param name="subject">The subject of Email</param>
/// <param name="body">The body of Email</param>
/// <param name="ishtml">Html Or Text</param>
/// <param name="priority">The priority of Email</param>
/// <param name="filenames">the attachment of email(optional)</param>
public void SendMailWithAttachment(string toUser,string toMail, string subject, string body,bool ishtml,MailPriority priority,params string[] filenames)

{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(toUser,toMail,subject,body);
msg.BodyEncoding = Encoding.UTF8;
msg.Priority = priority;
msg.IsBodyHtml = ishtml ;
if (filenames != null)

{
foreach (string s in filenames)

{
msg.Attachments.Add(new Attachment(s));
}
}
System.Net.Mail.SmtpClient sc = new SmtpClient(Host, Port);
sc.EnableSsl = false;
sc.Timeout = 3600000;
sc.UseDefaultCredentials = false;
sc.Credentials = new System.Net.NetworkCredential(_User, _Pwd);
sc.DeliveryMethod = SmtpDeliveryMethod.Network;
try

{
sc.Send(msg);
}
catch (Exception e)

{
throw e;
}
}

/**//// <summary>
/// Sent Email including groups and attachment
/// </summary>
/// <param name="toUser">From Address</param>
/// <param name="toMail">To Address</param>
/// <param name="subject">The subject of Email</param>
/// <param name="body">The body of Email</param>
/// <param name="ishtml">Html Or Text</param>
/// <param name="priority">The priority of Email</param>
/// <param name="filenames">The attachment of Email(optional)</param>
public void SendMailWithGroupWithAttachment(string toUser,string toMail, string subject, string body,bool ishtml,MailPriority priority,params string[] filenames)

{
string[] toMails = toMail.Split(';');
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.From=new MailAddress(toUser );
foreach (string s in toMails)

{
msg.To.Add(s);
}
msg.Subject = subject;
msg.Body = body;
msg.BodyEncoding = Encoding.UTF8;
msg.Priority = priority;
msg.IsBodyHtml = ishtml;
if (filenames != null)

{
foreach (string s in filenames)
msg.Attachments.Add(new Attachment(s));
}
System.Net.Mail.SmtpClient sc = new SmtpClient(Host,Port);
sc.EnableSsl = false;
sc.Timeout = 3600000;
sc.UseDefaultCredentials = false;
sc.Credentials = new System.Net.NetworkCredential(_User, _Pwd);
sc.DeliveryMethod = SmtpDeliveryMethod.Network;
try

{
sc.Send(msg);
}
catch (Exception e)

{
throw e;
}
}
#endregion


/**//// <summary>
/// Received Email
/// </summary>
public void ReseiveMail()

{
string ServerHost = "pop3."+this._Host ;
TcpClient tcp = new TcpClient(ServerHost, 110);
NetworkStream ns = tcp.GetStream();
StreamReader sr = new StreamReader(ns);
StreamWriter sw = new StreamWriter(ns);
}
}
5.页面调用方式参见附件
6.附件下载
附件(源代码下载)