去年8月时,因为需要做了一个Email收发系统,收邮件的太复杂了,贴不下来。发邮件分别写了两种发送方式,一种是基于NET WebMail的需要验证的EmailSend,一种是使用Socket直接基于SMTP发送的。
掌握基于SMTP协议使用Socket Coding的基本原理后,写这一类似的东西都能触类旁通了。比如基于FTP的Downlload,UpLoad的组件等等。
下面对即将看到的4个Class作个说明.
EmailInfo :构成Email的基本类,相当于System.Web.Mail.MailMessage(偶并不是不喜欢MailMessage,而是因为在接收Email时需要使用自己定义的EmailInfo)
SendEmailByNetWebMail:封装WebMail的类
SendEmailBySocket:基于SMTP协议直接使用SOCKET发送的类
EmailSender:对外开放使用发送Email的类。
using
System;
using System.Collections;
using System.Net.Sockets;
using System.IO;
using System.Text;
namespace NewEgg.EmailRSM
{
/**//// <summary>
/// Summary description for EmailInfo.
/// </summary>
public class EmailInfo
{
private ArrayList attachments = new ArrayList ();
private string from = "";
private string to = "";
private string fromName = "";
private string toName = "";
private string cc="";
private string bcc="";
private string subject = "";
private string body = "";
private string htmlBody = "";
private bool isHtml = false;
private string languageEncoding = "UTF-8";
private string encoding = "8bit";
private int priority = 3;
private string m_toperson="";
public EmailInfo()
{
//
// TODO: Add constructor logic here
//
}
/**//// <summary>
/// 发件人地址
/// </summary>
public string From
{
get { return from; }
set { if (value != from) from = value;}
}
/**//// <summary>
/// 收件人地址
/// </summary>
public string To
{
get { return to; }
set { if (value != to) to = value;}
}
/**//// <summary>
/// 发件人姓名
/// </summary>
public string FromName
{
get { return fromName; }
set { if (value != fromName) fromName = value; }
}
/**//// <summary>
/// 收件人姓名
/// </summary>
public string ToName
{
get { return toName; }
set { if (value != toName) toName = value; }
}
/**//// <summary>
/// 收信的个人
/// </summary>
public string ToPerson
{
get { return m_toperson ; }
set { if (value != m_toperson) m_toperson = value; }
}
/**//// <summary>
/// 抄送
/// </summary>
public string CC
{
get { return cc; }
set { if (value != cc) cc = value; }
}
/**//// <summary>
/// 暗送
/// </summary>
public string BCC
{
get { return bcc; }
set { if (value != bcc) bcc = value; }
}
/**//// <summary>
/// 邮件的主题
/// </summary>
public string Subject
{
get { return subject; }
set { if (value != subject) subject = value; }
}
/**//// <summary>
/// 邮件正文
/// </summary>
public string Body
{
get { return body; }
set { if (value != body) body = value; }
}
/**//// <summary>
/// 超文本格式的邮件正文
/// </summary>
public string HtmlBody
{
get { return htmlBody; }
set { if (value != htmlBody) htmlBody = value; }
}
/**//// <summary>
/// 是否是html格式的邮件
/// </summary>
public bool IsHtml
{
get { return isHtml; }
set { if (value != isHtml) isHtml = value; }
}
/**//// <summary>
/// 语言编码 [默认为GB2312]
/// </summary>
public string LanguageEncoding
{
get { return languageEncoding; }
set { if (value != languageEncoding) languageEncoding = value; }
}
/**//// <summary>
/// 邮件编码 [默认为8bit]
/// </summary>
public string MailEncoding
{
get { return encoding; }
set { if (value != encoding) encoding = value; }
}
/**//// <summary>
/// 邮件优先级 [默认为3]
/// </summary>
public int Priority
{
get { return priority; }
set { if (value != priority) priority = value; }
}
/**//// <summary>
/// 附件 [AttachmentInfo]
/// </summary>
public ArrayList Attachments
{
get { return attachments; }
}
}
}
using System.Collections;
using System.Net.Sockets;
using System.IO;
using System.Text;
namespace NewEgg.EmailRSM
{
/**//// <summary>
/// Summary description for EmailInfo.
/// </summary>
public class EmailInfo
{
private ArrayList attachments = new ArrayList ();
private string from = "";
private string to = "";
private string fromName = "";
private string toName = "";
private string cc="";
private string bcc="";
private string subject = "";
private string body = "";
private string htmlBody = "";
private bool isHtml = false;
private string languageEncoding = "UTF-8";
private string encoding = "8bit";
private int priority = 3;
private string m_toperson="";
public EmailInfo()
{
//
// TODO: Add constructor logic here
//
}
/**//// <summary>
/// 发件人地址
/// </summary>
public string From
{
get { return from; }
set { if (value != from) from = value;}
}
/**//// <summary>
/// 收件人地址
/// </summary>
public string To
{
get { return to; }
set { if (value != to) to = value;}
}
/**//// <summary>
/// 发件人姓名
/// </summary>
public string FromName
{
get { return fromName; }
set { if (value != fromName) fromName = value; }
}
/**//// <summary>
/// 收件人姓名
/// </summary>
public string ToName
{
get { return toName; }
set { if (value != toName) toName = value; }
}
/**//// <summary>
/// 收信的个人
/// </summary>
public string ToPerson
{
get { return m_toperson ; }
set { if (value != m_toperson) m_toperson = value; }
}
/**//// <summary>
/// 抄送
/// </summary>
public string CC
{
get { return cc; }
set { if (value != cc) cc = value; }
}
/**//// <summary>
/// 暗送
/// </summary>
public string BCC
{
get { return bcc; }
set { if (value != bcc) bcc = value; }
}
/**//// <summary>
/// 邮件的主题
/// </summary>
public string Subject
{
get { return subject; }
set { if (value != subject) subject = value; }
}
/**//// <summary>
/// 邮件正文
/// </summary>
public string Body
{
get { return body; }
set { if (value != body) body = value; }
}
/**//// <summary>
/// 超文本格式的邮件正文
/// </summary>
public string HtmlBody
{
get { return htmlBody; }
set { if (value != htmlBody) htmlBody = value; }
}
/**//// <summary>
/// 是否是html格式的邮件
/// </summary>
public bool IsHtml
{
get { return isHtml; }
set { if (value != isHtml) isHtml = value; }
}
/**//// <summary>
/// 语言编码 [默认为GB2312]
/// </summary>
public string LanguageEncoding
{
get { return languageEncoding; }
set { if (value != languageEncoding) languageEncoding = value; }
}
/**//// <summary>
/// 邮件编码 [默认为8bit]
/// </summary>
public string MailEncoding
{
get { return encoding; }
set { if (value != encoding) encoding = value; }
}
/**//// <summary>
/// 邮件优先级 [默认为3]
/// </summary>
public int Priority
{
get { return priority; }
set { if (value != priority) priority = value; }
}
/**//// <summary>
/// 附件 [AttachmentInfo]
/// </summary>
public ArrayList Attachments
{
get { return attachments; }
}
}
}
using
System;
using System.Collections;
using System.Net.Sockets;
using System.IO;
using System.Text;
namespace NewEgg.EmailRSM
{
/**//// <summary>
/// Summary description for EmailSender.
/// </summary>
public class EmailSender
{
private SendClassType m_SendClassType=SendClassType.NetWebMail ;
private string m_Server="";
private string m_UserId="";
private string m_Pwd="";
private int port = 25;
private EmailInfo m_Emailinfo=null;
public EmailSender()
{
//
// TODO: Add constructor logic here
//
}
/**//// <summary>
/// 选择采用哪种发送方式的枚举类型
/// </summary>
public enum SendClassType
{
NetWebMail,
SocketSend
}
parameres of this class#region parameres of this class
/**//// <summary>
///
/// </summary>
/// <param name="sendclassType">发送邮件采用哪种方式</param>
public EmailSender(SendClassType sendclassType)
{
m_SendClassType=sendclassType;
}
/**//// <summary>
/// check the type of send email type
/// </summary>
public SendClassType ChoiceSendType
{
get { return m_SendClassType; }
set { if (value != m_SendClassType) m_SendClassType = value; }
}
/**//// <summary>
/// Exchange server dns & ip
/// </summary>
public string Server
{
get { return m_Server; }
set { if (value != m_Server) m_Server = value; }
}
/**//// <summary>
/// userid
/// </summary>
public string UserId
{
get { return m_UserId; }
set { if (value != m_UserId) m_UserId = value; }
}
/**//// <summary>
/// password
/// </summary>
public string PassWord
{
get { return m_Pwd; }
set { if (value != m_Pwd) m_Pwd = value; }
}
/**//// <summary>
/// SMTP port [default:25]
/// </summary>
public int Port
{
get { return port; }
set { if (value != port) port = value; }
}
/**//// <summary>
/// EMAIL信息构件
/// </summary>
public EmailInfo EmailMessage
{
get {return m_Emailinfo;}
set {m_Emailinfo=value;}
}
#endregion
/**//// <summary>
///
/// </summary>
/// <returns></returns>
public bool SendEmail()
{
try
{
if(m_SendClassType==SendClassType.SocketSend )
{
SendEmailBySocket emailsend=new SendEmailBySocket();
emailsend.Server=m_Server ;
emailsend.Port =port ;
emailsend.UserName =m_UserId ;
emailsend.Password =m_Pwd ;
//emailsend.SendMail(m_Emailinfo);
string sMailTo=m_Emailinfo.To ;
foreach(string sPerson in sMailTo.Split(';'))
{
if(sPerson.Trim() !="")
{
m_Emailinfo.ToPerson =sPerson;
emailsend.SendMail(m_Emailinfo);
}
}
foreach(string sPerson in m_Emailinfo.CC.Split(';'))
{
if(sPerson.Trim() !="")
{
m_Emailinfo.ToPerson =sPerson;
emailsend.SendMail(m_Emailinfo);
}
}
foreach(string sPerson in m_Emailinfo.BCC.Split(';'))
{
if(sPerson.Trim() !="")
{
m_Emailinfo.ToPerson =sPerson;
emailsend.SendMail(m_Emailinfo);
}
}
}
else
{
SendEmailByNetWebMail emailsend=new SendEmailByNetWebMail();
emailsend.Server=m_Server ;
emailsend.UserName =m_UserId ;
emailsend.Password =m_Pwd ;
emailsend.SendMail(m_Emailinfo);
}
return true;
}
catch
{
return false;
}
}
/**//// <summary>
///
/// </summary>
/// <param name="mail"></param>
/// <returns></returns>
public bool SendEmail(EmailInfo mail)
{
m_Emailinfo =mail;
return SendEmail();
}
/**//// <summary>
///
/// </summary>
/// <param name="sEmailServer"></param>
/// <param name="sUserId"></param>
/// <param name="sPwd"></param>
/// <returns></returns>
public bool SendEmail(string sEmailServer,string sUserId,string sPwd)
{
m_Server=sEmailServer;
m_UserId=sUserId;
m_Pwd=sPwd;
return SendEmail();
}
/**//// <summary>
/// 附件信息
/// </summary>
public struct AttachmentInfo
{
private string fileName;
private string bytes;
/**//// <summary>
/// 附件的文件名 [如果输入路径,则自动转换为文件名]
/// </summary>
public string FileName
{
get { return fileName; }
set { fileName = Path.GetFileName(value); }
}
/**//// <summary>
/// 附件的内容 [由经Base64编码的字节组成]
/// </summary>
public string Bytes
{
get { return bytes; }
set { if (value != bytes) bytes = value; }
}
/**//// <summary>
/// 从流中读取附件内容并构造
/// </summary>
/// <param name="ifileName">附件的文件名</param>
/// <param name="stream">流</param>
public AttachmentInfo (string ifileName, Stream stream)
{
fileName = Path.GetFileName (ifileName);
byte[] by = new byte [stream.Length];
stream.Read (by,0,(int)stream.Length); // 读取文件内容
//格式转换
bytes = Convert.ToBase64String (by); // 转化为base64编码
}
/**//// <summary>
/// 按照给定的字节构造附件
/// </summary>
/// <param name="ifileName">附件的文件名</param>
/// <param name="ibytes">附件的内容 [字节]</param>
public AttachmentInfo (string ifileName, byte[] ibytes)
{
fileName = Path.GetFileName (ifileName);
bytes = Convert.ToBase64String (ibytes); // 转化为base64编码
}
/**//// <summary>
/// 从文件载入并构造
/// </summary>
/// <param name="path"></param>
public AttachmentInfo (string path)
{
fileName = Path.GetFileName (path);
FileStream file = new FileStream (path, FileMode.Open,FileAccess.Read,FileShare.Read);
byte[] by = new byte [file.Length];
file.Read (by,0,(int)file.Length); // 读取文件内容
//格式转换
bytes = Convert.ToBase64String (by); // 转化为base64编码
file.Close ();
}
}
}
}
using System.Collections;
using System.Net.Sockets;
using System.IO;
using System.Text;
namespace NewEgg.EmailRSM
{
/**//// <summary>
/// Summary description for EmailSender.
/// </summary>
public class EmailSender
{
private SendClassType m_SendClassType=SendClassType.NetWebMail ;
private string m_Server="";
private string m_UserId="";
private string m_Pwd="";
private int port = 25;
private EmailInfo m_Emailinfo=null;
public EmailSender()
{
//
// TODO: Add constructor logic here
//
}
/**//// <summary>
/// 选择采用哪种发送方式的枚举类型
/// </summary>
public enum SendClassType
{
NetWebMail,
SocketSend
}
parameres of this class#region parameres of this class
/**//// <summary>
///
/// </summary>
/// <param name="sendclassType">发送邮件采用哪种方式</param>
public EmailSender(SendClassType sendclassType)
{
m_SendClassType=sendclassType;
}
/**//// <summary>
/// check the type of send email type
/// </summary>
public SendClassType ChoiceSendType
{
get { return m_SendClassType; }
set { if (value != m_SendClassType) m_SendClassType = value; }
}
/**//// <summary>
/// Exchange server dns & ip
/// </summary>
public string Server
{
get { return m_Server; }
set { if (value != m_Server) m_Server = value; }
}
/**//// <summary>
/// userid
/// </summary>
public string UserId
{
get { return m_UserId; }
set { if (value != m_UserId) m_UserId = value; }
}
/**//// <summary>
/// password
/// </summary>
public string PassWord
{
get { return m_Pwd; }
set { if (value != m_Pwd) m_Pwd = value; }
}
/**//// <summary>
/// SMTP port [default:25]
/// </summary>
public int Port
{
get { return port; }
set { if (value != port) port = value; }
}
/**//// <summary>
/// EMAIL信息构件
/// </summary>
public EmailInfo EmailMessage
{
get {return m_Emailinfo;}
set {m_Emailinfo=value;}
}
#endregion
/**//// <summary>
///
/// </summary>
/// <returns></returns>
public bool SendEmail()
{
try
{
if(m_SendClassType==SendClassType.SocketSend )
{
SendEmailBySocket emailsend=new SendEmailBySocket();
emailsend.Server=m_Server ;
emailsend.Port =port ;
emailsend.UserName =m_UserId ;
emailsend.Password =m_Pwd ;
//emailsend.SendMail(m_Emailinfo);
string sMailTo=m_Emailinfo.To ;
foreach(string sPerson in sMailTo.Split(';'))
{
if(sPerson.Trim() !="")
{
m_Emailinfo.ToPerson =sPerson;
emailsend.SendMail(m_Emailinfo);
}
}
foreach(string sPerson in m_Emailinfo.CC.Split(';'))
{
if(sPerson.Trim() !="")
{
m_Emailinfo.ToPerson =sPerson;
emailsend.SendMail(m_Emailinfo);
}
}
foreach(string sPerson in m_Emailinfo.BCC.Split(';'))
{
if(sPerson.Trim() !="")
{
m_Emailinfo.ToPerson =sPerson;
emailsend.SendMail(m_Emailinfo);
}
}
}
else
{
SendEmailByNetWebMail emailsend=new SendEmailByNetWebMail();
emailsend.Server=m_Server ;
emailsend.UserName =m_UserId ;
emailsend.Password =m_Pwd ;
emailsend.SendMail(m_Emailinfo);
}
return true;
}
catch
{
return false;
}
}
/**//// <summary>
///
/// </summary>
/// <param name="mail"></param>
/// <returns></returns>
public bool SendEmail(EmailInfo mail)
{
m_Emailinfo =mail;
return SendEmail();
}
/**//// <summary>
///
/// </summary>
/// <param name="sEmailServer"></param>
/// <param name="sUserId"></param>
/// <param name="sPwd"></param>
/// <returns></returns>
public bool SendEmail(string sEmailServer,string sUserId,string sPwd)
{
m_Server=sEmailServer;
m_UserId=sUserId;
m_Pwd=sPwd;
return SendEmail();
}
/**//// <summary>
/// 附件信息
/// </summary>
public struct AttachmentInfo
{
private string fileName;
private string bytes;
/**//// <summary>
/// 附件的文件名 [如果输入路径,则自动转换为文件名]
/// </summary>
public string FileName
{
get { return fileName; }
set { fileName = Path.GetFileName(value); }
}
/**//// <summary>
/// 附件的内容 [由经Base64编码的字节组成]
/// </summary>
public string Bytes
{
get { return bytes; }
set { if (value != bytes) bytes = value; }
}
/**//// <summary>
/// 从流中读取附件内容并构造
/// </summary>
/// <param name="ifileName">附件的文件名</param>
/// <param name="stream">流</param>
public AttachmentInfo (string ifileName, Stream stream)
{
fileName = Path.GetFileName (ifileName);
byte[] by = new byte [stream.Length];
stream.Read (by,0,(int)stream.Length); // 读取文件内容
//格式转换
bytes = Convert.ToBase64String (by); // 转化为base64编码
}
/**//// <summary>
/// 按照给定的字节构造附件
/// </summary>
/// <param name="ifileName">附件的文件名</param>
/// <param name="ibytes">附件的内容 [字节]</param>
public AttachmentInfo (string ifileName, byte[] ibytes)
{
fileName = Path.GetFileName (ifileName);
bytes = Convert.ToBase64String (ibytes); // 转化为base64编码
}
/**//// <summary>
/// 从文件载入并构造
/// </summary>
/// <param name="path"></param>
public AttachmentInfo (string path)
{
fileName = Path.GetFileName (path);
FileStream file = new FileStream (path, FileMode.Open,FileAccess.Read,FileShare.Read);
byte[] by = new byte [file.Length];
file.Read (by,0,(int)file.Length); // 读取文件内容
//格式转换
bytes = Convert.ToBase64String (by); // 转化为base64编码
file.Close ();
}
}
}
}