Module1.bas
上传用户:hdzyqc
上传日期:2022-07-20
资源大小:5k
文件大小:5k
源码类别:

Email客户端

开发平台:

Visual Basic

  1. Attribute VB_Name = "Module1"
  2.             
  3. '***************************************************************'Windows API/Global Declarations for :SMTP: Simple Mail Testing P'     rogram'***************************************************************None
  4.                         
  5.  
  6.         
  7. '***************************************************************' Name: SMTP: Simple Mail Testing Program' Description:Allows sending of e-mail (SMTP) directly from a VB '     app using Winsock, WITH OUT having to buy an expensive add on com'     ponet' By: Brian Anderson''' Inputs:Requires: Server Address (Name or IP), Senders & Recipei'     ent's Names, Sender & Recipient E-Mail address, Body of message'' Returns:Nothing really, does give status on sending operation''Assumes:Very straight forward. Makes sending mail from a VB prog'     ram EASY!''Side Effects:NONE!''Code provided by Planet Source Code(tm) (http://www.PlanetSource'     Code.com) 'as is', without warranties as to performance, fitness,'     merchantability,and any other warranty (whether expressed or impl'     ied).'***************************************************************Dim Response As String, Reply As Integer, DateNow As StringDim first As String, Second As String, Third As StringDim Fourth As String, Fifth As String, Sixth As String
  8. Dim Seventh As String, Eighth As String
  9. Dim Start As Single, Tmr As Single
  10. Sub SendEmail(MailServerName As String, FromName As String, FromEmailAddress As String, ToName As String, ToEmailAddress As String, EmailSubject As String, EmailBodyOfMessage As String)
  11.         Winsock1.LocalPort = 0 ' Must set local port to 0 (Zero) or you can only send 1 e-mail per program start
  12.     If Winsock1.State = sckClosed Then ' Check to see if socet is closed        DateNow = Format(Date, "Ddd") & ", " & Format(Date, "dd Mmm YYYY") & " " & Format(Time, "hh:mm:ss") & "" & " -0600"        first = "mail from:" + Chr(32) + FromEmailAddress + vbCrLf ' Get who's sending E-Mail address        Second = "rcpt to:" + Chr(32) + ToEmailAddress + vbCrLf ' Get who mail is going to        Third = "Date:" + Chr(32) + DateNow + vbCrLf ' Date when being sent        Fourth = "From:" + Chr(32) + FromName + vbCrLf ' Who's Sending        Fifth = "To:" + Chr(32) + ToNametxt + vbCrLf ' Who it going to        Sixth = "Subject:" + Chr(32) + EmailSubject + vbCrLf ' Subject of E-Mail        Seventh = EmailBodyOfMessage + vbCrLf ' E-mail message body        Ninth = "X-Mailer: EBT Reporter v 2.x" + vbCrLf ' What program sent the e-mail, customize this        Eighth = Fourth + Third + Ninth + Fifth + Sixth ' Combine for proper SMTP sending        Winsock1.Protocol = sckTCPProtocol ' Set protocol for sending
  13.         Winsock1.RemoteHost = MailServerName ' Set the server address
  14.         Winsock1.RemotePort = 25 ' Set the SMTP Port
  15.         Winsock1.Connect ' Start connection
  16.         WaitFor ("220")
  17.         StatusTxt.Caption = "Connecting...."
  18.         StatusTxt.Refresh
  19.         Winsock1.SendData ("HELO worldcomputers.com" + vbCrLf)
  20.         WaitFor ("250")
  21.         StatusTxt.Caption = "Connected"
  22.         StatusTxt.Refresh
  23.         Winsock1.SendData (First)
  24.         StatusTxt.Caption = "Sending Message"
  25.         StatusTxt.Refresh
  26.         WaitFor ("250")
  27.         Winsock1.SendData (Second)
  28.         WaitFor ("250")
  29.         Winsock1.SendData ("data" + vbCrLf)
  30.         WaitFor ("354")
  31.         Winsock1.SendData (Eighth + vbCrLf)
  32.         Winsock1.SendData (Seventh + vbCrLf)
  33.         Winsock1.SendData ("." + vbCrLf)
  34.         WaitFor ("250")
  35.         Winsock1.SendData ("quit" + vbCrLf)
  36.         StatusTxt.Caption = "Disconnecting"
  37.         StatusTxt.Refresh
  38.         WaitFor ("221")
  39.         Winsock1.Close
  40.     Else
  41.         MsgBox (Str(Winsock1.State))
  42.     End If
  43. End Sub
  44. Sub WaitFor(ResponseCode As String)
  45.     Start = Timer ' Time event so won't get stuck in loop
  46.     While Len(Response) = 0
  47.         Tmr = Start - Timer
  48.         DoEvents ' Let System keep checking for incoming response **IMPORTANT**
  49.             If Tmr > 50 Then ' Time in seconds to wait
  50.                 MsgBox "SMTP service error, timed out while waiting for response", 64, MsgTitle
  51.                 Exit Sub
  52.             End If
  53.         Wend
  54.         While Left(Response, 3) <> ResponseCode
  55.             DoEvents
  56.                 If Tmr > 50 Then
  57.                     MsgBox "SMTP service error, impromper response code. Code should have been: " + ResponseCode + " Code recieved: " + Response, 64, MsgTitle
  58.                     Exit Sub
  59.                 End If
  60.         Wend
  61.             Response = "" ' Sent response code to blank **IMPORTANT**        End Sub
  62. Private Sub Command1_Click()
  63.     SendEmail txtEmailServer.Text, txtFromName.Text, txtFromEmailAddress.Text, txtToEmailAddress.Text, txtToEmailAddress.Text, txtEmailSubject.Text, txtEmailBodyOfMessage.Text    'MsgBox ("Mail Sent")    StatusTxt.Caption = "Mail Sent"    StatusTxt.Refresh    Beep    CloseEnd Sub
  64. Private Sub Command2_Click()
  65. End Sub
  66. Private Sub Form_Load()
  67. End Sub
  68. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
  69.     Winsock1.GetData Response ' Check for incoming response *IMPORTANT*
  70. End Sub