Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Excel 2007 VBA email via SMTP

Posted on 2014-04-28
5
Medium Priority
?
1,966 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 28

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

916 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