SMTP,POP3 简单的说一下,SMTP 是发送邮件的协议,POP3是接收邮件的协议
这里只用到SMTP协议.对于接收邮件System.Web.Mail 命名空并没有做支持.先实现简单的,POP3以后再研究了
先建立一个通用的类来包装一下烦琐的造作
using
System;
using
System.IO;
using
System.Text;
using
System.Web.Mail;

namespace
Yjj.Web.Mail

...
{

/**//// <summary>
/// Class1 的摘要说明。
/// </summary>
public class Commonly

...{
//邮件属性
private string _To;
private string _From;
private string _Subject;
private string _Body;
private string _Attach;
private string _Cc;
private string _Bcc;
private string _BodyFormat;
private string _BodyEncoding;
private string _Priority;
private string _UrlContentBase;
private string _UrlContentLocation;

//属性设置

/**//// <summary>
/// 邮件目的
/// </summary>
public string To

...{

set...{_To = value;}

get...{return _To;}
}

/**//// <summary>
/// 邮件发送者
/// </summary>
public string From

...{

set...{_From = value;}

get...{return _From;}
}


/**//// <summary>
/// 邮件标题
/// </summary>
public string Subject

...{

set...{_Subject = value;}

get...{return _Subject;}
}


/**//// <summary>
/// 邮件主体
/// </summary>
public string Body

...{

set...{_Body = value;}

get...{return _Body;}
}


/**//// <summary>
/// 邮件附件
/// </summary>
public string Attach

...{

set...{_Attach = value;}

get...{return _Attach;}
}


/**//// <summary>
/// 邮件抄送
/// </summary>
public string Cc

...{

set...{_Cc = value;}

get...{return _Cc;}
}

/**//// <summary>
/// 邮件匿名副本
/// </summary>
public string Bcc

...{

set...{_Bcc = value;}

get...{return _Bcc;}
}


/**//// <summary>
/// 邮件邮件格式类型HTML ,TEXT
/// </summary>
public string BodyFormat

...{

set...{_BodyFormat = value;}

get...{return _BodyFormat;}
}

/**//// <summary>
/// 邮件正文的编码类型
/// </summary>
public string BodyEncoding

...{

set...{_BodyEncoding = value;}

get...{return _BodyEncoding;}
}

/**//// <summary>
/// 邮件优先级别
/// </summary>
public string Priority

...{

set...{_Priority = value;}

get...{return _Priority;}
}

/**//// <summary>
/// 获取或设置 Content-Base HTTP 标头,即在 HTML 编码的电子邮件正文中使用的所有相对 URL 的 URL 基。
/// </summary>
public string UrlContentBase

...{

set...{_UrlContentBase = value;}

get...{return _UrlContentBase;}
}


/**//// <summary>
/// 获取或设置电子邮件的 Content-Location HTTP 标头。
/// </summary>
public string UrlContentLocation

...{

set...{_UrlContentLocation = value;}

get...{return _UrlContentLocation;}
}



//服务器属性
private string _MailServer;
private string _ServerPort;
private string _Authenticate;
private string _UserName;
private string _Password;
private string _SSL;
//服务器属性设置

/**//// <summary>
/// 邮件发送服务器。
/// </summary>
public string MailServer

...{

set...{_MailServer = value;}

get...{return _MailServer;}
}

/**//// <summary>
/// 邮件smtp 端口
/// </summary>
public string ServerPort

...{

set...{_ServerPort = value;}

get...{return _ServerPort;}
}

/**//// <summary>
/// 权限设置
/// </summary>
public string Authenticate

...{

set...{_Authenticate = value;}

get...{return _Authenticate;}
}

/**//// <summary>
/// 用户名
/// </summary>
public string UserName

...{

set...{_UserName = value;}

get...{return _UserName;}
}

/**//// <summary>
/// 密码
/// </summary>
public string Password

...{

set...{_Password = value;}

get...{return _Password;}
}


/**//// <summary>
/// 是否用SSL访问 是为1 不是不用附值
/// </summary>
public string SSL

...{

set...{_SSL = value;}

get...{return _SSL;}
}

//公共属性
public string Info;
//函数部分
//构造函数
public Commonly()

...{
//
// TODO: 在此处添加构造函数逻辑
//
}
public Commonly(string sTo,string sFrom,string sSubject,string sBody,string sCc,string sBcc,string sAttach)

...{
this._To = sTo;
this._From = sFrom;
this._Subject = sSubject;
this._Body = sBody;
this._Attach = sAttach;
this._Cc = sCc;
this._Bcc = sBcc;
}
//静态函数,用于发邮件
public static bool SendMail(Commonly m_info)

...{
MailMessage mm = new MailMessage();
mm.To = m_info._To;
mm.From = m_info._From;
mm.Subject = m_info._Subject;
mm.Body = m_info._Body;
mm.Cc = m_info._Cc;
mm.Bcc = m_info._Bcc;
mm.UrlContentBase = m_info._UrlContentBase;
mm.UrlContentLocation = m_info._UrlContentLocation;

//确定是否有附件
if (m_info._Attach != null && m_info._Attach != "")

...{
char[] delim = new char[] {,};
foreach (string sSubstr in m_info._Attach.Split(delim))

...{
if (File.Exists(sSubstr))

...{
MailAttachment MyAttachment = new MailAttachment(sSubstr);
mm.Attachments.Add(MyAttachment);
}
else

...{
m_info.Info = "附件不存在!";
return false;
}
}
}
//确定编码类型
if (m_info._BodyEncoding == Encoding.UTF7.EncodingName)
mm.BodyEncoding = Encoding.UTF7;
else if (m_info._BodyEncoding == Encoding.UTF8.EncodingName)
mm.BodyEncoding = Encoding.UTF8;
else
mm.BodyEncoding = Encoding.ASCII;

//确定mail类型 (HTML,TEXT)
switch (m_info._BodyFormat.ToUpper())

...{
case "HTML":
mm.BodyFormat = MailFormat.Html;
break;
default:
mm.BodyFormat = MailFormat.Text;
break;
}

//确定优先级
switch (m_info._Priority.ToUpper())

...{
case "HIGH":
mm.Priority = MailPriority.High;
break;
case "LOW":
mm.Priority = MailPriority.Low;
break;
default:
mm.Priority = MailPriority.Normal;
break;
}

//基本权限
mm.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", m_info._Authenticate);
//用户名
mm.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", m_info._UserName) ;
//密码
mm.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword",m_info._Password);
//端口:
mm.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport",m_info._ServerPort);
//SSL
if (m_info._SSL != null && m_info._SSL != "")

...{
mm.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", m_info._SSL);
}

SmtpMail.SmtpServer = m_info._MailServer;
try

...{
SmtpMail.Send(mm);
m_info.Info = "邮件发送成功!";
return true;
}
catch (System.Exception e)

...{
m_info.Info ="邮件发送失败! ";
m_info.Info += e.Message.ToString();
return false;
}
}
}
}
注释写的不是很详细... 懒惰没办法
下面就是开始用这个类发邮件了
//
Page_Load 这里只是为了测试,其实不用写也没关系
private
void
Page_Load(
object
sender, System.EventArgs e)

