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

Advertisements

60 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 …

  22. Ajad said

    Can you make the vid for it.

  23. wilsonb said

    Works great. Now, how to attach files?

  24. Enkay said

    do you have to declare password,username,iconf,imsg etc? vb is not able to recognise them. it gives a whole list of errors saying they are not declared. on declaring my username as string, it says that “@ does not match data type dat type string. also should we declare imsg,iconf etc. as objects or something else? adding the reference did not work. Help please

    • codingsense said

      Hi Enkay,

      For username, password, they should be your variables in which you will pass your account details.
      For rest you need to reference the assembly as mentioned in the beginning of the post by navigating to Project -> references and adding Microsoft CDO for windows 2000 Library.

      Thanks

  25. Noel Mari said

    Great! Thanks

  26. edgar said

    nice

  27. akhil said

    Hi

    Naveen Prabhu can you tell me how can create gmail smtp. username password.

  28. akhil said

    And what is simple code to send email using vb6 codes.

    thanks

    • codingsense said

      You replace the username and password, with your gmail credentials which you use to login into your gmail account.

      Thanks,
      Naveen

  29. Wilson said

    The code works great for sending in gmail, but I need it to display a different “From” address – both in the inbox listing and the email itself. Possible?

    I want to send through gmail, but receive replies via a programmable address based on the sender.

    Thank you,

    Wilson

  30. Rahul said

    Sir, you have set the value for iMsg , iConf and Flds ,schema. but please tell me about declaration of imsg,iconf,flds,schema…… pls

  31. vk Jain said

    Great Code!! Thanks alot

  32. vasanth said

    Dear bro whats the reference for this code

  33. Vivek said

    Thanks for your code
    Its working quiet

  34. saurabh tanwer said

    Thank u sir ! your code works fine , but it only sending mail to gmail users. To yahoo user ,or rediff or hotmail it is not working .Can you give me a solution

    • codingsense said

      Hi Saurabh,

      The piece of code should work fine with any of the other mail servers. Just you need to replace the below fields appropriately
      1) smtpserver
      2) smtpserverport
      3) smtpusessl

      Then giving your email id and password of that mail service should work smoothly.

      Thanks,
      Naveen

  35. Hi Codingsense! Please, I am having error with the following code line…
    Flds.Item(schema & “sendusername”) = emmanuelotp@gmail.com

    .To = To
    and beside what is the meaning of the iMsg and how can I call it up. Please reply

    • codingsense said

      Hi Prince,

      Try using ” emmanuelotp@gmail.com” in double quotes. IMsg is a class that will store the email message that you want to send. Its available after you add reference to Microsoft CDO, please see the step in the starting of the post.

      Thanks,
      Naveen

  36. Shyam said

    Dear Mr Naveen,

    Thanks for this wonderful solution! It works perfectly!
    Could it be possible to send an attachment through this? Your reply would be very highly helpful!

    Thanks again!

  37. atul mali said

    can we attach any file to this

  38. minh hang said

    Hi!
    I run your code. Before I send gmail, It run very good. But now, It display error “Runtime error ’48’ with CdoCongig.Fields. Error in loading dll”. Click Debug it display error at line “Set Flds = iConf.Fields”
    Please help me!
    Thanks

  39. Chris Danos said

    Hi,
    Great code, thanks !!!!
    Is there a way to take a feedback whether the mail sent or failed?

    Thanks again!!

  40. Anjali said

    sir, I used your code. But it is giving me error
    The transport error code was 0x80040217. The server response was not available.
    I have removed firewall blockage.
    What is this error, sir ?
    Regards

  41. Radu Sandu said

    Hi,

    Great code!!!
    But I have a problem with sending e-mail.
    If i send first time, I received the following error message: Error 13 – Type mismatch. But when I send another time, the message was sent successfully. What is the problem?

    Thanks!

  42. Abhishek Drolia said

    Thanks bro, u showed the way!

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 )

Google+ photo

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

Connecting to %s

 
%d bloggers like this: