Results 1 to 40 of 56

Thread: Simple VB emailer

Threaded View

  1. #1

    Thread Starter
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Simple VB emailer

    Here is a class you can use to send email from VB. I wrote it years ago, but have seen a few requests for info on emailing.
    Here is an example of how to use the class

    Edit: Before using the class you'll have to add a refernce to MSWINSCK.OCX
    Goto Project\References Browse to system32\MSWINSOCK.OCX
    VB Code:
    1. Option Explicit
    2.  
    3. Private WithEvents SMTP As clsSMTP
    4.  
    5. Private Sub Command1_Click()
    6.  
    7. With SMTP
    8.  'who is the email from?
    9.  .Sender = "YoMama"
    10.  'who is it going to?
    11.  .Recipient = "[email protected]"
    12.  'your SMTP server
    13.  .SMTPhost = "smtp.myserver.com"
    14.  'optional subject line
    15.  .Subject = "SMTP test"
    16.  'make connection to the server
    17.  .OpenMail
    18.  'did connection succeed?
    19.  If Not .Connected Then Exit Sub
    20.  'you can send as many lines as you want here
    21.  .Send "Bill,"
    22.  .Send "This is a test of your wonderful SMTP client!"
    23.  'make sure you close or nothing will get sent
    24.  .CloseMail
    25. End With
    26.  
    27. End Sub
    28.  
    29. Private Sub Form_Load()
    30.     Set SMTP = New clsSMTP
    31. End Sub
    32.  
    33.  
    34. Private Sub SMTP_Error(Code As Integer, Message As String)
    35.     Debug.Print "SMTP Error "; Code, Message
    36. End Sub
    37.  
    38. Private Sub SMTP_ServerResponse(Reply As Integer, Message As String)
    39.     Debug.Print Reply, Message
    40. End Sub
    Pretty simple, Now here is the class
    VB Code:
    1. Option Explicit
    2.  
    3. Private mvarConnected As Boolean
    4. Private mvarRecipient As String
    5. Private mvarSender As String
    6. Private mvarSMTPhost As String
    7. Private mvarSubject As String
    8.  
    9. Private WithEvents Winsock As MSWinsockLib.Winsock
    10.  
    11. Const OK = 250
    12. Const Connection = 220
    13. Const ReadyforData = 354
    14. Const UserUnknown = 550
    15.  
    16. Dim Reply As Integer
    17. Dim Message As String
    18. Dim MyName As String
    19. Dim ErrMSg As String
    20.  
    21. Event Error(Code As Integer, Message As String)
    22. Event ServerResponse(Reply As Integer, Message As String)
    23.  
    24. Private Function WaitFor(R As Integer) As Boolean
    25. Dim A
    26.     A = Timer + 3
    27.     While Not Reply = R And A > Timer
    28.     DoEvents
    29.     Wend
    30.     If A < Timer Then
    31.         WaitFor = True
    32.     Else
    33.         WaitFor = False
    34.     End If
    35.     Reply = 0
    36. End Function
    37.  
    38. Public Property Let Subject(ByVal vData As String)
    39.     mvarSubject = vData
    40. End Property
    41.  
    42. Public Property Get Subject() As String
    43.     Subject = mvarSubject
    44. End Property
    45.  
    46. Public Property Let SMTPhost(ByVal vData As String)
    47.     mvarSMTPhost = vData
    48. End Property
    49.  
    50. Public Property Get SMTPhost() As String
    51.     SMTPhost = mvarSMTPhost
    52. End Property
    53.  
    54. Public Sub Send(Message As String)
    55.  Winsock.SendData Message & vbCrLf
    56. End Sub
    57.  
    58. Public Property Let Sender(ByVal vData As String)
    59.     mvarSender = vData
    60. End Property
    61.  
    62. Public Property Get Sender() As String
    63.     Sender = mvarSender
    64. End Property
    65.  
    66. Public Property Let Recipient(ByVal vData As String)
    67.     mvarRecipient = vData
    68. End Property
    69.  
    70. Public Property Get Recipient() As String
    71.     Recipient = mvarRecipient
    72. End Property
    73.  
    74. Public Property Get Connected() As Boolean
    75.     Connected = mvarConnected
    76. End Property
    77.  
    78. Public Sub OpenMail()
    79.     Reply = 0
    80.     'connect to SMTP server
    81.     Winsock.Connect mvarSMTPhost, 25
    82.     If WaitFor(Connection) Then
    83.         GiveError
    84.         Exit Sub
    85.     End If
    86.     'send hello and wait for OK
    87.     Winsock.SendData "helo " & mvarSMTPhost & vbCrLf
    88.     If WaitFor(OK) Then
    89.         GiveError
    90.         Winsock.Close
    91.         Exit Sub
    92.     End If
    93.     'send sender's name
    94.     MyName = WhoAmI
    95.      If InStr(mvarSender, "@") = 0 Then
    96.         Winsock.SendData "Mail From:<" & mvarSender & "@" & MyName & ">" & vbCrLf
    97.     Else
    98.         Winsock.SendData "Mail From:<" & mvarSender & ">" & vbCrLf
    99.     End If
    100.     If WaitFor(OK) Then
    101.         GiveError
    102.         Winsock.Close
    103.         Exit Sub
    104.     End If
    105.     'send recipient
    106.     Winsock.SendData "RCPT TO:<" & mvarRecipient & ">" & vbCrLf
    107.     If WaitFor(OK) Then
    108.         GiveError
    109.         Winsock.Close
    110.         Exit Sub
    111.     End If
    112.     'make ready for data
    113.     Winsock.SendData "Data" & vbCrLf
    114.     If WaitFor(ReadyforData) Then
    115.         GiveError
    116.         Winsock.Close
    117.         Exit Sub
    118.     End If
    119.     'Send Date:
    120.     Winsock.SendData "Date: " & Format(Date, " dd mmm yy ") & Time & vbCrLf
    121.     'To:
    122.     Winsock.SendData "TO:<" & mvarRecipient & ">" & vbCrLf
    123.     'From:
    124.     Winsock.SendData "From: <" & mvarSender & "@" & MyName & ">" & vbCrLf
    125.     'Subject:
    126.     If Not mvarSubject = "" Then Winsock.SendData "Subject:" & mvarSubject & vbCrLf
    127.     mvarConnected = True
    128. End Sub
    129.  
    130. Private Function WhoAmI() As String
    131. Dim X As Integer, Y As Integer
    132.     X = InStr(Message, "Hello")
    133.     Y = InStr(Message, "[")
    134.     If X = 0 Or Y = 0 Then
    135.         WhoAmI = Winsock.LocalIP
    136.     Else
    137.         X = X + 6
    138.         Y = Y - 1 - X
    139.         WhoAmI = Mid$(Message, X, Y)
    140.     End If
    141. End Function
    142.  
    143. Public Sub CloseMail()
    144.     Winsock.SendData "." & vbCrLf
    145.     If WaitFor(OK) Then
    146.         GiveError
    147.         Exit Sub
    148.     End If
    149.     Winsock.SendData "QUIT"
    150.     Winsock.Close
    151. End Sub
    152.  
    153. Private Sub GiveError()
    154.  Dim Message As String
    155.  Select Case Reply
    156.  Case Is = 10061
    157.   Message = "Can't Connect to " & mvarSMTPhost
    158.  Case Is = 11001
    159.   Message = mvarSMTPhost & " is not a valid name or address."
    160.  Case Is = 550
    161.   Message = "User " & mvarRecipient & " not known to server."
    162.  Case Is = 0
    163.   Message = "Timeout waiting for server response"
    164.  Case Is = 500
    165.   Message = "Server does not recognize a command sent."
    166.  Case Is = 553
    167.   Message = "Invalid recipient name"
    168.  Case Is = 1
    169.   Message = "You must set Host, Recipient and Sender before connecting"
    170.  Case Else
    171.   Message = "Mail tool error."
    172.  End Select
    173.  
    174.  RaiseEvent Error(Reply, Message)
    175. End Sub
    176.  
    177. Private Sub Class_Initialize()
    178.     Set Winsock = New MSWinsockLib.Winsock
    179. End Sub
    180.  
    181. Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)
    182. Dim X As String
    183.     Winsock.GetData X
    184.     Reply = Val(X)
    185.     X = Trim$(Mid(X, 4))
    186.     RaiseEvent ServerResponse(Reply, X)
    187.     Message = X
    188. End Sub
    189.  
    190. Private Sub Winsock_Error(ByVal Number As Integer, Description As String, _
    191. ByVal Scode As Long, ByVal Source As String, _
    192. ByVal HelpFile As String, ByVal HelpContext As Long, _
    193. CancelDisplay As Boolean)
    194.     Reply = Number
    195. End Sub
    Last edited by moeur; Jul 29th, 2005 at 08:11 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width