Making a VB6 application send an email?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Making a VB6 application send an email?

  1. #1
    Join Date
    Feb 2009
    Posts
    4

    Exclamation Making a VB6 application send an email?

    Hi

    I would like to know how to send an email through a VB6 application. I'm using WizBang's Winsock-SMTP Server mailer. So far I've got...

    Code:
    Dim UserName$, UserMail$, MailRecipiant$, MailBody$, SockData$
    
    Private Sub Command1_Click()
    UserName = "JetlagJad"
    UserMail = "Jarryd <jarryd-1995@hotmail.com>"
    MailRecipiant = UserMail
    MailBody = "The message goes here"
    Winsock1.LocalPort = 0
    Winsock1.RemoteHost = "smtp.gmail.com"
    Winsock1.RemotePort = 587
    Winsock1.Connect
    End Sub
    
    Private Sub Winsock1_Connect()
    Label1 = "Sending message..."
    Winsock1.SendData "EHLO " & UserName & vbCrLf
    If Not WaitFor("250") Then GoTo 100
    Winsock1.SendData "MAIL FROM: " & UserMail & vbCrLf
    If Not WaitFor("250") Then GoTo 100
    Winsock1.SendData "RCPT TO: " & MailRecipiant & vbCrLf
    If Not WaitFor("250") Then GoTo 100
    Winsock1.SendData "DATA" & vbCrLf
    If Not WaitFor("354") Then GoTo 100
    Winsock1.SendData MailBody & vbCrLf & "." & vbCrLf
    If Not WaitFor("250") Then GoTo 100
    Winsock1.SendData "QUIT" & vbCrLf
    If Not WaitFor("221") Then GoTo 100
    Label1 = "Message sent"
    GoTo 200
    100
    Label1 = SockData
    200
    Winsock1.Close
    End Sub
    
    Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    Winsock1.GetData SockData
    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)
    Label1 = "Error: " & Description
    SockData = "Error"
    Winsock1.Close
    End Sub
    
    Private Function WaitFor(SockResponse As String) As Boolean
    Do While Left(SockData, 3) <> SockResponse And Left(SockData, 3) <> "220" And Left(SockData, 3) <> "250"
      DoEvents
      If Left(SockData, 3) > "400" Then Exit Function
    Loop
    WaitFor = 1
    SockData = ""
    End Function
    I get the error message:
    530 5.7.0 Must issue a STARTTLS command first. f42sm2188083rvb.3

    Thanks in advance! Oh and by the way.. first post! Woohoo!

  2. #2
    Join Date
    Aug 2004
    Posts
    184

    Cool Re: Making a VB6 application send an email?

    First of all, the first command should be "HELO" not "EHLO"
    secondly the port number for SMTP SERVERS is usually 25, although this may be a secured socket and needs aditional programming, you will have to talk to other ppl about that or try using a different server. I have this same code and it works ok for me.

  3. #3
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,451

    Re: Making a VB6 application send an email?

    Quote Originally Posted by JetlagJad View Post
    Hi

    I get the error message:
    530 5.7.0 Must issue a STARTTLS command first. f42sm2188083rvb.3

    Thanks in advance! Oh and by the way.. first post! Woohoo!
    GMail requires an encrypted connection to send email. I personally don't know of VB6 TLS implementation. You will probably need to use a 3rd party control to send mail through google.

    Now if you are sending EMail to a gmail account, you can use port 25 and the code just like you have posted and it should work, IF you ISP allows you to connect out on port 25.

    ETA: Welcome to the boards
    Last edited by sotoasty; February 14th, 2009 at 08:03 AM. Reason: Add Welcome

  4. #4
    Join Date
    Feb 2009
    Posts
    4

    Re: Making a VB6 application send an email?

    So what SMTP servers do you suggest I could use??

  5. #5
    Join Date
    Dec 2008
    Posts
    19

    Re: Making a VB6 application send an email?

    You don't need a third party control. As long as you have an account on gmail you can use their smtp server. Try port 465 as this is a secured server and you will need to do SSL authentication.

    Also, I strongly recommend using CDO instead of Winsock. Go to this link for full CDO code in the thread and how to set up with gmail.

    http://www.xtremevbtalk.com/showthread.php?t=304277

  6. #6
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,999

    Re: Making a VB6 application send an email?

    type MAILTO: from the command line. if it opens OUTLOOK/Express, you're under the SP2 ruleset. You can NOT send automated email anymore, guys!!!
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,451

    Re: Making a VB6 application send an email?

    Quote Originally Posted by dglienna View Post
    type MAILTO: from the command line. if it opens OUTLOOK/Express, you're under the SP2 ruleset. You can NOT send automated email anymore, guys!!!
    dglienna, I respect you but what are you talking about? MAILTO is basically a shortcut to open up the default mail provider. Here they are talking about sending an e-mail to a mail server. Using the code above does nothing with OE or Outlook. Some ISP's are putting re-directs on port 25 but not all. Mine does. In fact, I had to open a different port on our e-mail server so that I can send email from home, as all connections on port 25 are re-directed to my ISPs mailserver. It is a very effective way of blocking mass mailing programs.

    Jet,
    As Tom stated, CDO might be the best way to go. Or you might check this link out (courtesy of George1111).

    http://www.freevbcode.com/ShowCode.Asp?ID=109

  8. #8
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,999

    Re: Making a VB6 application send an email?

    Won't work if the pc has SP2 or greater
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  9. #9
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,451

    Re: Making a VB6 application send an email?

    Your PC can't block port 25. Well, that's not true. The firewall can block port 25 going out or you AV software can. But all you have to do is add an exception for your application. Personally, I think this is one of the most vile things that the AV/Windows designers do. I don't need them telling me what to do with my Applications.

    However, the above code will work fine on ANY MailServer that does not require TLS. There is/and can be nothing in SP2 to block all applications connecting on port 25. I use similar code in my applications. We have over 100 people using XP sp2 and 3. Some useing Vista SP1. We have a 2nd and 3rd Offices mostly on XP. Everyone of them can and do send e-mail on a daily basis, through my application.

    It will work. All you have to do is add your app in the firewall exceptions, or check you antiVirus.

  10. #10
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: Making a VB6 application send an email?

    I can only agree here.
    I'm using an older SMTP.ocx from ostrosoft and I'm running XP SP3.
    I can send emails no problem with this little control.
    It also worked perfect when SP2 was on.
    I never did no change to any firewall for that matter, and our company is protected by a hardware firewall device.

  11. #11
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,999

    Re: Making a VB6 application send an email?

    My ISP requires all email be sent from my registered account, and must use port 25. (I have to use a different account when I'm on another provider)

    You used to be able to program Outlook to send emails as fast as you could queue them up. Then, there was the delay. Then the OK button clicking program.

    You get the idea. Sure, there are 3rd party tools, but they are not good for sending spam
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center