Solved

Sending CDO SMTP Mail via gmail

Posted on 2013-01-22
20
2,044 Views
Last Modified: 2014-11-12
I need to convert my email send capabilities of my web site to use my gmail account.  I could use some help modifying this code to work.  Thanks!!

			Set cdoConfig = CreateObject("CDO.Configuration")
			With cdoConfig.Fields
				.Item(cdoSendUsingMethod) = cdoSendUsingPort
				.Item(cdoSMTPServer) = "old.server"
				.Item(cdoSMTPAuthenticate) = 1
				.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
				.Item(cdoSendUsername) = "your_mom"
				.Item(cdoSendPassword) = "pwd"
				.Update
			End With

			Set cdoMessage = CreateObject("CDO.Message")
			With cdoMessage
				Set .Configuration = cdoConfig
				.To = sEvntDirEmail
				.From = sEmail
				.Subject = sEventName & " Contact: " & sSubject
				.TextBody = sMsg
				.Send
			End With
			Set cdoMessage = Nothing

Open in new window

0
Comment
Question by:Bob Schneider
  • 8
  • 7
  • 5
20 Comments
 
LVL 30

Assisted Solution

by:Wayne Barron
Wayne Barron earned 286 total points
ID: 38808233
This is what I use.

Pay attention to the top part, it is a secure socket layer connection.

Set myMail = CreateObject("CDO.Message") 
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'Send the message using the network (SMTP over the network).
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="smtp.gmail.com"
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true 'Use SSL for the connection (True or False)
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
     
' If your server requires outgoing authentication uncomment the lines bleow and use a valid email address and password.
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'basic (clear-text) authentication
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") ="webmaster@domain.com"
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") ="**********"

myMail.Configuration.Fields.Update

Open in new window


Carrzkiss
0
 
LVL 52

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 214 total points
ID: 38808234
Your outgoing server is smtp.gmail.com

Just use this for one off's.  If you send bulk through gmail your account will probably get closed.  

This is a great resource  http://www.paulsadowski.com/wsh/cdo.htm  Look for the code that uses, "Sending a text email using authentication against a remote SMTP server."

Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory. 
Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network). 

Const cdoAnonymous = 0 'Do not authenticate
Const cdoBasic = 1 'basic (clear-text) authentication
Const cdoNTLM = 2 'NTLM

