Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending email with Outlook 2010 - keeps giving run-time error 287

Status
Not open for further replies.

AncientTiger

Programmer
Jul 5, 2001
238
0
0
US
Ok, I'm at my wits end... this SHOULD be so simple, yet I've beat my head against a brick wall....

I'm trying to set up a function in my project to send emails using Microsoft Outlook 2010, and every code example I find fails.

My specs:
Windows 7 64bit
Visual Basic 6 / Visual Studios 6
I have referenced the Microsoft Outlook 14.0 Object Library (msoutl.olb)​

Here's my function code

Code:
Public Function SENDOUTLOOKEMAIL(ByVal OUTEMLIST As String, ByVal OUTSUBJ As String, ByVal OUTMSSG As String, ByVal OUTFILES As String) As Boolean
Dim oApp As Outlook.Application
Dim oEmail As Outlook.MailItem
Set oApp = New Outlook.Application
Set oEmail = oApp.CreateItem(olMailItem)
With oEmail
    If BCCIT = True Then
        .BCC = OUTEMLIST
    Else
        .To = OUTEMLIST
    End If
    .Subject = OUTSUBJ
    .Body = OUTMSSG
    .Recipients.ResolveAll
    .SAVE
    On Error Resume Next
    .Send
    If Err <> 0 Then
        X = MsgBox("Error Occured: " + Err.Description)
        Err.Clear
        SENDOUTLOOKEMAIL = False
    Else
        X = MsgBox("Email Successfully Sent!!", vbInformation)
        SENDOUTLOOKEMAIL = True
    End If
End With
Set oEmail = Nothing
oApp.Quit
Set oApp = Nothing
    
Exit Function

It gets to .Recipients.ResolveAll, then gives a "Run-time error '287': Application-defined or object-defined error"

I'm not sure what else to do :(

Any help would be GREATLY appreciated....also, my workplace is getting Office 2013... this script SHOULD work the same with Outlook 2013, right?


------------------------------------
[yinyang] Over 20 years of programming, and still learning every day! [yinyang]
 
Correction to the function header...

Code:
Public Function SENDOUTLOOKEMAIL(ByVal OUTEMLIST As String, ByVal OUTSUBJ As String, ByVal OUTMSSG As String, ByVal OUTFILES As String, ByVal BCCIT as Boolean) As Boolean

------------------------------------
[yinyang] Over 20 years of programming, and still learning every day! [yinyang]
 
This is what I use to send e-mails from my code.
You just need to set your cdoSMTPServer and pass some parameters into this Sub.


Code:
[green]
'''Add Referebce to: Microsoft CDO For Windows 2000 Library
[/green]
Public Sub SendAMessage(strFrom As String, strTo As String, _
    strCC As String, strSubject As String, strTextBody As String, _
    Optional strBcc As String, Optional strAttachDoc As String)

Dim objMessage As CDO.Message

On Error GoTo MyErrorHadler

Set objMessage = New CDO.Message

With objMessage
    .From = strFrom
    .To = strTo
    If Len(Trim$(strCC)) > 0 Then
        .CC = strCC
    End If
    If Len(strBcc) > 0 Then
        .BCC = strBcc
    End If[green]
    ''' On behalf of
    '.Sender = "Bill.Gates@msn.com"[/green]
    .Subject = strSubject
    .TextBody = strTextBody
    
    If Len(strAttachDoc) > 0 Then
        .AddAttachment strAttachDoc
    End If
    
    With .Configuration.Fields
        .Item(CDO.cdoSMTPServer) = [red]"YOUR.SMTP.SERVER.LAN"[/red]
        .Item(CDO.cdoSMTPServerPort) = 25
        .Item(CDO.cdoSendUsingMethod) = CDO.cdoSendUsingPort
        .Item(cdoSMTPConnectionTimeout) = 10
        .Update
    End With
    .Send
End With

Set objMessage = Nothing

Exit Sub
MyErrorHadler:
[green]
'' Do something here in case of an error
[/green]
Resume Next

End Sub

You may also want to contact your e-mail admin to allow your application to send e-mails this way. (because you can send a LOT of e-mails this way, i.e. SPAM)

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Thank you for the reply Andrzejek!

I've seen examples of the CDO sending method, however I'm not able to use that in the environment where this script will be used :(

I do appreciate the suggestion though :)

------------------------------------
[yinyang] Over 20 years of programming, and still learning every day! [yinyang]
 
Oh, I'd imagine this is Outlook's security settings.

You could try set Allow Programmatic Access in Outlook to 'always' or 'yes'. (not best practice, but it would at least let us verify whether this is the issue)

 
Thanks for the reply strongm!

Tried messing with trust center settings but to no avail.

One thing I DID discover is that if outlook is running, the email sends just fine. When I close out outlook and try to send, I get the error message again.

Probably something in the settings that I'm just not seeing?

Also, I've tried this very same script on another partition that's running Office 2013 now, and it works just fine, whether out look is open or closed.

Again... I'm leaning more toward some obscure outlook setting that I'm not familiar with...

------------------------------------
[yinyang] Over 20 years of programming, and still learning every day! [yinyang]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top