...
{
// 在此处放置用户代码以初始化页面
if (!this.IsPostBack)

...{
txtTo.Text="yjjie2008@sina.com";
txtFrom.Text="yjjie2008@gmail.com";
txtCc.Text="yjjie@163.com";
txtBcc.Text="yjjie2005@sina.com";
txtSubject.Text="This is a test TITLE";
txtBody.Text="This is a test BODY.";
txtBodyEncoding.Text = Encoding.UTF8.EncodingName;
// Name of relay mail server in your domain.
txtMailServer.Text="smtp.gmail.com";
txtServerPort.Text="465";
txtUserName.Text="username";// 这是你要发送的邮件的帐号这里用的是gmail的,因为它比较特殊
txtPassword.Text="password";//密码
}
}
//
两个按扭事件
private
void
btnSubmit_Click(
object
sender, System.EventArgs e)

...
{
Commonly MyMail = new Commonly(txtTo.Text.Trim(),txtFrom.Text.Trim(),txtSubject.Text,txtBody.Text,txtCc.Text.Trim(),txtBcc.Text.Trim(),FAttach.PostedFile.FileName);
MyMail.MailServer = txtMailServer.Text;
MyMail.ServerPort = txtServerPort.Text;
MyMail.BodyEncoding = txtBodyEncoding.Text;
MyMail.BodyFormat = txtBodyEncoding.Text;
MyMail.Priority = DDLPriority.SelectedValue;
MyMail.UserName = txtUserName.Text.Trim();
MyMail.Password = txtPassword.Text.Trim();
MyMail.Authenticate = "1";
if (CkbSSL.Checked)

...{
MyMail.SSL = "1";
}
if (Commonly.SendMail(MyMail))

...{
this.Alert(MyMail.Info,this);
}
else

...{
this.Alert(MyMail.Info,this);
}

}

private
void
btnClear_Click(
object
sender, System.EventArgs e)

...
{
lblMsg1.Text="";
txtTo.Text="";
txtFrom.Text="";
txtSubject.Text="";
txtBody.Text="";
txtBcc.Text="";
txtCc.Text="";
txtBodyEncoding.Text="";
txtMailServer.Text="";
btnSubmit.Text="Submit";
}
//HTML部分
<
h4
>
Send a new mail message:
</
h4
>
<
Form
method
="post"
action
="MailForm.aspx"
runat
="server"
ID
="Form1"
>
<
table
width
="350"
bgcolor
="#ffff99"
>
<
tr
>
<
td
Align
="right"
><
b
>
To:
</
b
></
td
>
<
td
><
Asp:Textbox
id
="txtTo"
runat
="server"
/></
td
>
</
tr
>
<
tr
>
<
h4
><
td
Align
="right"
><
b
>
From:
</
b
></
td
>
</
h4
>
<
td
><
Asp:Textbox
id
="txtFrom"
runat
="server"
/></
td
>
</
tr
>
<
tr
>
<
td
Align
="right"
><
b
>
Subject:
</
b
></
td
>
<
td
><
Asp:Textbox
id
="txtSubject"
runat
="server"
/></
td
>
</
tr
>
<
tr
>
<
td
Align
="right"
><
b
>
MessageBody:
</
b
></
td
>
<
td
><
Asp:Textbox
id
="txtBody"
runat
="server"
/></
td
>
</
tr
>
<
tr
>
<
td
Align
="right"
><
b
>
Attachments:
</
b
></
td
>
<
td
><
INPUT
id
="FAttach"
type
="file"
name
="File1"
runat
="server"
></
td
>
</
tr
>
<
tr
>
<
td
Align
="right"
><
b
>
CC:
</
b
></
td
>
<
td
><
Asp:Textbox
id
="txtBcc"
runat
="server"
/></
td
>
</
tr
>
<
tr
>
<
td
Align
="right"
><
b
>
BCC:
</
b
></
td
>
<
td
><
Asp:Textbox
id
="txtCc"
runat
="server"
/></
td
>
</
tr
>
<
tr
>
<
td
Align
="right"
><
b
>
BodyEncoding:
</
b
></
td
>
<
td
><
Asp:Textbox
id
="txtBodyEncoding"
runat
="server"
/></
td
>
</
tr
>
<
tr
>
<
td
Align
="right"
><
b
>
BodyFormat:
</
b
></
td
>
<
td
>
<
asp:DropDownList
id
="DDLBodyFormat"
runat
="server"
Width
="120px"
>
<
asp:ListItem
Value
="Html"
Selected
="True"
>
Html
</
asp:ListItem
>
<
asp:ListItem
Value
="Text"
>
Text
</
asp:ListItem
>
</
asp:DropDownList
></
td
>
</
tr
>
<
tr
>
<
td
Align
="right"
><
b
>
Priority:
</
b
></
td
>
<
td
>
<
asp:DropDownList
id
="DDLPriority"
runat
="server"
Width
="120px"
>
<
asp:ListItem
Value
="Normal"
Selected
="True"
>
Normal
</
asp:ListItem
>
<
asp:ListItem
Value
="High"
>
High
</
asp:ListItem
>
<
asp:ListItem
Value
="LOW"
>
LOW
</
asp:ListItem
>
</
asp:DropDownList
></
td
>
</
tr
>
<
tr
>
<
td
Align
="right"
><
b
>
Mail Server:
</
b
></
td
>
<
td
><
Asp:Textbox
id
="txtMailServer"
runat
="server"
/></
td
>
</
tr
>
<
tr
>
<
td
Align
="right"
><
b
>
Server Port:
</
b
></
td
>
<
td
><
Asp:Textbox
id
="txtServerPort"
runat
="server"
/></
td
>
</
tr
>
<
tr
>
<
td
Align
="right"
><
b
>
User NameL:
</
b
></
td
>
<
td
>
<
asp:TextBox
id
="txtUserName"
runat
="server"
></
asp:TextBox
></
td
>
</
tr
>
<
tr
>
<
td
Align
="right"
><
b
>
Password::
</
b
></
td
>
<
td
>
<
asp:TextBox
id
="txtPassword"
runat
="server"
TextMode
="Password"
></
asp:TextBox
></
td
>
</
tr
>
<
tr
>
<
td
Align
="right"
><
b
>
Server SSL:
</
b
></
td
>
<
td
>
<
asp:CheckBox
id
="CkbSSL"
runat
="server"
Text
="SSL"
Checked
="True"
></
asp:CheckBox
></
td
>
</
tr
>
</
table
>
<
br
>
<
asp:button
id
="btnSubmit"
Text
="Submit"
runat
="server"
/>
<
asp:button
id
="btnClear"
Text
="Clear"
runat
="server"
/>
<
p
><
asp:Label
id
="lblMsg1"
runat
="server"
/></
p
>
</
Form
>
这里只用到SMTP协议.对于接收邮件System.Web.Mail 命名空并没有做支持.先实现简单的,POP3以后再研究了
先建立一个通用的类来包装一下烦琐的造作








































































































































































































































































































































































































注释写的不是很详细... 懒惰没办法

下面就是开始用这个类发邮件了




































































//HTML部分


















































































