基于Socket与NET.WEBMAIL发送邮件的源代码(二)

下面两个Class分别是基于WebMail和Socket:

  1 None.gif using  System;
  2 None.gif using  System.Web.Mail ;
  3 None.gif using  System.Collections;
  4 None.gif using  System.Text;
  5 None.gif
  6 None.gif namespace  NewEgg.EmailRSM
  7 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  8ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
  9InBlock.gif    /// Summary description for SendEmailByNetWebMail.
 10ExpandedSubBlockEnd.gif    /// </summary>

 11InBlock.gif    public class SendEmailByNetWebMail
 12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 13InBlock.gif        private string m_server = "";
 14InBlock.gif        private string m_userName = "";
 15InBlock.gif        private string m_password = "";
 16InBlock.gif        private EmailInfo m_emailinfo=null;
 17InBlock.gif
 18InBlock.gif        public SendEmailByNetWebMail()
 19ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 20InBlock.gif            //
 21InBlock.gif            // TODO: Add constructor logic here
 22InBlock.gif            //
 23ExpandedSubBlockEnd.gif        }

 24InBlock.gif
 25ContractedSubBlock.gifExpandedSubBlockStart.gif        属性#region 属性
 26ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 27InBlock.gif        /// EMAIL信息构件
 28ExpandedSubBlockEnd.gif        /// </summary>

 29InBlock.gif        public EmailInfo EmailMessage
 30ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 31ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gif{return m_emailinfo;}
 32ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{m_emailinfo=value;}
 33ExpandedSubBlockEnd.gif        }

 34InBlock.gif
 35ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 36InBlock.gif        /// SMTP服务器域名
 37ExpandedSubBlockEnd.gif        /// </summary>

 38InBlock.gif        public string Server 
 39ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 40ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn m_server; }
 41ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gifif (value != m_server) m_server = value; }
 42ExpandedSubBlockEnd.gif        }
 
 43InBlock.gif
 44ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 45InBlock.gif        /// 用户名 [需要身份验证时]
 46ExpandedSubBlockEnd.gif        /// </summary>

 47InBlock.gif        public string UserName 
 48ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 49ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn m_userName; }
 50ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gifif (value != m_userName) m_userName = value; }
 51ExpandedSubBlockEnd.gif        }
 
 52InBlock.gif
 53ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 54InBlock.gif        /// 密码 [需要身份验证时]
 55ExpandedSubBlockEnd.gif        /// </summary>

 56InBlock.gif        public string Password 
 57ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 58ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn m_password; }
 59ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gifif (value != m_password) m_password = value; }
 60ExpandedSubBlockEnd.gif        }
 
 61ExpandedSubBlockEnd.gif        #endregion

 62InBlock.gif
 63ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 64InBlock.gif        /// 发送邮件
 65ExpandedSubBlockEnd.gif        /// </summary>

 66InBlock.gif        public bool SendMail()
 67ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 68InBlock.gif            try
 69ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 70InBlock.gif                MailMessage mailObj =new MailMessage();
 71InBlock.gif                mailObj.From =m_emailinfo.From ;
 72InBlock.gif                mailObj.To =m_emailinfo.To ;
 73InBlock.gif                mailObj.Subject =m_emailinfo.Subject ;
 74InBlock.gif                mailObj.Body =m_emailinfo.Body;
 75InBlock.gif                if(m_emailinfo.IsHtml)
 76ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 77InBlock.gif                    mailObj.BodyFormat=MailFormat.Html;
 78ExpandedSubBlockEnd.gif                }

 79InBlock.gif                else
 80ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 81InBlock.gif                    mailObj.BodyFormat=MailFormat.Text ;
 82ExpandedSubBlockEnd.gif                }

 83InBlock.gif                mailObj.Cc=m_emailinfo.CC ;
 84InBlock.gif                mailObj.Bcc =m_emailinfo.BCC ;
 85InBlock.gif
 86InBlock.gif                switch(m_emailinfo.MailEncoding)
 87ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 88InBlock.gif                    case "GB2312":
 89InBlock.gif                        mailObj.BodyEncoding=System.Text.Encoding.GetEncoding("GB2312");
 90InBlock.gif                        break;
 91InBlock.gif                    case "UTF-8":
 92InBlock.gif                        mailObj.BodyEncoding=System.Text.Encoding.GetEncoding("UTF-8");
 93InBlock.gif                        break;
 94InBlock.gif                    default:
 95InBlock.gif                        mailObj.BodyEncoding=Encoding.Default;
 96InBlock.gif                        break;
 97ExpandedSubBlockEnd.gif                }

 98InBlock.gif                switch(m_emailinfo.Priority)
 99ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
