Solved

Excel 2007 VBA email via SMTP

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

Assisted Solution

by:MacroShadow
MacroShadow earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
All the solutions were good.  WalkAboutTigger told me exactly what I needed to do.

Thanks

Glen
0
 
LVL 15

Expert Comment

by:WalkaboutTigger
Comment Utility
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

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

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

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