Solved

Excel 2007 VBA email via SMTP

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
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.

867 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

16 Experts available now in Live!

Get 1:1 Help Now