日期:2009-03-05  浏览次数:20432 次


using System;
using System.Text;
using System.IO;
using System.Net;

using System.Net.Sockets;
using System.Collections;

namespace SkyDev.Web.Mail
{
public enum MailFormat{Text,HTML};
public enum MailPriority{Low=1,Normal=3,High=5};

#region Class mailAttachments
public class MailAttachments
{
private const int MaxAttachmentNum=10;
private IList _Attachments;

public MailAttachments()
{
_Attachments=new ArrayList();
}

public string this[int index]
{
get { return (string)_Attachments[index];}
}
/// <summary>
/// 添加邮件附件
/// </summary>
/// <param name="FilePath">附件的绝对路径</param>
public void Add(params string[] filePath)
{
if(filePath==null)
{
throw(new ArgumentNullException("非法的附件"));
}
else
{
for(int i=0;i<filePath.Length;i++)
{
Add(filePath[i]);
}
}
}

/// <summary>
/// 添加一个附件,当指定的附件不存在时,忽略该附件,不产生异常。
/// </summary>
/// <param name="filePath">附件的绝对路径</param>
public void Add(string filePath)
{
//当附件存在时才加入,否则忽略
if (System.IO.File.Exists(filePath))
{
if (_Attachments.Count<MaxAttachmentNum)
{
_Attachments.Add(filePath);
}
}
}

public void Clear()//清除所有附件
{
_Attachments.Clear();
}

public int Count//获取附件个数
{
get { return _Attachments.Count;}
}

}
#endregion//end Class mailAttachments



#region Class MailMessage
/// <summary>
/// MailMessage 表示SMTP要发送的一封邮件的消息。
/// </summary>
public class MailMessage
{
private const int MaxRecipientNum=10;
public MailMessage()
{
_Recipients=new ArrayList();//收件人列表
_Attachments=new MailAttachments();//附件
_BodyFormat=MailFormat.Text;//缺省的邮件格式为Text
_Priority=MailPriority.Normal;
_Charset="GB2312";
}

/// <summary>
/// 设定语言代码,默认设定为GB2312,如不需要可设置为""
/// </summary>
public string Charset
{
get { return _Charset;}
set { _Charset=value;}
}

public string From
{
get{ return _From;}
set { _From=value;}
}

public string FromName
{
get { return _FromName;}
set { _FromName=value;}
}
public string Body
{
get { return _Body;}
set { _Body=value;}
}

public string Subject
{
get { return _Subject;}
set { _Subject=value;}
}

public MailAttachments Attachments
{
get {return _Attachments;}
set { _Attachments=value;}
}

public MailPriority Priority
{
get { return _Priority;}
set { _Priority=value;}
}

public IList Recipients
{
get { return _Recipients;}
}
/// <summary>
/// 增加一个收件人地址
/// </summary>
/// <param name="recipient">收件人的Email地址</param>
public void AddRecipients(string recipient)
{
//先检查邮件地址是否符合规范
if (_Recipients.Count<MaxRecipientNum)
{
_Recipients.Add(recipient);//增加到收件人列表
}
}

public void AddRecipients(params string[] recipient)
{
if (recipient==null)
{
throw (new ArgumentException("收件人不能为空."));