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
Filip said
Hi
Thanks for the code – worked perfectly
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.
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
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
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
codingsense said
Hi,
Can you check in some other system? I had seen that problem in only one system but on others it worked fine.
I will try to replicate it once again for you. Can you give me more details of your system?
Thanks,
Codingsense
vinayak said
yes this worked in other system….
thanks for the code……..
——-
but what is the problem in previous machine
should IIS is compulsory for dis code ???
codingsense said
Hi,
You are most welcome. Not really it does not relate with IIS.
Thanks,
Codingsense
vinayak said
can i attach any files and send to mail through this code ????
plzzzz help!!!!
codingsense said
Hi,
For sending mail with an attachment here is a link try it.
http://www.vbforums.com/showthread.php?t=337578
or
http://www.freevbcode.com/ShowCode.Asp?ID=109
Thanks,
Codingsense
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.
mac said
thanks man, very good code – simple and work perfect
Chris said
Thanks! Your code worked like magic. And it’s simple and easily understood.
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
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
FeRZ said
Damn, thanks a lot man.. I was pissin ma pants, searchin for this.. Luv ya man, thanks again!!!
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.
prasad said
thanx. it worked perfectly. How do we ATTACH files to the mail ?
Genuineapps said
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…………….
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.
aneeta said
Email address present in to should be hidden from the receiver
How should I do that using smtp in VB6.0
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 .
JASIR KT said
How to cancel sending while sending is in process…
radheyshyam sharma said
thanks Naveen Prabhu sir
thanks you very much it works exactly as i wish… agains thanks with regards – radheys@yahoo.com
Arvindkumar Naicker said
Thank you Mr. Naveen.
dalugoda said
its greate….. thank you very much….
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 …