Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Excel 2007 VBA email via SMTP

Posted on 2014-04-28
5
Medium Priority
?
1,935 Views
Last Modified: 2014-04-28
Can someone review the following VBA code?  I am getting the error:

SendUsing configuration Value is invalid

Sub TestCopy()
   
   '///////////////////////////////////////
'//SMTP Configuration Settings

        Dim Msg As String
   
        Dim FileName As String
        Dim iMsg As Object
        Dim iConf As Object
        Dim Flds As Variant
        Dim Attach As String
       
        Attach = "C:\Users\Glen\Documents\Passwords.xlsx"
   
        Set iMsg = CreateObject("CDO.Message")
        Set iConf = CreateObject("CDO.Configuration")
        iConf.Load -1
        Set Flds = iConf.Fields
        With Flds
       
                     
        '//SMTP Configuration for SMTPCORP.COM
             .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") _
                             = "smtpcorp.com"
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 2525
             .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
            .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "gpspow"
             .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "99999"
            .Update
        End With
         Msg = "Test message area" & vbCrLf & vbCrLf
        'Create Mail Item ans send it
        With iMsg
                Set .Configuration = iConf
                .To = "gpspow55@gmail.com"
                 '.To = EmailAddr
                .From = """Glen"" <gpspow55@gmail.com>"
                '.CC = "" ';"PHanson@sleh.com"
                 .CC = "gpspow55@gmail.com;"
                .BCC = ""
                .Subject = Subj
                .TextBody = Msg
                .AddAttachment Attach
                 .Send
        End With
'//////////////////////////////////////

           

           
           
           
End Sub


Thanks

Glen
0
Comment
Question by:GPSPOW
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 27

Assisted Solution

by:MacroShadow
MacroShadow earned 500 total points
ID: 40027946
You're missing several configuration items:
sendusing
'Specifies the method used to send messages:
'(1) Local SMTP Pickup Service (2) Use SMTP Over Network (3) Use Exchange Server
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = <whatever>

smptserverport
'The SMTP Port which must be enabled in your network by ISP or local Firewall
.Item("http://schemas.microsoft.com/cdo/configuration/smptserverport") = <whatever>

smtpusessl
'Indicates whether Secure Sockets Layer (SSL) should be used when
'sending messages using the SMTP protocol over the network or not.
'SSL/STARTTLS: Boolean
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = <True or False>

smtpconnectiontimeout
'Maximum Time in Seconds CDO will try to Establish Connection
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = <whatever>

The error message is caused by missing the first item.
0
 
LVL 35

Assisted Solution

by:Bembi
Bembi earned 500 total points
ID: 40027952
From my quick view over the code...
The smtp server has to be a server name, so i.e. myEmailserver.smtpcorp.com

Most Servers listen on port 25 for SMTP. So if you use any other port, the server has to listen on this port for SMTP connections, what has to be set up in the email server.

The other code looks fine for my imagination.
0
 
LVL 15

Accepted Solution

by:
WalkaboutTigger earned 1000 total points
ID: 40027965
Add

        .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'The CDO Sendusing Port

after

'//SMTP Configuration for SMTPCORP.COM
0
 

Author Closing Comment

by:GPSPOW
ID: 40027981
All the solutions were good.  WalkAboutTigger told me exactly what I needed to do.

Thanks

Glen
0
 
LVL 15

Expert Comment

by:WalkaboutTigger
ID: 40028019
As a matter of coding practice, you may want to

Dim cdoSchema

cdoSchema = "http://schemas.microsoft.com/cdo/configuration/"

and then use constructs such as

.item(cdoSchema & "sendusing") = 2

This makes the code more readable and helps to prevents typographical errors.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

722 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