Set objMessage = CreateObject("CDO.Message") 
objMessage.Subject = "Example CDO Message" 
objMessage.From = """Me"" <me@my.com>" 
objMessage.To = "test@paulsadowski.com" 
objMessage.TextBody = "This is some sample message text.." & vbCRLF & "It was sent using SMTP authentication."

'==This section provides the configuration information for the remote SMTP server.

objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 

'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.your.com"

'Type of authentication, NONE, Basic (Base64 encoded), NTLM
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic

'Your UserID on the SMTP server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = "youruserid"

'Your password on the SMTP server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "yourpassword"

'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 

'Use SSL for the connection (False or True)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False

'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60

objMessage.Configuration.Fields.Update

'==End remote SMTP server configuration section==

objMessage.Send

Open in new window

0
 

Author Comment

by:Bob Schneider
ID: 38808273
Ok padas, I tried that but got a "Name Redefined" error on this line:

Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory.

Except I know it is not elsewhere in the page.  Could it be from this portion of my global.asa page?

<!-- 
METADATA 
TYPE="typelib"
UUID="CD000000-8B95-11D1-82DB-00C04FB1625D"
NAME="CDO for Windows 2000 Library"
-->

Here is my entire code:
            Const cdoSendUsingPickup = 1 
            Const cdoSendUsingPort = 2
            Const cdoAnonymous = 0 
            Const cdoBasic = 1
            Const cdoNTLM = 2

            Set objMessage = CreateObject("CDO.Message") 
            objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
            objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
            objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
            objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "your_mom"
            objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "pwd"
            objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
            objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
            objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
            objMessage.Configuration.Fields.Update
            objMessage.Subject = sSubject 
            objMessage.From = sEmail 
            objMessage.To = "bobs@h51software.net" 
            objMessage.TextBody = sMsg
            objMessage.Send

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 30

Assisted Solution

by:Wayne Barron
Wayne Barron earned 286 total points
ID: 38808309
Give this a shot, this is your code, edited to take on the Gmail stuff, just like what I posted.

With cdoConfig.Fields
				.Item(cdoSendUsingMethod) = cdoSendUsingPort
				.Item(cdoSMTPServer) = "smtp.gmail.com"
				.Item(cdoSMTPAuthenticate) = 1
				.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
				.Item(cdoSendUsername) = "your_mom"
				.Item(cdoSendPassword) = "pwd"
				.Update
			End With

Open in new window

0
 
LVL 52

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 214 total points
ID: 38808321
It looks that way.  I have always used that example.  Just in case other pages are using that, I would just eliminate the portion of the new code that errors.  Here is what 1and1 has.http://faq.1and1.com/scripting_languages_supported/asp_active_server_pages/6.html
0
 
LVL 30

Assisted Solution

by:Wayne Barron
Wayne Barron earned 286 total points
ID: 38808330
I will repeat this one more time.
The code that I posted, picks up on the SSL Port, that GMail runs from.
GMail does not use port 25.

Please look at my code example again, and give it a shot.
0
 

Author Comment

by:Bob Schneider
ID: 38808349
carrzkiss, I will look at it in the morning....heading to bed...your persistence is much appreciated!!!
0
 
LVL 52

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 214 total points
ID: 38808355
Carzkiss is right about the port and ssl.  However, the initial problem was the name redefined thing.

When you get this working, I like to put this into a function on a separate page and bring it in as an include file to use everywhere.
<%
theHTML="some html"
theHTML=theHTML&"some  more  html"
'call function to send mail and wrap in template
SendMail (mybuddy@someplace.com ,me@mydomain.com ,"some subject" ,theHTML)
%>
<%
Function SendMail(To,From,Subject,body)

' cdo code above
objMessage.htmlBody = template_1(body) 'wrap template around our content

End function
%>
Even fancier... make a template as an include file

<%
' this is also on a separate asp page as an include file so you can change the template quickly and call it from the code above.
 
function template_1(content)
html=""
html=html&"<table>"
html=html&"<tr><td><img src=""http://fullurl/to_header_image.jpg""></td><tr>"
html=html&"<tr><td>"&content&"</td><tr>"
html=html&"<tr><td>footer stuff.  address city st zip</td><tr>"
html=html&"</table>"
end function
%>
0
 

Author Comment

by:Bob Schneider
ID: 38809352
carzkiss, I got a "Transport can't connect to the server..." error.  I even reset my password to ensure that I had that correct.  Any ideas?
0
 
LVL 30

Accepted Solution

by:
Wayne Barron earned 286 total points
ID: 38809385
smtpusessl") = true

With cdoConfig.Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServer) = "smtp.gmail.com"
.Item(cdoSMTPAuthenticate) = 1
.Item(http://schemas.microsoft.com/cdo/configuration/smtpusessl)=true ' If use SSL set to True, if not, set to False
.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
.Item(cdoSendUsername) = "your_mom"
.Item(cdoSendPassword) = "pwd"
.Update
End With

Open in new window


You need the smtpusessl. (5th line above)
http://kb.cffcs.com/Main.asp?irid=199&Type=Article

Carrzkiss
0
 

Author Comment

by:Bob Schneider
ID: 38809410
Perfect!  Thanks!!  I will now spend the time to change all of my web code to reflect this.  padas, thanks for the include idea.  Here is my last question: I use includes for my headers, menus, etc.  How can I stick an include inside a block of asp script????
0
 

Author Closing Comment

by:Bob Schneider
ID: 38809421
Excellent!  Thanks!!
0
 
LVL 30

Expert Comment

by:Wayne Barron
ID: 38809427
<%if something="else" then%>
<!--#include file="get.asp"-->
<%else%>
<!--#include file="it.asp"-->
<%end if%>

You cannot put it like this.

<%if something="else" then
<!--#include file="get.asp"-->
%>

So, do the first one set above, and you will be good to go.
(this is the way I do mine, I have as many as 100+ files per project, and everything is included, depending on Queries from the database (or) URL)

Have a good one.
Carrzkiss
0
 

Author Comment

by:Bob Schneider
ID: 38809439
So if I have:
<%
lots
and
lots
of
lines
of
code
%>

I can break it anywhere to insert an include?

<%
lots
and
lots
>%
<!--#include file="get.asp"-->
<%
of
lines
of
code
%>
0
 
LVL 30

Expert Comment

by:Wayne Barron
ID: 38809469
Absolutely!

Lets say that you have a page, and within that page, you have 1000 lines of code.
Database Connections.
Page Layout
exc...

You can do it like this.
(Lets just say that you are using tables, as this is 90% of my work, I am trying to convert over to DIV, however, it is a slow process)

<table>
<tr>
<%if request.QueryString("Admin") then%>
<td>Admin Menu</td>
<%elseif request.QueryString("Admin") then%>
<td>Normal Menu</td>
<%end if%>

<%if rs("stack")="Even" then%>
<td>Your stacked content is here</td>
<%elseif rs("stack")="OffSet" then%>
<td>Your stacked content is going to fall off the face of the earth</td>
<%end if%>


<%if rs("Ads")="User1" then%>
<td>1st Right Ads</td>
<%elseif rs("Ads")="User2" then%>
<td>2nd Right Ads</td>
<%end if%>
</table>

Open in new window


With the above scenario, it first deals with a Querystring, to see which menu to show.
Then we look at Database records to see which content to show.
Then we also check the Database records, to see which ads to show which user.

It is that simple, and makes for really easy debugging.

You can find more example here
http://www.cffcs.com

Let me know if you need any more advice on this Dynamic Coding Subject.
I live for this type of projects :)

Carrzkiss
0
 

Author Comment

by:Bob Schneider
ID: 38809479
Incredibly helpful!  Thanks for going above and beyond on this one!!!
0
 
LVL 30

Expert Comment

by:Wayne Barron
ID: 38809632
No problem.
I know sharing the Knowledge that I know, when I am able to.
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 38810253
I hope you guys are on the east coast - much too early!  Good luck!
0
 
LVL 30

Expert Comment

by:Wayne Barron
ID: 38810990
Yep, East coast for me.
Woke up at 3:30am, and laid back down around 8:00am, and just got back up about 30 minutes ago.
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 38811034
Ha, I do that too.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MS Outlook 2010 Cannot Display Image or Red X 36 32
Exchange 2007 3 35
OUTLOOK NOT receiving email from YAHOO - Error  0x80042110 7 122
Hide Table in merge 3 10
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
This Micro Tutorial demonstrates in Google Analytics how to create a custom report that shows you traffic over time using the month of year dimensions. There are also instructions on how to fix Google's odd month of year formatting, which Microsoft …
This Micro Tutorial demonstrates in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question