Page 1 of 2 12 LastLast
Results 1 to 40 of 56

Thread: Simple VB emailer

  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 = "whomever@whereever.net"
    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.

  2. #2
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Simple VB emailer

    nice job moeur

  3. #3
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Simple VB emailer

    Very nice. Another useful thing to add onto this class is to allow one to make an MIME encoded email enabling you to attach additional files to the message.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  4. #4
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Simple VB emailer

    Or maybe to authenticate the username and password that some SMTP servers require


    Has someone helped you? Then you can Rate their helpful post.

  5. #5
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Simple VB emailer

    Why am I getting an error here? I have the clsSMTP class named right, with a winsock control named Winsock on a form, with a button named Commannd1, so what is missing?

    VB Code:
    1. Private Sub Class_Initialize()
    2.     Set Winsock = New MSWinsockLib.Winsock
    3. End Sub

    Invalid use of NEW keyword
    Thanks.

    [Simple Send Email project]

  6. #6

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

    Re: Simple VB emailer

    Before using the class you'll have to add a refernce to MSWINSCK.OCX
    Goto Project\References Browse to system32\MSWINSOCK.OCX

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Simple VB emailer

    I had it loaded as a component. Thanks.
    Now, though I'm getting a SMTP timeout error.
    I'll try it again tomorrow.

    Thanks.

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Simple VB emailer

    One quick question: In the WaitFor function, you have A declared as a Variant (because you didn't explicitly declare it as anything else)

    Was that intentional? Is a Variant required here?

  9. #9

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

    Re: Simple VB emailer

    A could be single if you wanted.

  10. #10
    Addicted Member
    Join Date
    Nov 2001
    Location
    San Diego, CA
    Posts
    159

    Re: Simple VB emailer

    Just what the doctor ordered and can be used as is. However, I found I am having problem with the return address. If I use "YoMamma" or something else made up, it blows up in my face. However, it I use my "real" address, it works perfectly.

    BUT:

    It adds "@127.0.0.1" after the address. if the person tries to respond to it, they get an error message "553 Invalid address syntax". Any way of getting rid of that last portion?
    ttfn
    Kicker

  11. #11
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Simple VB emailer

    That is your ISP trying to prevent spam. I have the same thing, even when I'm not using their smtp server.

  12. #12

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

    Re: Simple VB emailer

    It adds "@127.0.0.1" after the address. if the person tries to respond to it, they get an error message "553 Invalid address syntax". Any way of getting rid of that last portion?
    I changed the code in the OpenMail routine a little bit on 7/29.
    VB Code:
    1. 'send sender's name
    2.     MyName = WhoAmI
    3.      If InStr(mvarSender, "@") = 0 Then
    4.         Winsock.SendData "Mail From:<" & mvarSender & "@" & MyName & ">" & vbCrLf
    5.     Else
    6.         Winsock.SendData "Mail From:<" & mvarSender & ">" & vbCrLf
    7.     End If
    See if your code reflects this change.

  13. #13
    Addicted Member
    Join Date
    Nov 2001
    Location
    San Diego, CA
    Posts
    159

    Re: Simple VB emailer

    See if your code reflects this change.
    Unfortunately it does. I only pulled the code down a couple of days ago and it is using this latter section.

    I can "live" with it, just prefer to have it cleaner.
    ttfn
    Kicker

  14. #14

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

    Re: Simple VB emailer

    I assume this is the .Sender address you're talking about.
    put some Debug.prints in there somewhere to see what's going on.
    what is mvarsender equal to and which line does it send?

  15. #15
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Re: Simple VB emailer

    How could I use Yahoo in your class?
    Regards,

    â„¢

    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  16. #16

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

    Re: Simple VB emailer

    How could I use Yahoo in your class?
    if Yahoo requires username and password authentification, then it won't work unless we add something to handle that. I could do that, but I'd need a server to test it on and mine doesn't require such things.

  17. #17
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Pakistan
    Posts
    436

    Re: Simple VB emailer

    Why i am getting error "Name conflicts with existing module, project or object library" when adding mswinsck.ocx file from prefrences.

  18. #18

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

    Re: Simple VB emailer

    Don't load the Winsock control into the toolbox.

  19. #19
    Addicted Member
    Join Date
    Nov 2001
    Location
    San Diego, CA
    Posts
    159

    Re: Simple VB emailer

    Don't load the Winsock control into the toolbox.
    I don't see where I have the Winsock control in the toolbox. I went to components and references and don't see the Winsock. Other than that, where would I check?
    ttfn
    Kicker

  20. #20

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

    Re: Simple VB emailer

    This error happens if you try to load MSWINSCK.OCX twice either in references or components.

  21. #21
    Addicted Member
    Join Date
    Nov 2001
    Location
    San Diego, CA
    Posts
    159

    Re: Simple VB emailer

    This error happens if you try to load MSWINSCK.OCX twice either in references or components
    Here is what I have loaded. Please take a look at them. I don't see where I have MSWINSCK.OCX loaded twice

    References
    VB for applications
    VB runtime objects & procedures
    VBobjects and procedures
    MS DAO 3.6 object library
    MS ActiveX Data Objectrs 2.5 library
    MS Data binding collection

    Components
    MS ADO Data Control 6.0 (OLEDB)
    MS Calendar Control 9.0
    MS Comm Controll 6.0
    MS Common dialog control 6.0 (SP3)
    MS Data Grid Control 6.0 (OLEDB)
    MS Flex grid control 6.0
    MS Masked edit control 6.0
    MS Multimedia control 6.0
    MS Rich textbox control 6.0 (SP4)
    MS Windows common controls 5.0 (SP2)
    MS Windows common controls-2 (SP4)

    Designers
    Data Environment
    Data Report
    DHTML Page
    Webclass

    Insertable Objects
    MS Calendar Control 9.0

    I really appreciate the help
    ttfn
    Kicker

  22. #22
    New Member
    Join Date
    Aug 2005
    Posts
    8

    Re: Simple VB emailer

    Uhh, i get a strange errror

    Userdefined type not defined

    and vb selects: Set Winsock = New mswinsocklib.Winsock
    Last edited by impClaw; Aug 13th, 2005 at 04:57 AM.

  23. #23

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

    Re: Simple VB emailer

    I would start a new project, add the reference, and cut and past the code.

  24. #24
    New Member
    Join Date
    Aug 2005
    Posts
    8

    Re: Simple VB emailer

    well, i have to do it in this project, is that possible?

    I could send the project...

    Also what shall the winsock class be named?
    EDIT: clsSMPT probable, im such a noob

    EDIT: ok, i use vb 5.0, that might be a problem.... since i have to add the winsock as a object not a referance....
    Last edited by impClaw; Aug 13th, 2005 at 01:05 PM.

  25. #25

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

    Re: Simple VB emailer

    It's been a long time since I used VB5. Try this prpject to see if it works. Then you can incorporate it into your project.
    Attached Files Attached Files

  26. #26
    New Member
    Join Date
    Aug 2005
    Posts
    8

    Re: Simple VB emailer

    Everything works! exept... i get this error:
    [SMTP Error 0 Timeout waiting for server response]

    And sometimes i get the [Wrong protecol or connection state for the requested transaction or request] error...

    also, what shall the [ .SMTPhost ] be?
    Last edited by impClaw; Aug 14th, 2005 at 03:59 AM.

  27. #27

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

    Re: Simple VB emailer

    smtp host has to be a vaild smtp server. People usually use their own ISP. Often the ISP will require a valis return address too.

    If you don't have an ISP you could try .SMTPhost = mail.flash.net

  28. #28
    New Member
    Join Date
    Aug 2005
    Posts
    8

    Re: Simple VB emailer

    still getting the same error

    it breaks at the line
    mvarWinsock.SendData "helo " & mvarSMTPhost & vbCrLf

  29. #29

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

    Re: Simple VB emailer

    You need an SMTP server that is going to cooperate. Can't help you with that I just use my ISP.

  30. #30
    New Member
    Join Date
    Aug 2005
    Posts
    8

    Re: Simple VB emailer

    I searched for free SMTP stuff but found nothing

    do you know how do use your ISP, since ive no idea what isp is and how to use my isp:s adress, if it has any..

  31. #31
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Simple VB emailer

    ISP = Internet Service Provider.

    You posted a question on this subject, which was the correct thing to do. However, questions do not belong in the CodeBank, so, I moved your question to where is should be, and where it will receive the most attention, and that is the ClassicVB section.

  32. #32
    New Member
    Join Date
    Aug 2005
    Posts
    8

    Re: Simple VB emailer

    Hmm, i fixed a SMTP server (smtp.gmail.com), still getting the same old error.

    here is the info i got about the SMTP:

    smtp.gmail.com (use authentication)
    Use Authentication: Yes
    Use STARTTLS: Yes (some clients call this SSL)
    Port: 465 or 587
    Last edited by impClaw; Aug 15th, 2005 at 12:42 PM.

  33. #33
    Addicted Member LeonX's Avatar
    Join Date
    Dec 2004
    Location
    Jakarta, Indonesia
    Posts
    172

    Re: Simple VB emailer

    great program .... , but why it cannot be run twice

  34. #34

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

    Re: Simple VB emailer

    I have no problem running it twice, what error are you getting?

  35. #35
    Addicted Member LeonX's Avatar
    Join Date
    Dec 2004
    Location
    Jakarta, Indonesia
    Posts
    172

    Re: Simple VB emailer

    the program not responding.

    try to do this (in vb ide):
    1. run the program once (F5)
    2. send a message
    3. close the log form
    4. close the mail form
    5. run the program again (F5)
    6. send another message
    7. it's not responding

    and i don't know why this happen?
    Regards,
    Ferry

  36. #36

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

    Re: Simple VB emailer

    What is the log form?
    If you have altered the code I'll have to see it.

  37. #37
    Addicted Member LeonX's Avatar
    Join Date
    Dec 2004
    Location
    Jakarta, Indonesia
    Posts
    172

    Re: Simple VB emailer

    no , i did not altered it at all.

    i will explain to you again , i open your project with vb6 , then i run it by pressing F5 , frmmail show up (mail form) , then i click the "Send" command after all configuration needed all sets , then frmLog show up (log form) , after that i click the "Close" command, back to the mail form then i close the form and back to the IDE, and 1 cycle is completed.

    then i do the same things again , but not responding after i click the "Send" command.

    hope you understand what i mean this time.
    Regards,
    Ferry

  38. #38

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

    Re: Simple VB emailer

    what project are you opening that has a frmLog? One of mine?

  39. #39
    Addicted Member LeonX's Avatar
    Join Date
    Dec 2004
    Location
    Jakarta, Indonesia
    Posts
    172

    Re: Simple VB emailer

    sorry , look like i on wrong forum , sorry moeur.
    after i check the code again , i think it is not yours.
    Regards,
    Ferry

  40. #40
    New Member
    Join Date
    Aug 2005
    Posts
    8

    Re: Simple VB emailer

    moeur, could you show how to make SMTP acces if you need login and Password, since i cannot find any SMTP without password request, and my ISP dont provide that...

Page 1 of 2 12 LastLast

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