Solved

Sending CDO SMTP Mail via gmail

Posted on 2013-01-22
20
1,880 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 30

Assisted Solution

by:Wayne Barron
Wayne Barron earned 286 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Bob Schneider
Comment Utility
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
Comment Utility
Excellent!  Thanks!!
0
 
LVL 30

Expert Comment

by:Wayne Barron
Comment Utility
<%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
Comment Utility
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
Comment Utility
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
Comment Utility
Incredibly helpful!  Thanks for going above and beyond on this one!!!
0
 
LVL 30

Expert Comment

by:Wayne Barron
Comment Utility
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
Comment Utility
I hope you guys are on the east coast - much too early!  Good luck!
0
 
LVL 30

Expert Comment

by:Wayne Barron
Comment Utility
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
Comment Utility
Ha, I do that too.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Explore the encryption capabilities built into Google Apps and how these features can help you meet privacy policy and regulatory compliance, but are not a full solution. Understand and compare the most popular email encryption services for Google A…
Create high volume marketing opportunities using email signatures with these top 10 DOs and DON'Ts of email signature marketing.
This Micro Tutorial  demonstrates whether your site uses one subdomain or multiple subdomains, how to create full URLs from Google Analytics content reports. This procedure is called concatenation and can also be done with the CONCATENTATE function.…
This Micro Tutorial will demonstrate importing calendar invites from events such as webinars into your Google Calendar.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now