Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / Win API / May 2007



Tip: Looking for answers? Try searching our database.

Sending Email to freehand address

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JonWayn - 21 May 2007 08:57 GMT
Is it possible to send an email, with Outlook installed, to an address that
is not stored in your addressbook? You can with the user-interface, so I dont
see why there wouldnt be a way to emulate that behaviour in programming.

Thanks for any input
MikeD - 22 May 2007 07:00 GMT
> Is it possible to send an email, with Outlook installed, to an address
> that
> is not stored in your addressbook? You can with the user-interface, so I
> dont
> see why there wouldnt be a way to emulate that behaviour in programming.

Yeah, but it's not going to be pretty. The code's not bad. In fact, it's
very simple. What's ugly is that Outlook is going to prompt the user for
permission to send the email (MS's way of stopping spam). Now if that's OK
for you (and more so, your users), then you merely need to use Automation
with Outlook to do it.

Here's something I wrote from quite a while ago (I don't claim it's the
greatest example):

-----
Public Function SendOutlookMail(ByVal sTo As String, sSubject As String,
sBody As String) As Boolean

   On Error GoTo EH

   Dim oOLApp              As Object
   Dim oOLMailItem         As Object
   Dim lPosition           As Long
   Dim sErrMsg             As String

   sErrMsg = "Unable to find Microsoft Outlook"
   Set oOLApp = CreateObject("Outlook.Application")

   sErrMsg = "Unable to create message in Outlook"
   Set oOLMailItem = oOLApp.CreateItem(olMailItem)

   oOLMailItem.Subject = sSubject

   sErrMsg = "Unable to interpret email address(es): " & sTo
   Do
       lPosition = InStr(1, sTo, ",")
       If lPosition = 0 Then
           oOLMailItem.Recipients.Add sTo
       Else
           oOLMailItem.Recipients.Add Mid$(sTo, 1, lPosition - 1)
           sTo = Right$(sTo, Len(sTo) - lPosition)
       End If
   Loop Until lPosition = 0

   oOLMailItem.Body = sBody

   sErrMsg = "Unable to send message"
   oOLMailItem.Send
   oOLMailItem.Display

   SendOutlookMail = True
   Exit Function

EH:
   MsgBox sErrMsg & vbCrLf & "(" & Err.Number & ": " & Err.Description &
")"
   SendOutlookMail = False

End Function
-----

Note that this both sends AND displays (for the sender) the email. Normally,
you'd really only want to use the Send method OR the Display method, not
both.  Also note that multiple recipients should be separated by commas (or
change the code to use whatever separater character(s) you want).

For more detailed information, consult Outlook's Help on its object model.
I believe this is part of the VBA Help in Outlook.

You also might want to consider alternatives that don't rely on Outlook. One
possibility would be CDO, which is part of Windows 2000 and greater. Using
CDO, you can send email vai the user's SMTP server and this also bypasses
Outlook's "security" (so users won't be prompted for their permission to
send the email). Plus, and maybe this is irrelevant for you in this
particular case, your program's not dependent on Outlook being installed. If
you want help with CDO, just search the VB-related newsgroups at google on
"CDO".  You'll find tons of example code (and some messages regarding
problems, as always).

Signature

Mike
Microsoft Visual Basic MVP

JonWayn - 23 May 2007 08:32 GMT
I have actually used CDO before. What I dislike about it is that it doesnt
place the sent mail item in the Send Items box of your default (or any, for
that matter) email client.

Has anyone figured out a way to automate sending emails with OE? I
personally have Outlook 2003 installed, however, another computer that I am
developing a message-sending solution for does not - it has OE instead. I
thought all along that MapiSendMail would use whatever default email client
was enabled. Not so. It chokes when it encounters OE. And even on my
computer, using that method, that was where I seemed to be restricted to name
resolution prior to sending.

> > Is it possible to send an email, with Outlook installed, to an address
> > that
[quoted text clipped - 75 lines]
> "CDO".  You'll find tons of example code (and some messages regarding
> problems, as always).
MikeD - 24 May 2007 06:19 GMT
>I have actually used CDO before. What I dislike about it is that it doesnt
> place the sent mail item in the Send Items box of your default (or any,
> for
> that matter) email client.

That is correct. CDO doesn't rely on an email client program.

> Has anyone figured out a way to automate sending emails with OE? I
> personally have Outlook 2003 installed, however, another computer that I
[quoted text clipped - 6 lines]
> name
> resolution prior to sending.

If you're going to rely on an email client program, you're going to have to
write code to support each of them. OE does not support automation, however,
it IS MAPI-complient. So I don't know why sending emails via MAPI should
fail with OE as the email client. Just for grins and giggles, try the sample
VBMail program included with VB (it uses the MAPI controls).

Signature

Mike
Microsoft Visual Basic MVP

JonWayn - 25 May 2007 08:45 GMT
I write all my codes using VBA. It doesn't have the feature that u r talking
about...

> >I have actually used CDO before. What I dislike about it is that it doesnt
> > place the sent mail item in the Send Items box of your default (or any,
[quoted text clipped - 19 lines]
> fail with OE as the email client. Just for grins and giggles, try the sample
> VBMail program included with VB (it uses the MAPI controls).
MikeD - 25 May 2007 16:52 GMT
>I write all my codes using VBA. It doesn't have the feature that u r
>talking
> about...

Then you really should be asking in a VBA programming newsgroup. Posting
here, it's assumed you're using VB6 or under. And just as in this case, you
may get non-relevant answers or suggestions.

Signature

Mike
Microsoft Visual Basic MVP

>> >I have actually used CDO before. What I dislike about it is that it
>> >doesnt
[quoted text clipped - 26 lines]
>> sample
>> VBMail program included with VB (it uses the MAPI controls).
msnews - 25 May 2007 17:59 GMT
For another OE hack, see:

http://vbnet.mvps.org/index.html?code/internet/shellexecutelargeemail.htm

>I have actually used CDO before. What I dislike about it is that it doesnt
> place the sent mail item in the Send Items box of your default (or any,
[quoted text clipped - 101 lines]
>> "CDO".  You'll find tons of example code (and some messages regarding
>> problems, as always).
Tony Proctor - 25 May 2007 17:05 GMT
If you don't mind seeing the default mail client, you could just try the
mailto: protocol, e.g.

Public Sub SendEmail(hWnd As Long, sEmail As String, sSubject As String,
sBody As String)
' Sends an email message using the curent default email client
Dim sUrl As String

   sUrl = "mailto:" & sEmail & "?subject=" & sURLEscape(sSubject) & _
       "&body=" & sURLEscape(sBody)
   ShellExecute hWnd, vbNullString, sUrl, vbNullString, App.Path,
SW_SHOWNORMAL
End Sub

That's about as simple as it gets. Everything else is much more difficult
:-)

   Tony Proctor

> Is it possible to send an email, with Outlook installed, to an address that
> is not stored in your addressbook? You can with the user-interface, so I dont
> see why there wouldnt be a way to emulate that behaviour in programming.
>
> Thanks for any input
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.