CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VB >  基础类

高手请看-->如何用VB实现smtp服务器功能?具体过程如何?能给个例子吗?

楼主little_hero(天生我才必有用!)2001-06-21 15:25:00 在 VB / 基础类 提问

问题点数:35、回复次数:3Top

1 楼richard_1()回复于 2001-06-21 15:50:00 得分 35

用Winsock控件实现。  
  附源程序:  
  Dim   REC()   As   String  
  Private   Enum   SMTP_State  
          MAIL_CONNECT  
          MAIL_HELO  
          MAIL_FROM  
          MAIL_RCPTTO  
          MAIL_DATA  
          MAIL_DOT  
          MAIL_QUIT  
  End   Enum  
   
  Private   m_State   As   SMTP_State  
  Dim   Index   As   Integer  
  Dim   recIndex             As   Integer  
   
  '  
   
  Private   Sub   cmdClose_Click()  
   
          Unload   Me  
           
  End   Sub  
   
  Private   Sub   cmdNew_Click()  
   
          txtRecipient   =   ""  
          txtSubject   =   ""  
          txtMessage   =   ""  
           
  End   Sub  
   
  Private   Sub   cmdSend_Click()  
   
          Winsock1.Connect   Trim$(txtHost),   25  
          m_State   =   MAIL_CONNECT  
          recIndex   =   1  
           
  End   Sub  
   
  Private   Sub   Form_Load()  
          '  
          'clear   all   textboxes  
          '  
  '         For   Each   ctl   In   Me.Controls  
  '                 If   TypeOf   ctl   Is   TextBox   Then  
  '                         ctl.Text   =   ""  
  '                 End   If  
  '         Next  
          '  
                  Index   =   1  
  End   Sub  
   
   
  Private   Sub   txtRecipient_LostFocus()  
   
  Dim   I   As   Integer,   J   As   Integer,   INDEX_I   As   Integer  
           
          J   =   1  
          INDEX_I   =   1  
          For   I   =   1   To   Len(txtRecipient)  
                  If   Mid(txtRecipient,   I,   1)   =   ";"   Or   Mid(txtRecipient,   I,   1)   =   "   "   Then  
                          If   I   =   J   Then  
                                  J   =   I   +   1  
                          ElseIf   Trim(Mid(txtRecipient,   J,   I   -   J))   =   ";"   Or   Trim(Mid(txtRecipient,   J,   I   -   J))   =   ""   Then  
                                  J   =   I   +   1  
                          Else  
                                  ReDim   Preserve   REC(1   To   INDEX_I)  
                                  REC(INDEX_I)   =   Trim(Mid(txtRecipient,   J,   I   -   J))  
                                  INDEX_I   =   INDEX_I   +   1  
                                  J   =   I   +   1  
                          End   If  
                  End   If  
          Next   I  
           
          If   Not   J   =   Len(txtRecipient)   +   1   Then  
                  ReDim   Preserve   REC(1   To   INDEX_I)  
                  REC(INDEX_I)   =   Trim(Mid(txtRecipient,   J,   Len(txtRecipient)   -   J   +   1))  
          End   If  
           
          For   I   =   1   To   UBound(REC)  
                  Debug.Print   REC(I)  
          Next   I  
           
  End   Sub  
   
  Private   Sub   Winsock1_DataArrival(ByVal   bytesTotal   As   Long)  
   
          Dim   strServerResponse       As   String  
          Dim   strResponseCode           As   String  
          Dim   strDataToSend               As   String  
           
          '  
          'Retrive   data   from   winsock   buffer  
          '  
          Winsock1.GetData   strServerResponse  
          '  
          Debug.Print   strServerResponse  
          '  
          'Get   server   response   code   (first   three   symbols)  
          '  
          strResponseCode   =   Left(strServerResponse,   3)  
          '  
          'Only   these   three   codes   tell   us   that   previous  
          'command   accepted   successfully   and   we   can   go   on  
          '  
          If   strResponseCode   =   "250"   Or   _  
                strResponseCode   =   "220"   Or   _  
                strResponseCode   =   "354"   Then  
                 
                  Select   Case   m_State  
                          Case   MAIL_CONNECT  
                                  'Change   current   state   of   the   session  
                                  m_State   =   MAIL_HELO  
                                  '  
                                  'Remove   blank   spaces  
                                  strDataToSend   =   Trim$(txtSender)  
                                  '  
                                  'Retrieve   mailbox   name   from   e-mail   address  
                                  strDataToSend   =   Left$(strDataToSend,   _  
                                                                  InStr(1,   strDataToSend,   "@")   -   1)  
                                  'Send   HELO   command   to   the   server  
                                  Winsock1.SendData   "HELO   "   &   strDataToSend   &   vbCrLf  
                                  '  
                                  Debug.Print   "HELO   "   &   strDataToSend  
                                  '  
                          Case   MAIL_HELO  
                                  '  
                                  'Change   current   state   of   the   session  
                                  m_State   =   MAIL_FROM  
                                  '  
                                  'Send   MAIL   FROM   command   to   the   server  
                                  Winsock1.SendData   "MAIL   FROM:"   &   Trim$(txtSender)   &   vbCrLf  
                                  '  
                                  Debug.Print   "MAIL   FROM:"   &   Trim$(txtSender)  
                                  '  
                          Case   MAIL_FROM  
                                  '  
                                  'Change   current   state   of   the   session  
                                  '  
                                  'Send   RCPT   TO   command   to   the   server  
                                   
                                  If   recIndex   <   UBound(REC)   Then  
                                        Winsock1.SendData   "RCPT   TO:"   &   REC(recIndex)   &   vbCrLf  
                                        m_State   =   MAIL_FROM  
                                        recIndex   =   recIndex   +   1  
                                  Else  
                                        Winsock1.SendData   "RCPT   TO:"   &   REC(recIndex)   &   vbCrLf  
                                        m_State   =   MAIL_RCPTTO  
                                  End   If  
                                  '  
                                  Debug.Print   "RCPT   TO:"   &   Trim$(txtRecipient)  
                                  '  
                          Case   MAIL_RCPTTO  
                                  '  
                                  'Change   current   state   of   the   session  
                                  m_State   =   MAIL_DATA  
                                  '  
                                  'Send   DATA   command   to   the   server  
                                  Winsock1.SendData   "DATA"   &   vbCrLf  
                                  '  
                                  Debug.Print   "DATA"  
                                  '  
                          Case   MAIL_DATA  
                                  '  
                                  'Change   current   state   of   the   session  
                                  m_State   =   MAIL_DOT  
                                  '  
                                  'So   now   we   are   sending   a   message   body  
                                  'Each   line   of   text   must   be   completed   with  
                                  'linefeed   symbol   (Chr$(10)   or   vbLf)   not   with   vbCrLf  
                                  '  
                                  'Send   Subject   line  
                                  Winsock1.SendData   "Subject:"   &   txtSubject   &   vbLf  
                                  '  
                                  Debug.Print   "Subject:"   &   txtSubject  
                                  '  
                                  Dim   varLines         As   Variant  
                                  Dim   varLine           As   Variant  
                                  '  
                                  'Parse   message   to   get   lines   (for   VB6   only)  
                                  varLines   =   Split(txtMessage,   vbCrLf)  
                                  '  
                                  'Send   each   line   of   the   message  
                                  For   Each   varLine   In   varLines  
                                          Winsock1.SendData   CStr(varLine)   &   vbLf  
                                          '  
                                          Debug.Print   CStr(varLine)  
                                  Next  
                                  '  
                                  'Send   a   dot   symbol   to   inform   server  
                                  'that   sending   of   message   comleted  
                                  Winsock1.SendData   "."   &   vbCrLf  
                                  '  
                                  Debug.Print   "."  
                                  '  
                          Case   MAIL_DOT  
                                  'Change   current   state   of   the   session  
                                  m_State   =   MAIL_QUIT  
                                  '  
                                  'Send   QUIT   command   to   the   server  
                                  Winsock1.SendData   "QUIT"   &   vbCrLf  
                                  '  
                                  Debug.Print   "QUIT"  
                          Case   MAIL_QUIT  
                                  '  
                                  'Close   connection  
                                  Winsock1.Close  
                                  '  
                  End   Select  
                 
          Else  
                  '  
                  'If   we   are   here   server   replied   with  
                  'unacceptable   respose   code   therefore   we   need  
                  'close   connection   and   inform   user   about   problem  
                  '  
                  Winsock1.Close  
                  '  
                  If   Not   m_State   =   MAIL_QUIT   Then  
                          MsgBox   "SMTP   Error:   "   &   strServerResponse,   _  
                                          vbInformation,   "SMTP   Error"  
                  Else  
                          MsgBox   "Message   sent   successfuly.",   vbInformation  
                  End   If  
                  '  
          End   If  
           
  End   Sub  
   
  Private   Sub   Winsock1_Error(ByVal   Number   As   Integer,   Description   As   String,   ByVal   Scode   As   Long,   ByVal   Source   As   String,   ByVal   HelpFile   As   String,   ByVal   HelpContext   As   Long,   CancelDisplay   As   Boolean)  
   
          MsgBox   "Winsock   Error   number   "   &   Number   &   vbCrLf   &   _  
                          Description,   vbExclamation,   "Winsock   Error"  
   
  End   Sub  
  Top

