Solved

Excel 2007 VBA email via SMTP

Posted on 2014-04-28
5
1,869 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 125 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 125 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 250 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why Won't My Excel Workbook Refresh The Data 2 47
Excel VBA inserting a formula 12 39
date format is changing when i run the macro 3 20
Excel Index/Match issue 4 20
A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

752 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