程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> C#實現SMTP服務器,使用TCP命令實現,功能比較完善

C#實現SMTP服務器,使用TCP命令實現,功能比較完善

編輯:關於C#
 

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("收件人不能為空."));
}
else
{
for (int i=0;i<recipient.Length;i++)
{
AddRecipients(recipient[i]);
}
}
}

public MailFormat BodyFormat
{
set { _BodyFormat=value;}
get { return _BodyFormat;}
}

private string _From;//發件人地址
private string _FromName;//發件人姓名
private IList _Recipients;//收件人
private MailAttachments _Attachments;//附件
private string _Body;//內容
private string _Subject;//主題
private MailFormat _BodyFormat;//郵件格式
private string _Charset="GB2312";//字符編碼格式
private MailPriority _Priority;//郵件優先級
}
#endregion


#region Class SmtpMail
public class SmtpServerHelper
{
private string CRLF="/r/n";//回車換行

/// <summary>
/// 錯誤消息反饋
/// </summary>
private string errmsg;

/// <summary>
/// TcpClient對象,用於連接服務器
/// </summary>
private TcpClient tcpClient;

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved