Send Email from a certain Mailbox 
Author Message
 Send Email from a certain Mailbox

Does anyone know how to create a Mail message in VBA with a different
mailbox other then the user running the code.  I know how to create a
message with but it uses the users outlook to send it off.  I would like to
be able to send a message from one mailbox regardless who runs the code.

Thanks
Rob.



Sun, 18 Jan 2004 01:10:48 GMT  
 Send Email from a certain Mailbox
Hi Rob,
I believe the only way to do that is by
using CDO. The CDO library is installed by default
with Outlook 98 but you must explicitly choose
to install it with Outlook 2000.

I have code to do what you want, but it's at work.
I'll post it tomorrow.

Dan

Quote:

> Does anyone know how to create a Mail message in VBA with a different
> mailbox other then the user running the code.  I know how to create a
> message with but it uses the users outlook to send it off.  I would like to
> be able to send a message from one mailbox regardless who runs the code.

> Thanks
> Rob.



Sun, 18 Jan 2004 05:44:01 GMT  
 Send Email from a certain Mailbox
Hey Thanks Dan!!  Yes I would love the Code..


Quote:
> Hi Rob,
> I believe the only way to do that is by
> using CDO. The CDO library is installed by default
> with Outlook 98 but you must explicitly choose
> to install it with Outlook 2000.

> I have code to do what you want, but it's at work.
> I'll post it tomorrow.

> Dan


> > Does anyone know how to create a Mail message in VBA with a different
> > mailbox other then the user running the code.  I know how to create a
> > message with but it uses the users outlook to send it off.  I would like
to
> > be able to send a message from one mailbox regardless who runs the code.

> > Thanks
> > Rob.



Sun, 18 Jan 2004 06:49:46 GMT  
 Send Email from a certain Mailbox
Hi Rob,
I've located my code, however it's
not as straight foreward as I thought.

I got a 'mapi class' somebody wrote and
used that. I'll post the code for the
class, which you can use. It's quite
convenient actually, but I don't know
how comfortable you'll be with this.

You'll see where I hard coded my mailbox
in the class module itself (MAPILogon sub). Simply
substitute your mailbox there.

I'll also post the code I used from the
form to send mail.

Here goes:

'**************** Class Start ***********************

Option Explicit

Private mobjSession As MAPI.Session
Private mobjMessage As Message
Private mboolErr As Boolean

Private mobjNewMessage As Message

Private Const mcERR_DOH = vbObjectError + 10000
Private Const mcERR_DECIMAL = 261144    'low word order +1000

Public Sub MAPIAddMessage()
    With mobjSession
        Set mobjNewMessage = .Outbox.Messages.Add
    End With
End Sub

Public Sub MAPIUpdateMessage()
    mobjNewMessage.Update
End Sub

Private Sub Class_Initialize()
    mboolErr = False
End Sub

Private Sub Class_Terminate()
    On Error Resume Next
    Set mobjMessage = Nothing
    mobjSession.Logoff
    Set mobjSession = Nothing
End Sub

Public Property Let MAPISetMessageBody(stBodyText As String)
    If Len(stBodyText) > 0 Then mobjNewMessage.Text = stBodyText
End Property

Public Property Let MAPISetMessageSubject(stSubject As String)
    If Len(stSubject) > 0 Then mobjNewMessage.Subject = stSubject
End Property

Public Property Get MAPIIsError() As Boolean
    MAPIIsError = mboolErr
End Property

Public Property Get MAPIRecipientCount() As Integer
    MAPIRecipientCount = mobjNewMessage.Recipients.Count
End Property

Public Sub MAPIAddAttachment(stFile As String, _
                        Optional stLabel As Variant)
Dim objAttachment As Attachment
Dim stMsg As String

    On Error GoTo Error_MAPIAddAttachment

    If mboolErr Then Err.Raise mcERR_DOH
    If Len(Dir(stFile)) = 0 Then Err.Raise mcERR_DOH + 10

    If IsMissing(stLabel) Then stLabel = CStr(stFile)

    With mobjNewMessage
        .Text = " " & mobjNewMessage.Text
        Set objAttachment = .Attachments.Add
        With objAttachment
            .Position = 0
            .Name = stLabel
            'no need to link a file me thinks
            .Type = CdoFileData
            .ReadFromFile stFile
        End With
        .Update
    End With

Exit_MAPIAddAttachment:
    Set objAttachment = Nothing
    Exit Sub
Error_MAPIAddAttachment:
    mboolErr = True
    If Err = mcERR_DOH + 10 Then
        stMsg = "Couldn't locate the file " & vbCrLf
        stMsg = stMsg & "'" & stFile & "'." & vbCrLf
        stMsg = stMsg & "Please check the file name and path and try again."
        MsgBox stMsg, vbExclamation + vbOKOnly, "File Not Found"
    ElseIf Err <> mcERR_DOH Then
        MsgBox "Error " & Err.Number & vbCrLf & Err.Description
    End If
    Resume Exit_MAPIAddAttachment
End Sub

Public Sub MAPIAddRecipient(stPerson As String, intAddressType As Integer)
Dim objNewRecipient As Recipient 'local

    On Error GoTo Error_MAPIAddRecipient

    If mboolErr Then Err.Raise mcERR_DOH

    'If there's no SMTP present in the stPerson var, then
    'we have to use Name, else Address
    With mobjNewMessage
        If InStr(1, stPerson, "SMTP:") > 0 Then
            Set objNewRecipient = .Recipients.Add(Address:=stPerson, _
                                                Type:=intAddressType)
        Else
            Set objNewRecipient = .Recipients.Add(Name:=stPerson, _
                                                Type:=intAddressType)
        End If
        objNewRecipient.Resolve
    End With

Exit_MAPIAddRecipient:
    Set objNewRecipient = Nothing
    Exit Sub

