Results 1 to 5 of 5

Thread: also on the winsock smtp emailer

  1. #1

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604

    also on the winsock smtp emailer

    another problem is that using winsock to send the email,
    it only works if I pick an email address that is on my domain.

    when I pick an email address like [email protected]
    it throws out error 550, saying that it doesn't recognize that user.

    Any ideas?
    Wen Gang, Programmer
    VB6, QB, HTML, ASP, VBScript, Visual C++, Java

  2. #2
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: also on the winsock smtp emailer

    Quote Originally Posted by wengang View Post
    another problem is that using winsock to send the email,
    it only works if I pick an email address that is on my domain.

    when I pick an email address like [email protected]
    it throws out error 550, saying that it doesn't recognize that user.

    Any ideas?
    Are you using Winsock for a client or server? Then it depends on how you are setting up ypur application and what SMTP commands you're using.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  3. #3

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604

    Re: also on the winsock smtp emailer

    Honestly, I haven't tried to email programattically before.
    I find it a bit confusing.

    I have a website, say ggg.com and an email there, say [email protected]
    I have a code that I can use to send messages from [email protected] to [email protected], but when I get the email in Outlook, the body of the message is not there, only the subject in the subject line.
    Also, I can't send an email to anybody I want, only to myself.
    The code is stuff I've pieced together from all the samples I'm finding here (pieced together because I can't seem to get a single piece of code to work without altering it).

    I'll post the code below:

    vb Code:
    1. Option Explicit
    2.  
    3. Private WithEvents SMTP As clsSMTP
    4.  
    5. Private Sub Command1_Click()
    6. Dim Winsock1 As New Winsock
    7.  
    8. With SMTP
    9.  'who is the email from?
    10.  .Sender = "[email protected]"
    11.  'who is it going to?
    12.  .Recipient = "[email protected]"
    13.  'your SMTP server
    14.  .SMTPhost = "mail.ggg.com"
    15.  'optional subject line
    16.  .Subject = "SMTP test"
    17.  'make connection to the server
    18.  
    19.  .OpenMail Winsock1
    20.  'did connection succeed?
    21.  If Not .Connected Then Exit Sub
    22.  'you can send as many lines as you want here
    23. .Send "This is a test of your wonderful SMTP client!"
    24.  'make sure you close or nothing will get sent
    25.  .CloseMail
    26. End With
    27. MsgBox ("Success")
    28.  
    29. End Sub
    30.  
    31.  
    32.  
    33. Private Sub Form_Load()
    34.     Set SMTP = New clsSMTP
    35. End Sub

    SMTP is a class and it's code is below:
    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. Private WithEvents mvarWinsock As Winsock
    12.  
    13. Const OK = 250
    14. Const Connection = 220
    15. Const ReadyforData = 354
    16. Const UserUnknown = 550
    17.  
    18. Dim Reply As Integer
    19. Dim Message As String
    20. Dim MyName As String
    21. Dim ErrMSg As String
    22.  
    23. Event Error(Code As Integer, Message As String)
    24. Event ServerResponse(Reply As Integer, Message As String)
    25.  
    26. Private Function WaitFor(R As Integer) As Boolean
    27. Dim A
    28.     A = Timer + 30
    29.     While Not Reply = R And A > Timer
    30.     DoEvents
    31.     Wend
    32.     If A < Timer Then
    33.         WaitFor = True
    34.     Else
    35.         WaitFor = False
    36.     End If
    37.     Reply = 0
    38. End Function
    39.  
    40. Public Property Let Subject(ByVal vData As String)
    41.     mvarSubject = vData
    42. End Property
    43.  
    44. Public Property Get Subject() As String
    45.     Subject = mvarSubject
    46. End Property
    47.  
    48. Public Property Let SMTPhost(ByVal vData As String)
    49.     mvarSMTPhost = vData
    50. End Property
    51.  
    52. Public Property Get SMTPhost() As String
    53.     SMTPhost = mvarSMTPhost
    54. End Property
    55.  
    56. Public Sub Send(Message As String)
    57.  mvarWinsock.SendData Message & vbCrLf
    58. End Sub
    59.  
    60. Public Property Let Sender(ByVal vData As String)
    61.     mvarSender = vData
    62. End Property
    63.  
    64. Public Property Get Sender() As String
    65.     Sender = mvarSender
    66. End Property
    67.  
    68. Public Property Let Recipient(ByVal vData As String)
    69.     mvarRecipient = vData
    70. End Property
    71.  
    72. Public Property Get Recipient() As String
    73.     Recipient = mvarRecipient
    74. End Property
    75.  
    76. Public Property Get Connected() As Boolean
    77.     Connected = mvarConnected
    78. End Property
    79.  
    80. Public Sub OpenMail(myWinsock As Winsock)
    81.    
    82.     Set mvarWinsock = myWinsock
    83.    
    84.     Reply = 0
    85.     'connect to SMTP server
    86.     mvarWinsock.Connect mvarSMTPhost, 25
    87.     If WaitFor(Connection) Then
    88.         GiveError
    89.         Exit Sub
    90.     End If
    91.     'send hello and wait for OK
    92.     mvarWinsock.SendData "helo " & mvarSMTPhost & vbCrLf
    93.     If WaitFor(OK) Then
    94.         GiveError
    95.         mvarWinsock.Close
    96.         Exit Sub
    97.     End If
    98.     'send sender's name
    99.      If InStr(mvarSender, "@") = 0 Then
    100.         mvarWinsock.SendData "Mail From:<" & mvarSender & "@" & MyName & ">" & vbCrLf
    101.     Else
    102.         mvarWinsock.SendData "Mail From:<" & mvarSender & ">" & vbCrLf
    103.     End If
    104.     If WaitFor(OK) Then
    105.         GiveError
    106.         mvarWinsock.Close
    107.         Exit Sub
    108.     End If
    109.     'send recipient
    110.     mvarWinsock.SendData "RCPT TO:<" & mvarRecipient & ">" & vbCrLf
    111.     If WaitFor(OK) Then
    112.         GiveError
    113.         mvarWinsock.Close
    114.         Exit Sub
    115.     End If
    116.     'make ready for data
    117.     mvarWinsock.SendData "Data" & vbCrLf
    118.     If WaitFor(ReadyforData) Then
    119.         GiveError
    120.         mvarWinsock.Close
    121.         Exit Sub
    122.     End If
    123.     'Send Date:
    124.     mvarWinsock.SendData "Date: " & Format(Date, " dd mmm yy ") & Time & vbCrLf
    125.     'To:
    126.     mvarWinsock.SendData "TO:<" & mvarRecipient & ">" & vbCrLf
    127.     'From:
    128.     mvarWinsock.SendData "From: <" & mvarSender & ">" & vbCrLf
    129.     'Subject:
    130.     If Not mvarSubject = "" Then mvarWinsock.SendData "Subject:" & mvarSubject & vbCrLf
    131.     'Body
    132.     mvarWinsock.SendData "Body: THis is a test." & vbCrLf
    133.     mvarWinsock.SendData "This is a a test email." & vbCrLf
    134.     mvarConnected = True
    135. End Sub
    136.  
    137. Private Function WhoAmI() As String
    138. Dim X As Integer, Y As Integer, Z As Integer
    139.  
    140.     X = InStr(Message, "Hello")
    141.     Y = InStr(Message, "[")
    142.     Z = InStr(Message, "]")
    143.    
    144.     'MsgBox (X & " " & Y)
    145.    
    146.     If X = 0 Or Y = 0 Or Z = 0 Then
    147.         WhoAmI = mvarWinsock.LocalIP
    148.     Else
    149.         X = X + 7
    150.         'Y = Y - 1 - X
    151.         Z = Z - X
    152.         WhoAmI = Mid$(Message, X, Z)
    153.         'WhoAmI = Mid$(Message, X, Y)
    154.     End If
    155. End Function
    156.  
    157. Public Sub CloseMail()
    158.     mvarWinsock.SendData "." & vbCrLf
    159.     If WaitFor(OK) Then
    160.         GiveError
    161.         Exit Sub
    162.     End If
    163.     mvarWinsock.SendData "QUIT"
    164.     mvarWinsock.Close
    165. End Sub
    166.  
    167. Private Sub GiveError()
    168.  Dim Message As String
    169.  Select Case Reply
    170.  Case Is = 10061
    171.   Message = "Can't Connect to " & mvarSMTPhost
    172.  Case Is = 11001
    173.   Message = mvarSMTPhost & " is not a valid name or address."
    174.  Case Is = 550
    175.   Message = "User " & mvarRecipient & " not known to server."
    176.  Case Is = 0
    177.   Message = "Timeout waiting for server response"
    178.  Case Is = 500
    179.   Message = "Server does not recognize a command sent."
    180.  Case Is = 553
    181.   Message = "Invalid recipient name"
    182.  Case Is = 1
    183.   Message = "You must set Host, Recipient and Sender before connecting"
    184.  Case Else
    185.   Message = "Mail tool error."
    186.  End Select
    187.  
    188.  RaiseEvent Error(Reply, Message)
    189. End Sub
    190.  
    191. Private Sub Class_Initialize()
    192.     'Set mvarWinsock = New Winsock
    193. End Sub
    194.  
    195. Private Sub mvarWinsock_DataArrival(ByVal bytesTotal As Long)
    196. Dim X As String
    197.     mvarWinsock.GetData X
    198.     Reply = Val(X)
    199.     X = Trim$(Mid(X, 4))
    200.     RaiseEvent ServerResponse(Reply, X)
    201.     Message = X
    202. End Sub
    203.  
    204. Private Sub mvarWinsock_Error(ByVal Number As Integer, Description As String, _
    205. ByVal Scode As Long, ByVal Source As String, _
    206. ByVal HelpFile As String, ByVal HelpContext As Long, _
    207. CancelDisplay As Boolean)
    208.     Reply = Number
    209. End Sub

    I've tried to include the email body both in the sub and in the class itself, but it just doesn't show up.
    Then of course the more serious issue of only being able to email myself so far.
    Wen Gang, Programmer
    VB6, QB, HTML, ASP, VBScript, Visual C++, Java

  4. #4
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: also on the winsock smtp emailer

    Try this little SMTP application and see what it does for you. If the mail recipient requires Authentication then this will not do you any good as you will need to know what it is and how to use it.
    Attached Files Attached Files
    Last edited by jmsrickland; Jun 11th, 2011 at 10:55 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  5. #5
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: also on the winsock smtp emailer

    I suspect the problem of no body being received is to do with this and the version of Outlook you're using:
    Code:
       mvarWinsock.SendData "Body: THis is a test." & vbCrLf
    Try
    Code:
       mvarWinsock.SendData "THis is a test." & vbCrLf
    or even this.
    Code:
       mvarWinsock.SendData "Body:" & vbCrLf
       mvarWinsock.SendData " THis is a test." & vbCrLf
    I assume that "Body:" is something that Outlook can expect as I don't see it defined anywhere in the SMTP Protocol.

    BTW: Why have you modified the Class code ? It works perfectly 'as is'

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