100InBlock.gif                    case 1:
101InBlock.gif                        mailObj.Priority=MailPriority.High ;
102InBlock.gif                        break;
103InBlock.gif                    case 2:
104InBlock.gif                        mailObj.Priority=MailPriority.Normal  ;
105InBlock.gif                        break;
106InBlock.gif                    case 3:
107InBlock.gif                        mailObj.Priority=MailPriority.Low  ;
108InBlock.gif                        break;
109InBlock.gif                    default:
110InBlock.gif                        mailObj.Priority=MailPriority.Normal  ;
111InBlock.gif                        break;
112ExpandedSubBlockEnd.gif                }

113InBlock.gif                for (int i = 0; i < m_emailinfo.Attachments.Count; i++)
114ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
115InBlock.gif                    MailAttachment myAttachment = new MailAttachment(m_emailinfo.Attachments[i].ToString() );
116InBlock.gif                    mailObj.Attachments.Add(myAttachment);
117ExpandedSubBlockEnd.gif                }

118InBlock.gif
119InBlock.gif                mailObj.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate""1");   //basic authentication 
120InBlock.gif
121InBlock.gif                mailObj.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", m_userName); //set your username here 
122InBlock.gif
123InBlock.gif                mailObj.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", m_password ); //set your password here 
124InBlock.gif
125InBlock.gif                mailObj.BodyFormat =MailFormat.Html;
126InBlock.gif                mailObj.Priority = MailPriority.High;
127InBlock.gif                SmtpMail.SmtpServer =m_server ;
128InBlock.gif                SmtpMail.Send(mailObj);
129InBlock.gif                return true;
130ExpandedSubBlockEnd.gif            }

131InBlock.gif            catch
132ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
133InBlock.gif                return false;
134ExpandedSubBlockEnd.gif            }

135ExpandedSubBlockEnd.gif        }

136InBlock.gif
137ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
138InBlock.gif        /// 发送Email 
139InBlock.gif        /// </summary>
140InBlock.gif        /// <param name="email"></param>
141ExpandedSubBlockEnd.gif        /// <returns></returns>

142InBlock.gif        public bool SendMail(EmailInfo email)
143ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
144InBlock.gif            m_emailinfo=email;
145InBlock.gif            return  SendMail();
146InBlock.gif            
147ExpandedSubBlockEnd.gif        }

148InBlock.gif
149InBlock.gif        
150ExpandedSubBlockEnd.gif    }

151ExpandedBlockEnd.gif}

152 None.gif

None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.Net.Sockets;
None.gif
using  System.IO;
None.gif
using  System.Text;
None.gif
using  System.Threading;
None.gif
None.gif
namespace  NewEgg.EmailRSM
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for SendEmailBySocket.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class SendEmailBySocket
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private string server = "";
InBlock.gif        
private int port = 25;
InBlock.gif        
private string userName = "";
InBlock.gif        
private string password = "";
InBlock.gif        
private EmailInfo emailinfo=null;
InBlock.gif 
InBlock.gif        
public SendEmailBySocket()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: Add constructor logic here
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
属性#region 属性
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// EMAIL信息构件
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public EmailInfo EmailMessage
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gif{return emailinfo;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{emailinfo=value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// SMTP服务器域名
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string Server 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn server; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gifif (value != server) server = value; }
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// SMTP服务器端口 [默认为25]
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public int Port 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn port; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gifif (value != port) port = value; }
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 用户名 [需要身份验证时]
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string UserName 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn userName; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gifif (value != userName) userName = value; }
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 密码 [需要身份验证时]
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string Password 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn password; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gifif (value != password) password = value; }
ExpandedSubBlockEnd.gif        }
 