Error_MAPIAddRecipient:
    mboolErr = True
    Resume Exit_MAPIAddRecipient
End Sub

Public Sub MAPISendMessage(Optional boolSaveCopy As Variant, _
                            Optional boolShowDialog As Variant)

    If IsMissing(boolSaveCopy) Then
        boolSaveCopy = True
    End If
    If IsMissing(boolShowDialog) Then
        boolShowDialog = False
    End If
    mobjNewMessage.Send savecopy:=boolSaveCopy, showdialog:=boolShowDialog
    mobjSession.DeliverNow
End Sub

Public Sub MAPILogon()
On Error GoTo err_sMAPILogon
Const cERROR_USERCANCEL = -2147221229

    Set mobjSession = CreateObject("MAPI.Session")
    mobjSession.Logon "Dan Artuso", , False

exit_sMAPILogon:
    Exit Sub

err_sMAPILogon:
    mboolErr = True
    If Err = CdoE_LOGON_FAILED - mcERR_DECIMAL Then
        MsgBox "Logon Failed", vbCritical + vbOKOnly, "Error"
    ElseIf Err = cERROR_USERCANCEL Then
        MsgBox "Aborting since you pressed cancel.", _
                vbOKOnly + vbInformation, "Operation Cancelled!"
    Else
        MsgBox "Error number " & Err - mcERR_DECIMAL & " description. " _
                & Error$(Err)
    End If
    Resume exit_sMAPILogon
End Sub

Public Sub MAPILogoff()
On Error GoTo err_sMAPILogoff

    mobjSession.Logoff

    Set mobjNewMessage = Nothing
    Set mobjSession = Nothing

exit_sMAPILogoff:
    Exit Sub

err_sMAPILogoff:
    Resume exit_sMAPILogoff
End Sub
'**************** Class End  ***********************

Form code:

 'get the people who need reminders sent to them
 rsEmailRem.Open strSqlEmailRem, cn, adOpenStatic

 Dim clMAPI As clsMAPIEmail
 Set clMAPI = New clsMAPIEmail

 'send email out for each record
 Do While Not rsEmailRem.EOF
    With clMAPI
         .MAPILogon
         .MAPIAddMessage
         .MAPISetMessageBody = rsPeriod!Message
         .MAPISetMessageSubject = rsPeriod!Subject & " " &
rsEmailRem!FileName
         .MAPIAddRecipient rsEmailRem!Email, 1
         .MAPIUpdateMessage
         .MAPISendMessage False
         .MAPILogoff
     End With
     rsEmailRem.MoveNext
 Loop

If you can't make heads or tails of this, post back.

HTH
Dan


Quote:
> Hey Thanks Dan!!  Yes I would love the Code..



> > Hi Rob,
> > I believe the only way to do that is by
> > using CDO. The CDO library is installed by default
> > with Outlook 98 but you must explicitly choose
> > to install it with Outlook 2000.

> > I have code to do what you want, but it's at work.
> > I'll post it tomorrow.

> > Dan


> > > Does anyone know how to create a Mail message in VBA with a different
> > > mailbox other then the user running the code.  I know how to create a
> > > message with but it uses the users outlook to send it off.  I would
like
> to
> > > be able to send a message from one mailbox regardless who runs the
code.

> > > Thanks
> > > Rob.



Sun, 18 Jan 2004 22:33:08 GMT  
 Send Email from a certain Mailbox
Hi Dan! Thanks again for the post...I think I have it figured it out but
what do these to variables do for the form code?

Dim clMAPI As clsMAPIEmail
Set clMAPI = New clsMAPIEmail

Rob.


Quote:
> Hi Rob,
> I've located my code, however it's
> not as straight foreward as I thought.

> I got a 'mapi class' somebody wrote and
> used that. I'll post the code for the
> class, which you can use. It's quite
> convenient actually, but I don't know
> how comfortable you'll be with this.

> You'll see where I hard coded my mailbox
> in the class module itself (MAPILogon sub). Simply
> substitute your mailbox there.

> I'll also post the code I used from the
> form to send mail.

> Here goes:

> '**************** Class Start ***********************

> Option Explicit

> Private mobjSession As MAPI.Session
> Private mobjMessage As Message
> Private mboolErr As Boolean

> Private mobjNewMessage As Message

> Private Const mcERR_DOH = vbObjectError + 10000
> Private Const mcERR_DECIMAL = 261144    'low word order +1000

> Public Sub MAPIAddMessage()
>     With mobjSession
>         Set mobjNewMessage = .Outbox.Messages.Add
>     End With
> End Sub

> Public Sub MAPIUpdateMessage()
>     mobjNewMessage.Update
> End Sub

> Private Sub Class_Initialize()
>     mboolErr = False
> End Sub

> Private Sub Class_Terminate()
>     On Error Resume Next
>     Set mobjMessage = Nothing
>     mobjSession.Logoff
>     Set mobjSession = Nothing
> End Sub

> Public Property Let MAPISetMessageBody(stBodyText As String)
>     If Len(stBodyText) > 0 Then mobjNewMessage.Text = stBodyText
> End Property

> Public Property Let MAPISetMessageSubject(stSubject As String)
>     If Len(stSubject) > 0 Then mobjNewMessage.Subject = stSubject
> End Property

> Public Property Get MAPIIsError() As Boolean
>     MAPIIsError = mboolErr
> End Property

> Public Property Get MAPIRecipientCount() As Integer
>     MAPIRecipientCount = mobjNewMessage.Recipients.Count
> End Property

> Public Sub MAPIAddAttachment(stFile As String, _
>                         Optional stLabel As Variant)
> Dim objAttachment As Attachment
> Dim stMsg As String

>     On Error GoTo Error_MAPIAddAttachment

>     If mboolErr Then Err.Raise mcERR_DOH
>     If Len(Dir(stFile)) = 0 Then Err.Raise mcERR_DOH + 10

