Results 1 to 16 of 16

Thread: Send Mail from VB6 No Controls - using Gmail

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2013
    Posts
    389

    Send Mail from VB6 No Controls - using Gmail

    This code will help you send mail from your VB6 form without Winsock \ inet \ webbrowser1 or any other control, simple and easy.
    This code uses the CDO.Message method*
    I'll be using Gmail on this example but it can actually be used with other mail servers.

    Code:
    Option Explicit
    
    Dim msgA As Object 'Define the CDO
    
    Private Function GmailSend(xUsername, xPassword, xMailTo, xSubject, xMainText)
        Set msgA = CreateObject("CDO.Message") 'set the CDO to reffer as.
        
        msgA.To = xMailTo 'get targeted mail from command
        msgA.Subject = xSubject 'get subject from command
        msgA.HTMLBody = xMainText 'Main Text - You may use HTML tags here, for example <BR> to immitate "VBCRLF" (start new line) etc.
        msgA.From  = "mail@mail.com" 'The from part, make sure its syntax template is a valid mail one, user@domain.com, or something.
    ' HTMLBODY is a STRING, do not try to link a multilined textbox to it without using the ''replace'' function for 'VBCRLf' with '<BR>' (example later)
        
        'Notice, i simplified it, however, you may use more values depending on your needs, such as:
        '.Bcc = "mail@mail.com" ' - BCC..
        '.Cc = "mail@mail.com" ' - CC..
        '
        '.CreateMHTMLBody ("www.mywebsite.com/index.html) 'send an entire webpage from a site
        '.CreateMHTMLBody ("c:\program files\download.htm) 'Send an entire webpage from your PC
        '.AddAttachment ("c:\myfile.zip") 'Send a file from your pc (notice uploading may take a while depending on your connection)
    
        
        'Gmail Username (from which mail will be sent)    
        msgA.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = xUsername
        'Gmail Password
        msgA.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = xPassword
        
        'Mail Server address.
        msgA.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
        
        'To set SMTP over the network = 2
        'To set Local SMTP = 1
        msgA.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        
        'Type of Authenthication
        '0 - None
        '1 - Base 64 encoded (Normal)
        '2 - NTLM
        msgA.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
        
        'Port
        msgA.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
        
        'Send using SSL True\False
        msgA.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
        
        'Update values of the SMTP configuration
        msgA.Configuration.Fields.Update
        
        'Send it.
        msgA.Send
        
        ' On error ... (using on error resume next is over rated, realy.)
        GmailSend = Err.Number
        If Err.Number <> 0 Then MsgBox "Mail Error: " & Err.Description
     
    End Function
    
    Private Sub Command1_Click()
    
    'Usage: GmailSend ("USERNAME","PASSWORD","SendTo@mail.com","Subject","Text Body <br> New line Here"
    'As i was saying, to multiline textbox won't work here. so you'll have to use the Replace function BEFORE sending the mail.
    
    
    TextBody.Text = Replace(TextBody.Text, vbCrLf, "<br>", 1, , vbTextCompare) 'Again, you may use a string, TextBody.text is simply as example
    
    If GmailSend("USERNAME", "PASSWORD", "SendTo@mail.com", "Subject", TextBody.Text) = 0 Then MsgBox "Mail Sent!" 'TextBody.Text is the textbox with the BODY text in it - might as well use any string you want.
    End Sub
    I made it kinda easy to understand, hope it helps.
    Example code sendMail_stum.zip

    'For the leechers, make sure you set the username\password\sendto fields before clicking Command1*

    Last edited by stum; Feb 9th, 2014 at 08:32 AM.

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Send Mail from VB6 No Controls - using Gmail

    Am getting this as an error:

    Mail Error: At least one of the From or Sender fields is required, and neither was found.

    I have a correct email address for both (XXXXX@gmail.com).
    Any suggestions?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2013
    Posts
    389

    Re: Send Mail from VB6 No Controls - using Gmail

    yes, i forgot the from part, please add
    msgA.From = "mail@mail.com"

    i'll fix it on the main post.

  4. #4
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Send Mail from VB6 No Controls - using Gmail

    Thanks! Will test. I currently use Outlook Express on my XP machine to periodically send out birthday notifications to myself, but as OE is becoming a dinosaur, wanted to move to gmail. Appreciate it.

  5. #5
    Hyperactive Member
    Join Date
    Mar 2014
    Posts
    321

    Re: Send Mail from VB6 No Controls - using Gmail

    error , this message could not be sent to the smtp server , the transport errror code was xxxxxxx the server response was not available

  6. #6
    New Member
    Join Date
    Jan 2017
    Posts
    1

    Re: Send Mail from VB6 No Controls - using Gmail

    Quote Originally Posted by stum View Post
    This code will help you send mail from your VB6 form without Winsock \ inet \ webbrowser1 or any other control, simple and easy.
    This code uses the CDO.Message method*
    I'll be using Gmail on this example but it can actually be used with other mail servers.

    Code:
    Option Explicit
    
    Dim msgA As Object 'Define the CDO
    
    Private Function GmailSend(xUsername, xPassword, xMailTo, xSubject, xMainText)
        Set msgA = CreateObject("CDO.Message") 'set the CDO to reffer as.
        
        msgA.To = xMailTo 'get targeted mail from command
        msgA.Subject = xSubject 'get subject from command
        msgA.HTMLBody = xMainText 'Main Text - You may use HTML tags here, for example <BR> to immitate "VBCRLF" (start new line) etc.
        msgA.From  = "mail@mail.com" 'The from part, make sure its syntax template is a valid mail one, user@domain.com, or something.
    ' HTMLBODY is a STRING, do not try to link a multilined textbox to it without using the ''replace'' function for 'VBCRLf' with '<BR>' (example later)
        
        'Notice, i simplified it, however, you may use more values depending on your needs, such as:
        '.Bcc = "mail@mail.com" ' - BCC..
        '.Cc = "mail@mail.com" ' - CC..
        '
        '.CreateMHTMLBody ("www.mywebsite.com/index.html) 'send an entire webpage from a site
        '.CreateMHTMLBody ("c:\program files\download.htm) 'Send an entire webpage from your PC
        '.AddAttachment ("c:\myfile.zip") 'Send a file from your pc (notice uploading may take a while depending on your connection)
    
        
        'Gmail Username (from which mail will be sent)    
        msgA.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = xUsername
        'Gmail Password
        msgA.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = xPassword
        
        'Mail Server address.
        msgA.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
        
        'To set SMTP over the network = 2
        'To set Local SMTP = 1
        msgA.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        
        'Type of Authenthication
        '0 - None
        '1 - Base 64 encoded (Normal)
        '2 - NTLM
        msgA.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
        
        'Port
        msgA.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
        
        'Send using SSL True\False
        msgA.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
        
        'Update values of the SMTP configuration
        msgA.Configuration.Fields.Update
        
        'Send it.
        msgA.Send
        
        ' On error ... (using on error resume next is over rated, realy.)
        GmailSend = Err.Number
        If Err.Number <> 0 Then MsgBox "Mail Error: " & Err.Description
     
    End Function
    
    Private Sub Command1_Click()
    
    'Usage: GmailSend ("USERNAME","PASSWORD","SendTo@mail.com","Subject","Text Body <br> New line Here"
    'As i was saying, to multiline textbox won't work here. so you'll have to use the Replace function BEFORE sending the mail.
    
    
    TextBody.Text = Replace(TextBody.Text, vbCrLf, "<br>", 1, , vbTextCompare) 'Again, you may use a string, TextBody.text is simply as example
    
    If GmailSend("USERNAME", "PASSWORD", "SendTo@mail.com", "Subject", TextBody.Text) = 0 Then MsgBox "Mail Sent!" 'TextBody.Text is the textbox with the BODY text in it - might as well use any string you want.
    End Sub
    I made it kinda easy to understand, hope it helps.
    Example code sendMail_stum.zip

    'For the leechers, make sure you set the username\password\sendto fields before clicking Command1*

    I tested this code in vb6 it works wonderfull
    only if I am basing my project conected to sql 2014
    if I am conecthing my project to a access 2003 database
    then it stops by msg.Send it makes an error masaage
    run-time error -2147220975 the massage could not be sent to the SMTP server the transport error dose was 0x80040217. the server response was not availble

  7. #7
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,487

    Re: Send Mail from VB6 No Controls - using Gmail

    Quote Originally Posted by stum View Post
    yes, i forgot the from part, please add
    msgA.From = "mail@mail.com"

    i'll fix it on the main post.
    I don't think that has been done yet

    Could someone add it, and also incorporate a full example with valid send examples (can be made up, but demonstrate exactly what should be typed), and attach the project

    Thanks,
    Rob

  8. #8
    New Member
    Join Date
    Jun 2022
    Posts
    3

    Re: Send Mail from VB6 No Controls - using Gmail

    Quote Originally Posted by Moshe Singer View Post
    I tested this code in vb6 it works wonderfull
    only if I am basing my project conected to sql 2014
    if I am conecthing my project to a access 2003 database
    then it stops by msg.Send it makes an error masaage
    run-time error -2147220975 the massage could not be sent to the SMTP server the transport error dose was 0x80040217. the server response was not availble
    This code CDo have no relation with sql server or outlouk, it's indépendant.
    My problem is that code work fine in my laptop windows10 but when I launch my app in win7 doesnwt work because of transport error !
    Any solutions ? Please

  9. #9
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Send Mail from VB6 No Controls - using Gmail

    This is probably configuration problem with TLS version being supported by Schannel. Try to configure Win7 to support TLS 1.2 by default.

    Note that you have to have Service Pack 1 for Win7 installed to get TLS 1.2 in Schannel.

    If still does not work then probably your SMTP server requires even newer TLS version 1.3. (Doubt it very much but still a possible insanity.)

    If this is the case you might want to try the STARTTLS proxy with CDO from this post as well which supports TLS 1.3 on Win7 (and even down to NT 4.0).

    cheers,
    </wqw>

  10. #10
    Addicted Member
    Join Date
    Feb 2004
    Posts
    145

    Re: Send Mail from VB6 No Controls - using Gmail

    Quote Originally Posted by wqweto View Post
    This is probably configuration problem with TLS version being supported by Schannel. Try to configure Win7 to support TLS 1.2 by default. If still does not work then probably your SMTP server requires even newer TLS version</wqw>
    I received this notification from GMX.com mail server...

    "We are contacting you because one or more of your devices or programs uses the TLS 1.0 and/or TLS 1.1 encryption protocols to connect to our GMX email servers. This involves either access via SMTP (sending emails) or retrieval via POP3/IMAP (receiving emails).

    Unfortunately, the TLS 1.0 and TLS 1.1 encryption protocols are outdated and no longer meet current security standards. To protect your email communications, soon we will no longer allow access to our GMX servers with these outdated protocols.

    We recommend that you only use newer devices and programs that support the latest TLS 1.2 or 1.3 encryption protocols and transfer your emails securely."

    Question: does CDO.message use all TLS versions in current Windows (10)? Or must we change setup of our VB6 code in order to ensure TLS 1.2 or 1.3 is used?

    I know that if I run the exe from my WinXP machine, it will try to use TLS 1.0, but will it always work on current (Win10) machines?
    /Jimboat

  11. #11
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Send Mail from VB6 No Controls - using Gmail

    CDO and IE use Schannel library for TLS and you can configure default Schannel protocols/ciphers through registry so an app using default settings can be forced to use or not to use TLS 1.2 for instance.

    IE/CDO on Win7 with latest patches will (by default) use TLS 1.2 just fine so you *must* be ok as long as TLS 1.2 is supported by respective web/mail servers i.e. until TLS 1.3 starts being required.

    For OS baked TLS 1.3 support you will need to go up to 11 i.e. Win11 -- ouch! -- because no Win8, Win8.1 nor Win10 support TLS 1.3 in their stock Schannel implementations.

    cheers,
    </wqw>

  12. #12
    Addicted Member
    Join Date
    Feb 2004
    Posts
    145

    Re: Send Mail from VB6 No Controls - using Gmail

    Quote Originally Posted by wqweto View Post
    CDO and IE use Schannel library for TLS and you can configure default Schannel protocols/ciphers through registry so an app using default settings can be forced to use or not to use TLS 1.2 for instance.

    IE/CDO on Win7 with latest patches will (by default) use TLS 1.2 just fine so you *must* be ok as long as TLS 1.2 is supported by respective web/mail servers i.e. until TLS 1.3 starts being required.

    For OS baked TLS 1.3 support you will need to go up to 11 i.e. Win11 -- ouch! -- because no Win8, Win8.1 nor Win10 support TLS 1.3 in their stock Schannel implementations.cheers,</wqw>
    wqweto - sounds good (for now!) many thanks.
    /Jimboat

  13. #13
    New Member
    Join Date
    Jun 2022
    Posts
    3

    Re: Send Mail from VB6 No Controls - using Gmail

    Quote Originally Posted by wqweto View Post
    CDO and IE use Schannel library for TLS and you can configure default Schannel protocols/ciphers through registry so an app using default settings can be forced to use or not to use TLS 1.2 for instance.

    IE/CDO on Win7 with latest patches will (by default) use TLS 1.2 just fine so you *must* be ok as long as TLS 1.2 is supported by respective web/mail servers i.e. until TLS 1.3 starts being required.

    For OS baked TLS 1.3 support you will need to go up to 11 i.e. Win11 -- ouch! -- because no Win8, Win8.1 nor Win10 support TLS 1.3 in their stock Schannel implementations.

    cheers,
    </wqw>
    I don't think that tls 1.2 where the problem exist, because i have already installed a registry .reg that make default tls to tls 1.2 and it's the same in pc that my app work fine. Please any help.
    Ps : my app work only on PC's where i develop my code (2 pc) but on the other pc no ! Is it a problem with dlls vb6 ?

  14. #14
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Send Mail from VB6 No Controls - using Gmail

    Quote Originally Posted by Lahmid View Post
    I don't think that tls 1.2 where the problem exist, because i have already installed a registry .reg that make default tls to tls 1.2 and it's the same in pc that my app work fine.
    On the machine you have troubles start Internet Explorer (not Edge) and navigate to https://www.howsmyssl.com/ and notice info under Version section.

    It says TLS 1.2 here but I have Service Pack 1 for Win7 installed on my VM.

    Quote Originally Posted by Lahmid View Post
    Is it a problem with dlls vb6 ?
    No, provided that by "dlls vb6" you don't mean CDO dlls (which are not part of VB6)

    cheers,
    </wqw>

  15. #15
    New Member
    Join Date
    Jun 2022
    Posts
    3

    Re: Send Mail from VB6 No Controls - using Gmail

    Quote Originally Posted by wqweto View Post
    On the machine you have troubles start Internet Explorer (not Edge) and navigate to https://www.howsmyssl.com/ and notice info under Version section.

    It says TLS 1.2 here but I have Service Pack 1 for Win7 installed on my VM.



    No, provided that by "dlls vb6" you don't mean CDO dlls (which are not part of VB6)

    cheers,
    </wqw>
    Thank you for reply, i reinstalled pack KB of tls1.2, my tls1.2 now is installed but always the problem exist (error on sending mail)

  16. #16
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Send Mail from VB6 No Controls - using Gmail

    Quote Originally Posted by Lahmid View Post
    Thank you for reply, i reinstalled pack KB of tls1.2, my tls1.2 now is installed but always the problem exist (error on sending mail)
    I can only advise you to start a new thread outside of CodeBank forum with a simplified version your failing send mail under Win7.

    Post a full working code which has trouble with CDO under Win7 in Visual Basic 6 and Earlier forum which can be copy/posted in a form and investigated under Win7.

    The idea is to help anyone willing to help you to have a compiling code which is failing under Win7, so that anyone willing to help is not preparing sample with CDO which might be very different than your actual code.

    In the simplified working code you post in a new thread in Visual Basic 6 and Earlier forum make sure to include the exact SMTP server which is failing if it's a public one.

    You can mask/skip your username/password settings but the exact SMTP server that is not working under Win7 is very imported to be included in the code you post.

    cheers,
    </wqw>

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