InBlock.gif    
InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="mail"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public bool SendMail(EmailInfo mail)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            emailinfo 
=mail;
InBlock.gif            
return SendMail();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 发送邮件
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public bool SendMail()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 创建TcpClient对象, 并建立连接
InBlock.gif
            TcpClient tcp = null;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                tcp 
= new TcpClient (server, port);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new Exception ("无法连接服务器");
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            ReadString (tcp.GetStream());
//获取连接信息
InBlock.gif
InBlock.gif            
// 开始进行服务器认证
InBlock.gif            
// 如果状态码是250则表示操作成功
InBlock.gif
            if (!Command (tcp.GetStream(), "EHLO Localhost""250"))
InBlock.gif                
throw new Exception ("登陆阶段失败");
InBlock.gif
InBlock.gif            
if (userName != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// 需要身份验证
InBlock.gif
                if (!Command (tcp.GetStream(), "AUTH LOGIN""334"))
InBlock.gif                    
throw new Exception ("身份验证阶段失败");
InBlock.gif             
InBlock.gif                
string nameB64 = ToBase64 (userName); // 此处将username转换为Base64码
InBlock.gif
                if (!Command (tcp.GetStream(), nameB64, "334"))
InBlock.gif                    
throw new Exception ("身份验证阶段失败");
InBlock.gif                 
InBlock.gif                
string passB64 = ToBase64 (password); // 此处将password转换为Base64码
InBlock.gif
                if (!Command (tcp.GetStream(), passB64, "235"))
InBlock.gif                    
throw new Exception ("身份验证阶段失败");
InBlock.gif                
ExpandedSubBlockEnd.gif            }

InBlock.gif            Thread.Sleep(
100);
InBlock.gif
InBlock.gif            
// 准备发送
InBlock.gif
            WriteString (tcp.GetStream(), "mail From: " + emailinfo.From );
InBlock.gif            WriteString (tcp.GetStream(), 
"rcpt to: " + emailinfo.ToPerson);
InBlock.gif            WriteString (tcp.GetStream(), 
"data");
InBlock.gif
InBlock.gif            
// 发送邮件头
InBlock.gif
            WriteString (tcp.GetStream(), "Date: " + DateTime.Now); // 时间
InBlock.gif
            WriteString (tcp.GetStream(), "From: " + emailinfo.FromName  + "<" + emailinfo.From  + ">"); // 发件人
InBlock.gif
            WriteString (tcp.GetStream(), "Subject: " + emailinfo.Subject ); // 主题
InBlock.gif
            WriteString (tcp.GetStream(), "To:" + emailinfo.ToName  + "<" + emailinfo.To  + ">"); // 收件人
InBlock.gif
            WriteString (tcp.GetStream(), "Cc:" + emailinfo.CC   + "<" + emailinfo.CC   + ">"); // 收件人
InBlock.gif
            WriteString (tcp.GetStream(), "Bcc:" + emailinfo.BCC   + "<" + emailinfo.BCC  + ">"); // 收件人
InBlock.gif
InBlock.gif            
//邮件格式 
InBlock.gif
            WriteString (tcp.GetStream(), "Content-Type: multipart/mixed; boundary=\"unique-boundary-1\"");
InBlock.gif            WriteString (tcp.GetStream(), 
"Reply-To:" + emailinfo.From ); // 回复地址
InBlock.gif
            WriteString (tcp.GetStream(), "X-Priority:" + emailinfo.Priority.ToString() ); // 优先级
InBlock.gif
            WriteString (tcp.GetStream(), "MIME-Version:1.0"); // MIME版本
InBlock.gif
InBlock.gif            
// 数据ID,随意
InBlock.gif
            WriteString (tcp.GetStream(), "Content-Transfer-Encoding:" + emailinfo.MailEncoding ); // 内容编码
InBlock.gif
            WriteString (tcp.GetStream(), "X-Mailer:NewEgg.MailSender"); // 邮件发送者
InBlock.gif
            WriteString (tcp.GetStream(), "");