>     If IsMissing(stLabel) Then stLabel = CStr(stFile)

>     With mobjNewMessage
>         .Text = " " & mobjNewMessage.Text
>         Set objAttachment = .Attachments.Add
>         With objAttachment
>             .Position = 0
>             .Name = stLabel
>             'no need to link a file me thinks
>             .Type = CdoFileData
>             .ReadFromFile stFile
>         End With
>         .Update
>     End With

> Exit_MAPIAddAttachment:
>     Set objAttachment = Nothing
>     Exit Sub
> Error_MAPIAddAttachment:
>     mboolErr = True
>     If Err = mcERR_DOH + 10 Then
>         stMsg = "Couldn't locate the file " & vbCrLf
>         stMsg = stMsg & "'" & stFile & "'." & vbCrLf
>         stMsg = stMsg & "Please check the file name and path and try
again."
>         MsgBox stMsg, vbExclamation + vbOKOnly, "File Not Found"
>     ElseIf Err <> mcERR_DOH Then
>         MsgBox "Error " & Err.Number & vbCrLf & Err.Description
>     End If
>     Resume Exit_MAPIAddAttachment
> End Sub

> Public Sub MAPIAddRecipient(stPerson As String, intAddressType As Integer)
> Dim objNewRecipient As Recipient 'local

>     On Error GoTo Error_MAPIAddRecipient

>     If mboolErr Then Err.Raise mcERR_DOH

>     'If there's no SMTP present in the stPerson var, then
>     'we have to use Name, else Address
>     With mobjNewMessage
>         If InStr(1, stPerson, "SMTP:") > 0 Then
>             Set objNewRecipient = .Recipients.Add(Address:=stPerson, _
>                                                 Type:=intAddressType)
>         Else
>             Set objNewRecipient = .Recipients.Add(Name:=stPerson, _
>                                                 Type:=intAddressType)
>         End If
>         objNewRecipient.Resolve
>     End With

> Exit_MAPIAddRecipient:
>     Set objNewRecipient = Nothing
>     Exit Sub

> Error_MAPIAddRecipient:
>     mboolErr = True
>     Resume Exit_MAPIAddRecipient
> End Sub

> Public Sub MAPISendMessage(Optional boolSaveCopy As Variant, _
>                             Optional boolShowDialog As Variant)

>     If IsMissing(boolSaveCopy) Then
>         boolSaveCopy = True
>     End If
>     If IsMissing(boolShowDialog) Then
>         boolShowDialog = False
>     End If
>     mobjNewMessage.Send savecopy:=boolSaveCopy, showdialog:=boolShowDialog
>     mobjSession.DeliverNow
> End Sub

> Public Sub MAPILogon()
> On Error GoTo err_sMAPILogon
> Const cERROR_USERCANCEL = -2147221229

>     Set mobjSession = CreateObject("MAPI.Session")
>     mobjSession.Logon "Dan Artuso", , False

> exit_sMAPILogon:
>     Exit Sub

> err_sMAPILogon:
>     mboolErr = True
>     If Err = CdoE_LOGON_FAILED - mcERR_DECIMAL Then
>         MsgBox "Logon Failed", vbCritical + vbOKOnly, "Error"
>     ElseIf Err = cERROR_USERCANCEL Then
>         MsgBox "Aborting since you pressed cancel.", _
>                 vbOKOnly + vbInformation, "Operation Cancelled!"
>     Else
>         MsgBox "Error number " & Err - mcERR_DECIMAL & " description. " _
>                 & Error$(Err)
>     End If
>     Resume exit_sMAPILogon
> End Sub

> Public Sub MAPILogoff()
> On Error GoTo err_sMAPILogoff

>     mobjSession.Logoff

>     Set mobjNewMessage = Nothing
>     Set mobjSession = Nothing

> exit_sMAPILogoff:
>     Exit Sub

> err_sMAPILogoff:
>     Resume exit_sMAPILogoff
> End Sub
> '**************** Class End  ***********************

> Form code:

>  'get the people who need reminders sent to them
>  rsEmailRem.Open strSqlEmailRem, cn, adOpenStatic

>  Dim clMAPI As clsMAPIEmail
>  Set clMAPI = New clsMAPIEmail

>  'send email out for each record
>  Do While Not rsEmailRem.EOF
>     With clMAPI
>          .MAPILogon
>          .MAPIAddMessage
>          .MAPISetMessageBody = rsPeriod!Message
>          .MAPISetMessageSubject = rsPeriod!Subject & " " &
> rsEmailRem!FileName
>          .MAPIAddRecipient rsEmailRem!Email, 1
>          .MAPIUpdateMessage
>          .MAPISendMessage False
>          .MAPILogoff
>      End With
>      rsEmailRem.MoveNext
>  Loop

> If you can't make heads or tails of this, post back.

> HTH
> Dan



> > Hey Thanks Dan!!  Yes I would love the Code..



> > > Hi Rob,
> > > I believe the only way to do that is by
> > > using CDO. The CDO library is installed by default
> > > with Outlook 98 but you must explicitly choose
> > > to install it with Outlook 2000.

> > > I have code to do what you want, but it's at work.
> > > I'll post it tomorrow.

> > > Dan


> > > > Does anyone know how to create a Mail message in VBA with a
different
> > > > mailbox other then the user running the code.  I know how to create
a
> > > > message with but it uses the users outlook to send it off.  I would
> like
> > to
> > > > be able to send a message from one mailbox regardless who runs the
> code.

> > > > Thanks
> > > > Rob.



Mon, 19 Jan 2004 04:13:42 GMT  
 Send Email from a certain Mailbox
Hi Rob,
Well, the code for the class goes in a class module.
So you now have a clsMAPIEmail class (this is
what you must name your class) with properties
and methods you can use.

Now you have to instantiate an instance of
your class in order to use it. That's where those two
lines come in.

Of course you can just use some of the code directly
if you don't want to create a class.

Dan

Quote:

> Hi Dan! Thanks again for the post...I think I have it figured it out but
> what do these to variables do for the form code?

> Dim clMAPI As clsMAPIEmail
> Set clMAPI = New clsMAPIEmail

> Rob.



> > Hi Rob,
> > I've located my code, however it's
> > not as straight foreward as I thought.

> > I got a 'mapi class' somebody wrote and
> > used that. I'll post the code for the
> > class, which you can use. It's quite
> > convenient actually, but I don't know
> > how comfortable you'll be with this.

> > You'll see where I hard coded my mailbox
> > in the class module itself (MAPILogon sub). Simply
> > substitute your mailbox there.

> > I'll also post the code I used from the
> > form to send mail.

> > Here goes:

> > '**************** Class Start ***********************

> > Option Explicit

> > Private mobjSession As MAPI.Session
> > Private mobjMessage As Message
> > Private mboolErr As Boolean

> > Private mobjNewMessage As Message

> > Private Const mcERR_DOH = vbObjectError + 10000
> > Private Const mcERR_DECIMAL = 261144    'low word order +1000

> > Public Sub MAPIAddMessage()
> >     With mobjSession
> >         Set mobjNewMessage = .Outbox.Messages.Add
> >     End With
> > End Sub

> > Public Sub MAPIUpdateMessage()
> >     mobjNewMessage.Update
> > End Sub

> > Private Sub Class_Initialize()
> >     mboolErr = False
> > End Sub

> > Private Sub Class_Terminate()
> >     On Error Resume Next
> >     Set mobjMessage = Nothing
> >     mobjSession.Logoff
> >     Set mobjSession = Nothing
> > End Sub

> > Public Property Let MAPISetMessageBody(stBodyText As String)
> >     If Len(stBodyText) > 0 Then mobjNewMessage.Text = stBodyText
> > End Property

> > Public Property Let MAPISetMessageSubject(stSubject As String)
> >     If Len(stSubject) > 0 Then mobjNewMessage.Subject = stSubject
> > End Property

> > Public Property Get MAPIIsError() As Boolean
> >     MAPIIsError = mboolErr
> > End Property

> > Public Property Get MAPIRecipientCount() As Integer
> >     MAPIRecipientCount = mobjNewMessage.Recipients.Count
> > End Property

> > Public Sub MAPIAddAttachment(stFile As String, _
> >                         Optional stLabel As Variant)
> > Dim objAttachment As Attachment
> > Dim stMsg As String

> >     On Error GoTo Error_MAPIAddAttachment

> >     If mboolErr Then Err.Raise mcERR_DOH
> >     If Len(Dir(stFile)) = 0 Then Err.Raise mcERR_DOH + 10

> >     If IsMissing(stLabel) Then stLabel = CStr(stFile)

> >     With mobjNewMessage
> >         .Text = " " & mobjNewMessage.Text
> >         Set objAttachment = .Attachments.Add
> >         With objAttachment
> >             .Position = 0
> >             .Name = stLabel
> >             'no need to link a file me thinks
> >             .Type = CdoFileData
> >             .ReadFromFile stFile
> >         End With
> >         .Update
> >     End With

> > Exit_MAPIAddAttachment:
> >     Set objAttachment = Nothing
> >     Exit Sub
> > Error_MAPIAddAttachment:
> >     mboolErr = True
> >     If Err = mcERR_DOH + 10 Then
> >         stMsg = "Couldn't locate the file " & vbCrLf
> >         stMsg = stMsg & "'" & stFile & "'." & vbCrLf
> >         stMsg = stMsg & "Please check the file name and path and try
> again."
> >         MsgBox stMsg, vbExclamation + vbOKOnly, "File Not Found"
> >     ElseIf Err <> mcERR_DOH Then
> >         MsgBox "Error " & Err.Number & vbCrLf & Err.Description
> >     End If
> >     Resume Exit_MAPIAddAttachment
> > End Sub

> > Public Sub MAPIAddRecipient(stPerson As String, intAddressType As Integer)
> > Dim objNewRecipient As Recipient 'local

> >     On Error GoTo Error_MAPIAddRecipient

> >     If mboolErr Then Err.Raise mcERR_DOH

> >     'If there's no SMTP present in the stPerson var, then
> >     'we have to use Name, else Address
> >     With mobjNewMessage
> >         If InStr(1, stPerson, "SMTP:") > 0 Then
> >             Set objNewRecipient = .Recipients.Add(Address:=stPerson, _
> >                                                 Type:=intAddressType)
> >         Else
> >             Set objNewRecipient = .Recipients.Add(Name:=stPerson, _
> >                                                 Type:=intAddressType)
> >         End If
> >         objNewRecipient.Resolve
> >     End With

> > Exit_MAPIAddRecipient:
> >     Set objNewRecipient = Nothing
> >     Exit Sub

> > Error_MAPIAddRecipient:
> >     mboolErr = True
> >     Resume Exit_MAPIAddRecipient
> > End Sub

> > Public Sub MAPISendMessage(Optional boolSaveCopy As Variant, _
> >                             Optional boolShowDialog As Variant)

> >     If IsMissing(boolSaveCopy) Then
> >         boolSaveCopy = True
> >     End If
> >     If IsMissing(boolShowDialog) Then
> >         boolShowDialog = False
> >     End If
> >     mobjNewMessage.Send savecopy:=boolSaveCopy, showdialog:=boolShowDialog
> >     mobjSession.DeliverNow
> > End Sub

> > Public Sub MAPILogon()
> > On Error GoTo err_sMAPILogon
> > Const cERROR_USERCANCEL = -2147221229

> >     Set mobjSession = CreateObject("MAPI.Session")
> >     mobjSession.Logon "Dan Artuso", , False

