Naveen's Weblog

Bridge to future

Sending Email through Gmail using VB 6.0

Posted by codingsense on July 17, 2009


Hi,

Here is a sample code that is used to send emails from any SMPT server using CDO in Visual Basic 6.0.
Here first i have added a reference to the project by navigating to Project -> references and adding Microsoft CDO for windows 2000 Library.

Sub SendEmail()

Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
Set Flds = iConf.Fields

' send one copy with Google SMTP server (with autentication)
schema = "http://schemas.microsoft.com/cdo/configuration/"
Flds.Item(schema & "sendusing") = 2
Flds.Item(schema & "smtpserver") = "smtp.gmail.com"
Flds.Item(schema & "smtpserverport") = 465
Flds.Item(schema & "smtpauthenticate") = 1
Flds.Item(schema & "sendusername") =  Username@gmail.com
Flds.Item(schema & "sendpassword") = password
Flds.Item(schema & "smtpusessl") = 1
Flds.Update

With iMsg
.To = To
.From = username@gmail.com
.Subject = "Mail from gmail"
.HTMLBody = "Test message using CDO in vb6 from Gmail smtp"
Set .Configuration = iConf
.Send
End With

Set iMsg = Nothing
Set iConf = Nothing
Set Flds = Nothing

End Sub

If you need to send mail from .Net here is the procedure.

Thanks,
Naveen Prabhu

34 Responses to “Sending Email through Gmail using VB 6.0”

  1. Filip said

    Hi

    Thanks for the code – worked perfectly

  2. Niraj Sinha said

    Hi,
    while sending it is giving an error message: run-time error ‘-2147220973(80040213)’:The transport failed to connect to the server.

    • codingsense said

      Hi,

      Check if you have given proper email id and password.

      Thanks,
      Naveen Prabhu

    • Peter said

      Installing Internet information server fixed this for me. It doesn’t use the local server, but maybe one of the required services needs to be running and isn’t.

  3. jayant said

    can we send a mail with out using the authentication.
    i mean to say with out user ID and Password

    • codingsense said

      Hi,

      No you cannot access gmail SMTP server without authentication. Without logging inside the server you cannot send the mail.

      Thanks,
      Naveen

  4. Kyle said

    thank you so much, you have made a big difference, I have seen alot of code out there, but non of them worked like yours.

    keep doing the great job!

    Thanks. Kyle

    • codingsense said

      Hi Kyle,

      Thanks for the comments. Your support will inspire me to help the community more.

      Thanks,
      Naveen Prabhu

  5. vinayak said

    Hi,
    while sending it is giving an error message: run-time error ‘-2147220973(80040213)’:The transport failed to connect to the server.

    Even though , i provided proper email id and password,
    It giving this error msg

  6. Rahul Dhoble said

    Thank You for code
    Bcz i am wondering about this , How mail to be send without using outlook i have search lots of site for that
    But no satisfaction.
    My search ends here with your code.

    Thanks for such a uploadation.

  7. mac said

    thanks man, very good code – simple and work perfect

  8. Chris said

    Thanks! Your code worked like magic. And it’s simple and easily understood. :-)

  9. vinayak said

    Hello sir, iam using one private Smtp address instead of gmail(smtp.gmail.com) one,
    but it is not giving any result… and
    it also not giving any kind of errors.

    i had made changes in place of
    …> smtpserver
    …> sendusername
    …> sendpassword

    plz help me..

    • codingsense said

      Hi Vinayak,

      You should also see what port no your SMTP uses, if there is not any port no, then remove smtpserverport line. And check if it works on SSL, or make smtpusessl = 0 or remove it.

      After making these changes please check it, if it still wont work, then give me the message that is displayed after the process fails. I will try my best to help you out.

      Thanks,
      Codingsense

  10. Vinayak said

    Hi sir i had made changes ,
    as ur specified in following manner

    Flds.Item(schema & “sendusing”) = 2
    Flds.Item(schema & “smtpserver”) = ‘private smtp addres
    Flds.Item(schema & “smtpauthenticate”) = 1
    Flds.Item(schema & “sendusername”) = Username
    Flds.Item(schema & “sendpassword”) = password
    Flds.Item(schema & “smtpusessl”) = 0

    but it is giving an errors
    1)The transport failed to connect to the server.
    when using smtpusessl = 1

    2)The server rejected the sender address. The server response was: 501 5.1.7 Bad sender address syntax
    when using smtpusessl = 0

    • codingsense said

      Hi Vinayak,

      Please ask the SSL and port no details to your system administrator, he might help you with that.

      And the error that you specified “The transport failed to connect to the server” was even visible to me in a specific machine, with same configuration try with some other machine and check if the connection is successful.

      Thanks,
      Naveen Prabhu

  11. FeRZ said

    Damn, thanks a lot man.. I was pissin ma pants, searchin for this.. Luv ya man, thanks again!!!

  12. shailesh said

    hi,
    your code is working fine.but Email is not goes to inbox it goes to trash or Junk . why like that ?

    • codingsense said

      Hi Shailesh,

      Its not because of the program, the program only sends mails to the given email id. The server to which are sending it might be treating it as spam. Check the contents of the mail that you are sending or check the policies of the server to which you are sending the mail.

      Thanks,
      Codingsense.

  13. prasad said

    thanx. it worked perfectly. How do we ATTACH files to the mail ?

  14. No , i created a new email id for test project and copy the code ( reference added ) i got errors @ line no’s
    Flds.Item(schema & “sendusername”) = Username@gmail.com
    .To = To
    .From = username@gmail.com
    I replaced them with double quotes then errors removed …… But run time error “—-The transport failed to connect to the server ———- ” this is partial message

    Please replay via gmail address…………. or comment on my blog…………….

  15. Jeniffer said

    My pc OS is xp. There is IIS installed and have smtp service running. I created a gmail account. In the vb 6 code I provided gmail address (and tried the gmail account name) and password. While sending it is giving an error message: “run-time error ‘-2147220973(80040213)’:The transport failed to connect to the server.”
    Is there any setting I miss?

    How to make it work?

    Thanks.

  16. anand said

    Can i know how to attach a attachment in this mail program ,i want to send a 4 to 5 attachment .
    Thanks in advance
    if u send a mail on anandtri@gmail.com for quick response .

  17. JASIR KT said

    How to cancel sending while sending is in process…

  18. radheyshyam sharma said

    thanks Naveen Prabhu sir

    thanks you very much it works exactly as i wish… agains thanks with regards – radheys@yahoo.com

  19. Arvindkumar Naicker said

    Thank you Mr. Naveen.

  20. dalugoda said

    its greate….. thank you very much….

  21. Manoj Patel said

    Thank you Sir
    This is Very Good …
    But how I Forward same Emails to contact of that person to whom i send that Email …

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.