Sending Email using default email application

  • Thread starter Thread starter Dino M. Buljubasic
  • Start date Start date
D

Dino M. Buljubasic

How can I detect default email application on user machine and use it to
send an email from my vb.net application?

Any help will be gratefully appreciated
Dino
 
Hi Dino,

The Process class has a Start method to which you can give a "mailto:"
command. With this you can specify the recipient, subject and body. There are
some limitations as to what characters you can allow - no '?'s, for instance!

Do you want to be able to open up the Mail program so that the User can
type the message or do you want to fill everything in yourself so that the
User just has to edit, if required, then hit the Send button. Do you just want
to send a message and not have the User do anything?

Example of how to use the code below:

StartDefaultMail ("TheBoss", "Re: My project bonus", "$$$ Thanks!! -
Dino")

Regards,
Fergus

<code>
Public Sub StartDefaultMail (sTo As String, _
Optional sSubject As String = "", _
Optional sMessage As String = "")
Try
Process.Start ("mailto:" & sTo & "?subject=" _
& sSubject & "&body=" & sMessage)

Catch e As Exception
MsgBox ("Couldn't start default email application" _
& vbCrLf & e.Message)
'or
Throw New Exception ("Couldn't start default email application", e)
End Try
End Sub
</code>
 
fergus,

can't you url encode the subject and body so that spaces become %20 and ?'s
are translated as well? haven't tried it before but i think that maybe a fix
for the limitation.

fft,

steve
 
Hi Steve,

When I was first experimenting with that little routine, I discovered the
problem with ':', '?' and '&' (space wasn't a problem - just the delimiters).
My first thought was to use UrlEncode but when I tried it, it didn't work and
I just got a url-encoded strings in my email.

Now you're saying that url-encoding could be a solution but you haven't
tested it!

Well, I could say all the above and leave it at that - but, no, I can't. I
have to prove it. So I did it again and it worked perfectly! I must have done
something wrong the first time round.

So, thanks, and yes, url-encoding <is> the way to go. :-)

Regards,
Fergus

<code>
'A reference to System.Web may be necessary
'in the Project for this Import to work.
Imports System.Web.HttpUtility

Public Sub StartDefaultMail (sTo As String, _
Optional sSubject As String = "", _
Optional sMessage As String = "")
Try
sTo = UrlEncode (sTo)
sSubject = UrlEncode (sSubject)
sMessage = UrlEncode (sMessage)
Process.Start ("mailto:" & sTo & "?subject=" _
& sSubject & "&body=" & sMessage)

Catch e As Exception
MsgBox ("Couldn't start default email application" _
& vbCrLf & e.Message)
'or
Throw New Exception ("Couldn't start default email application",
e)
End Try
End Sub
</code>
 
cool!

i wasn't trying to put you on the spot...i have worked w/ encoding spaces
before but not w/ other "lively" characters. it sounded like you had. i'm
glad i correctly theorized.

i learn something new every day.

cheers.
 
Hi Jay,

LOL. I should have remembered! ;-)

Something about 'waving' goodbye to your mail, boom, boom!
[Groans allowed - permission to kick granted]

Regards,
Fergus
 
No, I'd like to open up the mail program and enter the subject line and the
message by myself into it. The address To and From would be fetched from a
database.

Thanks Fergus :)
 
Hi Cor,

I'm monitoring the ones that Cindy took on, so that if the OP adds
anything, I can apologise for her absence (or <maybe> help). I didn't
(couldn't) promise to take on new questions. I'm very selective about which
Office queries I answer - because I'm no expert.

Regards,
Fergus
 
Hi Fergus,

Take a look on my comment then, and answer it here.
It is Austin, I want to know what you find of it.

:-)

Cor
 
Hi Fergus
I seen you answered it,

But maybe you don't see it, I dont know how you pronounce your name,
but maybe I had better written Fer-gurus.

Sounds nice the iron guru
:-)
Cor
 
Hi Cor,

ROFL. That's clever. ;-))

But maybe I should have been called Augus because I <love> GOLD!!

I like the sound of Iron Guru too, and Gold Guru? - great!! ROFL.

Glowing regards,
GG
 
Back
Top