2 楼little_hero(天生我才必有用!)回复于 2001-06-21 16:03:00 得分 0

老兄你误会了,我要的并非只是发那么简单,也就是说不用设smtp地址和端口,这些都由自已的程序使用smtp协议编程来实现的,我要的是服务器程序,用于邮件系统,不过还是谢谢你!  
  Top

3 楼little_hero(天生我才必有用!)回复于 2001-06-23 16:07:00 得分 0

请高手指点?Top

相关问题

  • VB中如何得到本机中默认的发送邮件的服务器地址(SMTP服务器)?
  • 哪位大哥可以给我一个不需要smtp服务器的发送邮件的例子???
  • 谁知道sina的smtp服务器?
  • 如何设计SMTP服务器?
  • smtp服务器端口是多少啊
  • SMTP邮件服务器的问题!
  • 怎样连接SMTP服务器?
  • 用IIS轻松架设SMTP服务器- -
  • 怎样做我的smtp服务器?
  • VB 连接 SQL2000服务器,怎么连

关键词

  • txtrecipient
  • smtp
  • mail
  • ctl
  • mid
  • trim
  • end subprivate sub
  • integer
  • click
  • state

得分解答快速导航

  • 帖主:little_hero
  • richard_1

相关链接

  • Visual Basic类图书
  • Visual Basic类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo