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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

default email client \w attachment

Status
Not open for further replies.

hinchdog

Programmer
Feb 14, 2001
380
US
is there a way to detect the default email client on a user's machine and then open that client with an attachment?
 
Hi,

To open a default client, you can use the ShellExecute funciton which recognises email addresses if they are prefixed with mailto: and run the default program.


Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
ByVal lpParameters As String, ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long


' Open the default program for sending email messages
' Returns True if successful, False otherwise



Public Function OpenEmailProgram(sDest As String, _
Optional sSubject As String, _
Optional sBody As String, _
Optional sCC As String, _
Optional sBCC As String)
Dim res As Long
res = ShellExecute 0, vbNullString, _
"mailto:" & sDest & _
"?subject=" & sSubject & _
"&body=" & sBody & _
"&CC=" & sCC & "&BCC=" & _
sBCC, 0&, 0&, 1
OpenEmailProgram = (res > 32)
End Function


NB!! Before to check the return result in case no e-mail program is 'defaulted'


If Not OpenEmailProgram("user@domain.com", _
"SendMail Test", "The function works!!!", _
"annother@domain.com, phathi@domain.com") Then
MsgBox "Unable to run the email program"
End If


Try it out.

Phathi >:):O>
 
Sorry,

A correction in the OpenEmailProgram Function.



Public Function OpenEmailProgram _
ByVal EmailAddress As String) _
As Boolean
Dim res As Long
res = ShellExecute(0&, "open", "mailto:" & EmailAddress, vbNullString, _
vbNullString, vbNormalFocus)
OpenEmailProgram = (res > 32)
End Function



so you just send the emailaddress.

OpenEmailProgram("phathi@domain.com")

oOps X-)
 
this is very nice, thank you. but how do i send an email attachment?
 
Okay,

Nowe that I've told you what you don't want to hear, here's something a bit closer to your reality.



Sub SendEmail(From As String, SendTo As String, Subject As String, _
EmailText As String, Optional AttachmentPath As String, _
Optional Attachment As String, Optional CC As String)
Const constRoutine As String = "SendEmail"

Dim strSendTo As String
Dim objSendMail As CDONTS.NewMail
Dim i As Integer

On Error GoTo TryMAPI

If SendTo = "" Then Exit Sub

Set objSendMail = New CDONTS.NewMail

With objSendMail
On Error Resume Next
.From = From
If CC <> &quot;&quot; Then
.CC = CC
End If

On Error GoTo ErrorHandler
.To = SendTo
.Subject = Subject
.Body = EmailText
AttachmentPath = Trim$(AttachmentPath)

If AttachmentPath <> &quot;&quot; Then
If Right$(AttachmentPath, 1) <> &quot;\&quot; Then
AttachmentPath = AttachmentPath & &quot;\&quot;
End If
.AttachFile (AttachmentPath & Attachment)
End If
.Send
End With

Exit Sub

TryMAPI:
On Error GoTo ErrorHandler

'If CDO fails, try MAPI
If CC <> &quot;&quot; Then
strSendTo = SendTo & &quot;; &quot; & CC
Else
strSendTo = SendTo
End If
End Sub



Remember to reference CDO

Ciao
Phathi >:):O>
 
thanks, but will this work behind a firewall? because that's the whole reason i wanted to open up the default email client.

-frank
 
also, with the CDO example you provided, this does not work with CDO 1.2 which is what i have. thanks anyway

-hinchdog
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top