> > exit_sMAPILogon:
> >     Exit Sub

> > err_sMAPILogon:
> >     mboolErr = True
> >     If Err = CdoE_LOGON_FAILED - mcERR_DECIMAL Then
> >         MsgBox "Logon Failed", vbCritical + vbOKOnly, "Error"
> >     ElseIf Err = cERROR_USERCANCEL Then
> >         MsgBox "Aborting since you pressed cancel.", _
> >                 vbOKOnly + vbInformation, "Operation Cancelled!"
> >     Else
> >         MsgBox "Error number " & Err - mcERR_DECIMAL & " description. " _
> >                 & Error$(Err)
> >     End If
> >     Resume exit_sMAPILogon
> > End Sub

> > Public Sub MAPILogoff()
> > On Error GoTo err_sMAPILogoff

> >     mobjSession.Logoff

> >     Set mobjNewMessage = Nothing
> >     Set mobjSession = Nothing

> > exit_sMAPILogoff:
> >     Exit Sub

> > err_sMAPILogoff:
> >     Resume exit_sMAPILogoff
> > End Sub
> > '**************** Class End  ***********************

> > Form code:

> >  'get the people who need reminders sent to them
> >  rsEmailRem.Open strSqlEmailRem, cn, adOpenStatic

> >  Dim clMAPI As clsMAPIEmail
> >  Set clMAPI = New clsMAPIEmail

> >  'send email out for each record
> >  Do While Not rsEmailRem.EOF
> >     With clMAPI
> >          .MAPILogon
> >          .MAPIAddMessage
> >          .MAPISetMessageBody = rsPeriod!Message
> >          .MAPISetMessageSubject = rsPeriod!Subject & " " &
> > rsEmailRem!FileName
> >          .MAPIAddRecipient rsEmailRem!Email, 1
> >          .MAPIUpdateMessage
> >          .MAPISendMessage False
> >          .MAPILogoff
> >      End With
> >      rsEmailRem.MoveNext
> >  Loop

> > If you can't make heads or tails of this, post back.

> > HTH
> > Dan



> > > Hey Thanks Dan!!  Yes I would love the Code..



> > > > Hi Rob,
> > > > I believe the only way to do that is by
> > > > using CDO. The CDO library is installed by default
> > > > with Outlook 98 but you must explicitly choose
> > > > to install it with Outlook 2000.

> > > > I have code to do what you want, but it's at work.
> > > > I'll post it tomorrow.

> > > > Dan


> > > > > Does anyone know how to create a Mail message in VBA with a
> different
> > > > > mailbox other then the user running the code.  I know how to create
> a
> > > > > message with but it uses the users outlook to send it off.  I would
> > like
> > > to
> > > > > be able to send a message from one mailbox regardless who runs the
> > code.

> > > > > Thanks
> > > > > Rob.



Mon, 19 Jan 2004 08:42:14 GMT  
 Send Email from a certain Mailbox
Thanks Again for the Code.

Do you know of away to get this to work with out calling a profile?  Can we
have it logon to the server from a specific username?

Rob.


Quote:
> Hi Rob,
> Well, the code for the class goes in a class module.
> So you now have a clsMAPIEmail class (this is
> what you must name your class) with properties
> and methods you can use.

> Now you have to instantiate an instance of
> your class in order to use it. That's where those two
> lines come in.

> Of course you can just use some of the code directly
> if you don't want to create a class.

> Dan


> > Hi Dan! Thanks again for the post...I think I have it figured it out but
> > what do these to variables do for the form code?

> > Dim clMAPI As clsMAPIEmail
> > Set clMAPI = New clsMAPIEmail

> > Rob.



> > > Hi Rob,
> > > I've located my code, however it's
> > > not as straight foreward as I thought.

> > > I got a 'mapi class' somebody wrote and
> > > used that. I'll post the code for the
> > > class, which you can use. It's quite
> > > convenient actually, but I don't know
> > > how comfortable you'll be with this.

> > > You'll see where I hard coded my mailbox
> > > in the class module itself (MAPILogon sub). Simply
> > > substitute your mailbox there.

> > > I'll also post the code I used from the
> > > form to send mail.

> > > Here goes:

> > > '**************** Class Start ***********************

> > > Option Explicit

> > > Private mobjSession As MAPI.Session
> > > Private mobjMessage As Message
> > > Private mboolErr As Boolean

> > > Private mobjNewMessage As Message

> > > Private Const mcERR_DOH = vbObjectError + 10000
> > > Private Const mcERR_DECIMAL = 261144    'low word order +1000

> > > Public Sub MAPIAddMessage()
> > >     With mobjSession
> > >         Set mobjNewMessage = .Outbox.Messages.Add
> > >     End With
> > > End Sub

> > > Public Sub MAPIUpdateMessage()
> > >     mobjNewMessage.Update
> > > End Sub

> > > Private Sub Class_Initialize()
> > >     mboolErr = False
> > > End Sub

> > > Private Sub Class_Terminate()
> > >     On Error Resume Next
> > >     Set mobjMessage = Nothing
> > >     mobjSession.Logoff
> > >     Set mobjSession = Nothing
> > > End Sub

> > > Public Property Let MAPISetMessageBody(stBodyText As String)
> > >     If Len(stBodyText) > 0 Then mobjNewMessage.Text = stBodyText
> > > End Property

> > > Public Property Let MAPISetMessageSubject(stSubject As String)
> > >     If Len(stSubject) > 0 Then mobjNewMessage.Subject = stSubject
> > > End Property

> > > Public Property Get MAPIIsError() As Boolean
> > >     MAPIIsError = mboolErr
> > > End Property

> > > Public Property Get MAPIRecipientCount() As Integer
> > >     MAPIRecipientCount = mobjNewMessage.Recipients.Count
> > > End Property

> > > Public Sub MAPIAddAttachment(stFile As String, _
> > >                         Optional stLabel As Variant)
> > > Dim objAttachment As Attachment
> > > Dim stMsg As String

> > >     On Error GoTo Error_MAPIAddAttachment

> > >     If mboolErr Then Err.Raise mcERR_DOH
> > >     If Len(Dir(stFile)) = 0 Then Err.Raise mcERR_DOH + 10

> > >     If IsMissing(stLabel) Then stLabel = CStr(stFile)

> > >     With mobjNewMessage
> > >         .Text = " " & mobjNewMessage.Text
> > >         Set objAttachment = .Attachments.Add
> > >         With objAttachment
> > >             .Position = 0
> > >             .Name = stLabel
> > >             'no need to link a file me thinks
> > >             .Type = CdoFileData
> > >             .ReadFromFile stFile
> > >         End With
> > >         .Update
> > >     End With

> > > Exit_MAPIAddAttachment:
> > >     Set objAttachment = Nothing
> > >     Exit Sub
> > > Error_MAPIAddAttachment:
> > >     mboolErr = True
> > >     If Err = mcERR_DOH + 10 Then
> > >         stMsg = "Couldn't locate the file " & vbCrLf
> > >         stMsg = stMsg & "'" & stFile & "'." & vbCrLf
> > >         stMsg = stMsg & "Please check the file name and path and try
> > again."
> > >         MsgBox stMsg, vbExclamation + vbOKOnly, "File Not Found"
> > >     ElseIf Err <> mcERR_DOH Then
> > >         MsgBox "Error " & Err.Number & vbCrLf & Err.Description
> > >     End If
> > >     Resume Exit_MAPIAddAttachment
> > > End Sub

> > > Public Sub MAPIAddRecipient(stPerson As String, intAddressType As
Integer)
> > > Dim objNewRecipient As Recipient 'local

> > >     On Error GoTo Error_MAPIAddRecipient

> > >     If mboolErr Then Err.Raise mcERR_DOH

> > >     'If there's no SMTP present in the stPerson var, then
> > >     'we have to use Name, else Address
> > >     With mobjNewMessage
> > >         If InStr(1, stPerson, "SMTP:") > 0 Then
> > >             Set objNewRecipient = .Recipients.Add(Address:=stPerson, _
> > >                                                 Type:=intAddressType)
> > >         Else
> > >             Set objNewRecipient = .Recipients.Add(Name:=stPerson, _
> > >                                                 Type:=intAddressType)
> > >         End If
> > >         objNewRecipient.Resolve
> > >     End With

> > > Exit_MAPIAddRecipient:
> > >     Set objNewRecipient = Nothing
> > >     Exit Sub

> > > Error_MAPIAddRecipient:
> > >     mboolErr = True
> > >     Resume Exit_MAPIAddRecipient
> > > End Sub

> > > Public Sub MAPISendMessage(Optional boolSaveCopy As Variant, _
> > >                             Optional boolShowDialog As Variant)

> > >     If IsMissing(boolSaveCopy) Then
> > >         boolSaveCopy = True
> > >     End If
> > >     If IsMissing(boolShowDialog) Then
> > >         boolShowDialog = False
> > >     End If
> > >     mobjNewMessage.Send savecopy:=boolSaveCopy,

showdialog:=boolShowDialog

- Show quoted text -

Quote:
> > >     mobjSession.DeliverNow
> > > End Sub

> > > Public Sub MAPILogon()
> > > On Error GoTo err_sMAPILogon
> > > Const cERROR_USERCANCEL = -2147221229

> > >     Set mobjSession = CreateObject("MAPI.Session")
> > >     mobjSession.Logon "Dan Artuso", , False

> > > exit_sMAPILogon:
> > >     Exit Sub

> > > err_sMAPILogon:
> > >     mboolErr = True
> > >     If Err = CdoE_LOGON_FAILED - mcERR_DECIMAL Then
> > >         MsgBox "Logon Failed", vbCritical + vbOKOnly, "Error"
> > >     ElseIf Err = cERROR_USERCANCEL Then
> > >         MsgBox "Aborting since you pressed cancel.", _
> > >                 vbOKOnly + vbInformation, "Operation Cancelled!"
> > >     Else
> > >         MsgBox "Error number " & Err - mcERR_DECIMAL & " description.
" _
> > >                 & Error$(Err)
> > >     End If
> > >     Resume exit_sMAPILogon
> > > End Sub

> > > Public Sub MAPILogoff()
> > > On Error GoTo err_sMAPILogoff

> > >     mobjSession.Logoff

> > >     Set mobjNewMessage = Nothing
> > >     Set mobjSession = Nothing

> > > exit_sMAPILogoff:
> > >     Exit Sub

> > > err_sMAPILogoff:
> > >     Resume exit_sMAPILogoff
> > > End Sub
> > > '**************** Class End  ***********************

> > > Form code:

> > >  'get the people who need reminders sent to them
> > >  rsEmailRem.Open strSqlEmailRem, cn, adOpenStatic

> > >  Dim clMAPI As clsMAPIEmail
> > >  Set clMAPI = New clsMAPIEmail

> > >  'send email out for each record
> > >  Do While Not rsEmailRem.EOF
> > >     With clMAPI
> > >          .MAPILogon
> > >          .MAPIAddMessage
> > >          .MAPISetMessageBody = rsPeriod!Message
> > >          .MAPISetMessageSubject = rsPeriod!Subject & " " &
> > > rsEmailRem!FileName
> > >          .MAPIAddRecipient rsEmailRem!Email, 1
> > >          .MAPIUpdateMessage
> > >          .MAPISendMessage False
> > >          .MAPILogoff
> > >      End With
> > >      rsEmailRem.MoveNext
> > >  Loop

> > > If you can't make heads or tails of this, post back.

> > > HTH
> > > Dan



> > > > Hey Thanks Dan!!  Yes I would love the Code..



> > > > > Hi Rob,
> > > > > I believe the only way to do that is by
> > > > > using CDO. The CDO library is installed by default
> > > > > with Outlook 98 but you must explicitly choose
> > > > > to install it with Outlook 2000.

> > > > > I have code to do what you want, but it's at work.
> > > > > I'll post it tomorrow.

> > > > > Dan


> > > > > > Does anyone know how to create a Mail message in VBA with a
> > different
> > > > > > mailbox other then the user running the code.  I know how to
create
> > a
> > > > > > message with but it uses the users outlook to send it off.  I
would
> > > like
> > > > to
> > > > > > be able to send a message from one mailbox regardless who runs
the
> > > code.

> > > > > > Thanks
> > > > > > Rob.



Sun, 25 Jan 2004 06:16:25 GMT  
 Send Email from a certain Mailbox
LOL
I just figured it out myselft. with the ProfileInfo property

Regards
Rob.


Quote:
> Thanks Again for the Code.

> Do you know of away to get this to work with out calling a profile?  Can
we
> have it logon to the server from a specific username?

> Rob.



> > Hi Rob,
> > Well, the code for the class goes in a class module.
> > So you now have a clsMAPIEmail class (this is
> > what you must name your class) with properties
> > and methods you can use.

> > Now you have to instantiate an instance of
> > your class in order to use it. That's where those two
> > lines come in.

> > Of course you can just use some of the code directly
> > if you don't want to create a class.

> > Dan


> > > Hi Dan! Thanks again for the post...I think I have it figured it out
but
> > > what do these to variables do for the form code?

> > > Dim clMAPI As clsMAPIEmail
> > > Set clMAPI = New clsMAPIEmail

> > > Rob.



> > > > Hi Rob,
> > > > I've located my code, however it's
> > > > not as straight foreward as I thought.

> > > > I got a 'mapi class' somebody wrote and
> > > > used that. I'll post the code for the
> > > > class, which you can use. It's quite
> > > > convenient actually, but I don't know
> > > > how comfortable you'll be with this.

> > > > You'll see where I hard coded my mailbox
> > > > in the class module itself (MAPILogon sub). Simply
> > > > substitute your mailbox there.

> > > > I'll also post the code I used from the
> > > > form to send mail.

> > > > Here goes:

> > > > '**************** Class Start ***********************

> > > > Option Explicit

> > > > Private mobjSession As MAPI.Session
> > > > Private mobjMessage As Message
> > > > Private mboolErr As Boolean

> > > > Private mobjNewMessage As Message

> > > > Private Const mcERR_DOH = vbObjectError + 10000
> > > > Private Const mcERR_DECIMAL = 261144    'low word order +1000

> > > > Public Sub MAPIAddMessage()
> > > >     With mobjSession
> > > >         Set mobjNewMessage = .Outbox.Messages.Add
> > > >     End With
> > > > End Sub

> > > > Public Sub MAPIUpdateMessage()
> > > >     mobjNewMessage.Update
> > > > End Sub

> > > > Private Sub Class_Initialize()
> > > >     mboolErr = False
> > > > End Sub

> > > > Private Sub Class_Terminate()
> > > >     On Error Resume Next
> > > >     Set mobjMessage = Nothing
> > > >     mobjSession.Logoff
> > > >     Set mobjSession = Nothing
> > > > End Sub

> > > > Public Property Let MAPISetMessageBody(stBodyText As String)
> > > >     If Len(stBodyText) > 0 Then mobjNewMessage.Text = stBodyText
> > > > End Property

> > > > Public Property Let MAPISetMessageSubject(stSubject As String)
> > > >     If Len(stSubject) > 0 Then mobjNewMessage.Subject = stSubject
> > > > End Property

> > > > Public Property Get MAPIIsError() As Boolean
> > > >     MAPIIsError = mboolErr
> > > > End Property

> > > > Public Property Get MAPIRecipientCount() As Integer
> > > >     MAPIRecipientCount = mobjNewMessage.Recipients.Count
> > > > End Property

> > > > Public Sub MAPIAddAttachment(stFile As String, _
> > > >                         Optional stLabel As Variant)
> > > > Dim objAttachment As Attachment
> > > > Dim stMsg As String

> > > >     On Error GoTo Error_MAPIAddAttachment

> > > >     If mboolErr Then Err.Raise mcERR_DOH
> > > >     If Len(Dir(stFile)) = 0 Then Err.Raise mcERR_DOH + 10

> > > >     If IsMissing(stLabel) Then stLabel = CStr(stFile)

> > > >     With mobjNewMessage
> > > >         .Text = " " & mobjNewMessage.Text
> > > >         Set objAttachment = .Attachments.Add
> > > >         With objAttachment
> > > >             .Position = 0
> > > >             .Name = stLabel
> > > >             'no need to link a file me thinks
> > > >             .Type = CdoFileData
> > > >             .ReadFromFile stFile
> > > >         End With
> > > >         .Update
> > > >     End With

> > > > Exit_MAPIAddAttachment:
> > > >     Set objAttachment = Nothing
> > > >     Exit Sub
> > > > Error_MAPIAddAttachment:
> > > >     mboolErr = True
> > > >     If Err = mcERR_DOH + 10 Then
> > > >         stMsg = "Couldn't locate the file " & vbCrLf
> > > >         stMsg = stMsg & "'" & stFile & "'." & vbCrLf
> > > >         stMsg = stMsg & "Please check the file name and path and try
> > > again."
> > > >         MsgBox stMsg, vbExclamation + vbOKOnly, "File Not Found"
> > > >     ElseIf Err <> mcERR_DOH Then
> > > >         MsgBox "Error " & Err.Number & vbCrLf & Err.Description
> > > >     End If
> > > >     Resume Exit_MAPIAddAttachment
> > > > End Sub