InBlock.gif
InBlock.gif            
if(emailinfo.Attachments.Count>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                WriteString (tcp.GetStream(), ToBase64 (
"This is a multi-part message in MIME format.")); 
InBlock.gif                WriteString (tcp.GetStream(), 
""); 
InBlock.gif
InBlock.gif                
// 从此处开始进行分隔输入
InBlock.gif
                WriteString (tcp.GetStream(), "--unique-boundary-1");
InBlock.gif
InBlock.gif                
// 在此处定义第二个分隔符
InBlock.gif
                
InBlock.gif                WriteString (tcp.GetStream(), 
"Content-Type: multipart/alternative;Boundary=\"unique-boundary-2\"");
InBlock.gif                
InBlock.gif                WriteString (tcp.GetStream(), 
"");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            Thread.Sleep(
100);
InBlock.gif            
if(!emailinfo.IsHtml)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// 文本信息
InBlock.gif
                if(emailinfo.Attachments.Count>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    WriteString (tcp.GetStream(), 
"--unique-boundary-2");
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    WriteString (tcp.GetStream(), 
"--unique-boundary-1"); 
ExpandedSubBlockEnd.gif                }

InBlock.gif                WriteString (tcp.GetStream(), 
"Content-Type: text/plain;charset=" + emailinfo.LanguageEncoding);
InBlock.gif                WriteString (tcp.GetStream(), 
"Content-Transfer-Encoding:" + emailinfo.MailEncoding );
InBlock.gif                WriteString (tcp.GetStream(), 
"");
InBlock.gif                WriteString (tcp.GetStream(), emailinfo.Body );
InBlock.gif                WriteString (tcp.GetStream(), 
"");//一个部分写完之后就写如空信息,分段
InBlock.gif
                if(emailinfo.Attachments.Count>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    WriteString (tcp.GetStream(), 
"--unique-boundary-2--");//分隔符的结束符号,尾巴后面多了--
ExpandedSubBlockEnd.gif
                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    WriteString (tcp.GetStream(), 
"--unique-boundary-1"); 
ExpandedSubBlockEnd.gif                }

InBlock.gif                WriteString (tcp.GetStream(), 
"");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//html信息
InBlock.gif

InBlock.gif                
if(emailinfo.Attachments.Count>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    WriteString (tcp.GetStream(), 
"--unique-boundary-2"); 
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    WriteString (tcp.GetStream(), 
"--unique-boundary-1"); 
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                WriteString (tcp.GetStream(), 
"Content-Type: text/html;charset=" + emailinfo.LanguageEncoding ); 
InBlock.gif                WriteString (tcp.GetStream(), 
"Content-Transfer-Encoding:" + emailinfo.MailEncoding ); 
InBlock.gif                WriteString (tcp.GetStream(), 
""); 
InBlock.gif                WriteString (tcp.GetStream(), emailinfo.HtmlBody ); 
InBlock.gif                WriteString (tcp.GetStream(), 
""); 
InBlock.gif                
if(emailinfo.Attachments.Count>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    WriteString (tcp.GetStream(), 
"--unique-boundary-2--");//分隔符的结束符号,尾巴后面多了-- 
InBlock.gif
                    
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    WriteString (tcp.GetStream(), 
"--unique-boundary-1"); 
ExpandedSubBlockEnd.gif                }

InBlock.gif                WriteString (tcp.GetStream(), 
"");
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if(emailinfo.Attachments.Count>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Thread.Sleep(
100);
InBlock.gif                
// 发送附件
InBlock.gif                
// 对文件列表做循环
InBlock.gif
                for (int i = 0; i < emailinfo.Attachments.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    WriteString (tcp.GetStream(), 
"--unique-boundary-1"); // 邮件内容分隔符
InBlock.gif
                    WriteString (tcp.GetStream(), "Content-Type: application/octet-stream;name=\"" + ((EmailSender.AttachmentInfo)emailinfo.Attachments[i]).FileName + "\""); // 文件格式
InBlock.gif
                    WriteString (tcp.GetStream(), "Content-Transfer-Encoding: base64"); // 内容的编码
InBlock.gif
                    WriteString (tcp.GetStream(), "Content-Disposition:attachment;name=\"" + ((EmailSender.AttachmentInfo)emailinfo.Attachments[i]).FileName + "\""); // 文件名
InBlock.gif
                    WriteString (tcp.GetStream(), "");
InBlock.gif                    WriteString (tcp.GetStream(), ((EmailSender.AttachmentInfo)emailinfo.Attachments[i]).Bytes); 
// 写入文件的内容
InBlock.gif
                    WriteString (tcp.GetStream(), "");
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            Thread.Sleep(
100);
InBlock.gif            Command (tcp.GetStream(), 
".""250"); // 最后写完了,输入"." 
InBlock.gif
InBlock.gif            
// 关闭连接
InBlock.gif
            tcp.Close ();
InBlock.gif            
return true;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 向流中写入字符
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="netStream">来自TcpClient的流</param>
ExpandedSubBlockEnd.gif        
/// <param name="str">写入的字符</param>

InBlock.gif        protected void WriteString (NetworkStream netStream, string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            str 
= str + "\r\n"// 加入换行符
InBlock.gif            
// 将命令行转化为byte[]
InBlock.gif
            byte[] bWrite = Encoding.GetEncoding(emailinfo.LanguageEncoding).GetBytes(str.ToCharArray());
InBlock.gif
InBlock.gif            
// 由于每次写入的数据大小是有限制的,那么我们将每次写入的数据长度定在75个字节,一旦命令长度超过了75,就分步写入。
InBlock.gif
            int start=0;
InBlock.gif            
int length=bWrite.Length;
InBlock.gif            
int page=0;
InBlock.gif            
int size=75;
InBlock.gif            
int count=size;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (length>75)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
// 数据分页
InBlock.gif
                    if ((length/size)*size<length)
InBlock.gif                        page
=length/size+1;
InBlock.gif                    
else
InBlock.gif                        page
=length/size;
InBlock.gif                    
for (int i=0;i<page;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        start
=i*size;
InBlock.gif                        
if (i==page-1)
InBlock.gif                            count
=length-(i*size);
InBlock.gif                        netStream.Write(bWrite,start,count);
// 将数据写入到服务器上
ExpandedSubBlockEnd.gif
                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    netStream.Write(bWrite,
0,bWrite.Length);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// 忽略错误
ExpandedSubBlockEnd.gif
            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 从流中读取字符
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="netStream">来自TcpClient的流</param>
ExpandedSubBlockEnd.gif        
/// <returns>读取的字符</returns>

InBlock.gif        protected string ReadString (NetworkStream netStream)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string sp = null;
InBlock.gif            
byte[] by = new byte[1024];
InBlock.gif            
int size = netStream.Read(by,0,by.Length);// 读取数据流
InBlock.gif
            if (size > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sp 
= Encoding.Default.GetString(by);// 转化为String
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
return sp;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 发出命令并判断返回信息是否正确
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="netStream">来自TcpClient的流</param>
InBlock.gif        
/// <param name="command">命令</param>
InBlock.gif        
/// <param name="state">正确的状态码</param>
ExpandedSubBlockEnd.gif        
/// <returns>是否正确</returns>

InBlock.gif        protected bool Command (NetworkStream netStream, string command, string state)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string sp=null;
InBlock.gif            
bool success=false;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                WriteString (netStream, command);
// 写入命令
InBlock.gif
                sp = ReadString (netStream);// 接受返回信息
InBlock.gif
                if (sp.IndexOf(state) != -1)// 判断状态码是否正确
InBlock.gif
                    success=true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// 忽略错误
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
return success;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 字符串编码为Base64
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="str">字符串</param>
ExpandedSubBlockEnd.gif        
/// <returns>Base64编码的字符串</returns>

InBlock.gif        protected string ToBase64 (string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
byte[] by = Encoding.Default.GetBytes (str.ToCharArray());
InBlock.gif                str 
= Convert.ToBase64String (by);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// 忽略错误
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
return str;
ExpandedSubBlockEnd.gif        }

InBlock.gif 
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/Jinqin/archive/2006/01/14/317074.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值