> > > > Public Sub MAPIAddRecipient(stPerson As String, intAddressType As
> Integer)
> > > > Dim objNewRecipient As Recipient 'local

> > > >     On Error GoTo Error_MAPIAddRecipient

> > > >     If mboolErr Then Err.Raise mcERR_DOH

> > > >     'If there's no SMTP present in the stPerson var, then
> > > >     'we have to use Name, else Address
> > > >     With mobjNewMessage
> > > >         If InStr(1, stPerson, "SMTP:") > 0 Then
> > > >             Set objNewRecipient = .Recipients.Add(Address:=stPerson,
_

Type:=intAddressType)
Quote:
> > > >         Else
> > > >             Set objNewRecipient = .Recipients.Add(Name:=stPerson, _

Type:=intAddressType)

- Show quoted text -

Quote:
> > > >         End If
> > > >         objNewRecipient.Resolve
> > > >     End With

> > > > Exit_MAPIAddRecipient:
> > > >     Set objNewRecipient = Nothing
> > > >     Exit Sub

> > > > Error_MAPIAddRecipient:
> > > >     mboolErr = True
> > > >     Resume Exit_MAPIAddRecipient
> > > > End Sub

> > > > Public Sub MAPISendMessage(Optional boolSaveCopy As Variant, _
> > > >                             Optional boolShowDialog As Variant)

> > > >     If IsMissing(boolSaveCopy) Then
> > > >         boolSaveCopy = True
> > > >     End If
> > > >     If IsMissing(boolShowDialog) Then
> > > >         boolShowDialog = False
> > > >     End If
> > > >     mobjNewMessage.Send savecopy:=boolSaveCopy,
> showdialog:=boolShowDialog
> > > >     mobjSession.DeliverNow
> > > > End Sub

> > > > Public Sub MAPILogon()
> > > > On Error GoTo err_sMAPILogon
> > > > Const cERROR_USERCANCEL = -2147221229

> > > >     Set mobjSession = CreateObject("MAPI.Session")
> > > >     mobjSession.Logon "Dan Artuso", , False

> > > > exit_sMAPILogon:
> > > >     Exit Sub

> > > > err_sMAPILogon:
> > > >     mboolErr = True
> > > >     If Err = CdoE_LOGON_FAILED - mcERR_DECIMAL Then
> > > >         MsgBox "Logon Failed", vbCritical + vbOKOnly, "Error"
> > > >     ElseIf Err = cERROR_USERCANCEL Then
> > > >         MsgBox "Aborting since you pressed cancel.", _
> > > >                 vbOKOnly + vbInformation, "Operation Cancelled!"
> > > >     Else
> > > >         MsgBox "Error number " & Err - mcERR_DECIMAL & "
description.
> " _
> > > >                 & Error$(Err)
> > > >     End If
> > > >     Resume exit_sMAPILogon
> > > > End Sub

> > > > Public Sub MAPILogoff()
> > > > On Error GoTo err_sMAPILogoff

> > > >     mobjSession.Logoff

> > > >     Set mobjNewMessage = Nothing
> > > >     Set mobjSession = Nothing

> > > > exit_sMAPILogoff:
> > > >     Exit Sub

> > > > err_sMAPILogoff:
> > > >     Resume exit_sMAPILogoff
> > > > End Sub
> > > > '**************** Class End  ***********************

> > > > Form code:

> > > >  'get the people who need reminders sent to them
> > > >  rsEmailRem.Open strSqlEmailRem, cn, adOpenStatic

> > > >  Dim clMAPI As clsMAPIEmail
> > > >  Set clMAPI = New clsMAPIEmail

> > > >  'send email out for each record
> > > >  Do While Not rsEmailRem.EOF
> > > >     With clMAPI
> > > >          .MAPILogon
> > > >          .MAPIAddMessage
> > > >          .MAPISetMessageBody = rsPeriod!Message
> > > >          .MAPISetMessageSubject = rsPeriod!Subject & " " &
> > > > rsEmailRem!FileName
> > > >          .MAPIAddRecipient rsEmailRem!Email, 1
> > > >          .MAPIUpdateMessage
> > > >          .MAPISendMessage False
> > > >          .MAPILogoff
> > > >      End With
> > > >      rsEmailRem.MoveNext
> > > >  Loop

> > > > If you can't make heads or tails of this, post back.

> > > > HTH
> > > > Dan



> > > > > Hey Thanks Dan!!  Yes I would love the Code..



> > > > > > Hi Rob,
> > > > > > I believe the only way to do that is by
> > > > > > using CDO. The CDO library is installed by default
> > > > > > with Outlook 98 but you must explicitly choose
> > > > > > to install it with Outlook 2000.

> > > > > > I have code to do what you want, but it's at work.
> > > > > > I'll post it tomorrow.

> > > > > > Dan


> > > > > > > Does anyone know how to create a Mail message in VBA with a
> > > different
> > > > > > > mailbox other then the user running the code.  I know how to
> create
> > > a
> > > > > > > message with but it uses the users outlook to send it off.  I
> would
> > > > like
> > > > > to
> > > > > > > be able to send a message from one mailbox regardless who runs
> the
> > > > code.

> > > > > > > Thanks
> > > > > > > Rob.



Sun, 25 Jan 2004 06:39:17 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Send Email over a certain Account

2. Send Email over a certain Account

3. Send an Email with Attachment at a Certain Time

4. reading mailbox for certain message

5. Moving an email to another mailbox.

6. Sending email to more then one email

7. Send a email to email-address with netscape ???

8. Send email from Acc97 to Lotus notes email.

9. VBA Send Email with specific Email Account

10. sending customized emails to emails in an excel spreadsheet

11. Same Email sent to multiple email accounts

12. Sending email to email address rather than that in the To: window

 

 
Powered by phpBB® Forum Software