473,322 Members | 1,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Send an email with attachment through default email client

I saw some posting some time ago regarding a "trick" to automatically
pop up an email editor with attachment using the default mail client.
It is
basically using automation to do the following steps

1. Using Explorer and copy a file, "C:\temp\attachment.txt"
2. Locate the item of type "MAPIMAIL File" in "SendTo" in Explorer
(i.e. the default mail client program)
3. Paste the file to the "MAPIMAIL File" item in "SendTo". This will
cause a mail editor pops up with with the file copied in Step 1
as its attachment.

How do I create C# automation that emulates the manual steps above?
Below is the code in VB Script I got that does that.
================================================== =
Option Explicit
Dim fso, oWShell, ie, oMapiMailItems, item
Dim strGuid, archive, cmdline
Set fso = CreateObject( "Scripting.FileSystemObject" )

' seems a bug in the Guid method requires us to clip the returned
string
strGuid = Left(CreateObject("Scriptlet.TypeLib").Guid, 38)

Set oWShell = CreateObject("WScript.Shell")
archive = fso.BuildPath(oWShell.ExpandEnvironmentStrings("%T EMP%"), _
"MyEncryptor_" & strGuid & ".fta" )

cmdline = "MyEncryptor.exe -r -f " & archive & " -o " & """" &
WScript.Arguments(0) & """"

oWShell.Run cmdline,,True

Set ie=CreateObject("InternetExplorer.Application")

ie.Navigate "c:\"
Do While ie.Busy Or ie.ReadyState<>4
WScript.Sleep 100
Loop

Const ssfSENDTO=9 'ShellSpecialFolderConstants.ssfSENDTO
Const ssfDESKTOP=0 ' ShellSpecialFolderConstants.ssfDESKTOP
ie.Document.Application.NameSpace(ssfDESKTOP).Item s().Item(archive).InvokeVerb
"copy"

Set oMapiMailItems =
ie.Document.Application.NameSpace(ssfSENDTO).Items
For Each item in oMapiMailItems
If item.Type = "MAPIMAIL File" Then
item.InvokeVerb "paste"
Exit For
End If
Set item = Nothing
Next

Set oMapiMailItems = Nothing
ie.Quit
Set ie = Nothing
WScript.Quit
Set WScript = Nothing
Set oWShell = Nothing
Set fso = Nothing

Apr 13 '07 #1
5 13289
Hmm this code looks quite scary...

You seem to be looking for "System.diagnostics.runas", IE from a COM
reference and "System.Environment" - and then some Specialfolder...
Perhaps System.io.Path / File for temporary files... so on

But perhaps it is also an idea to just use the Outlook API (reference
the outlook com component) to create a mail with an attachment...

Cheers,
Stefan.

On Apr 13, 9:46 am, "Ben K" <benk...@hotmail.comwrote:
I saw some posting some time ago regarding a "trick" to automatically
pop up an email editor with attachment using the default mail client.
It is
basically using automation to do the following steps

1. Using Explorer and copy a file, "C:\temp\attachment.txt"
2. Locate the item of type "MAPIMAIL File" in "SendTo" in Explorer
(i.e. the default mail client program)
3. Paste the file to the "MAPIMAIL File" item in "SendTo". This will
cause a mail editor pops up with with the file copied in Step 1
as its attachment.

How do I create C# automation that emulates the manual steps above?
Below is the code in VB Script I got that does that.
================================================== =
Option Explicit
Dim fso, oWShell, ie, oMapiMailItems, item
Dim strGuid, archive, cmdline
Set fso = CreateObject( "Scripting.FileSystemObject" )

' seems a bug in the Guid method requires us to clip the returned
string
strGuid = Left(CreateObject("Scriptlet.TypeLib").Guid, 38)

Set oWShell = CreateObject("WScript.Shell")
archive = fso.BuildPath(oWShell.ExpandEnvironmentStrings("%T EMP%"), _
"MyEncryptor_" & strGuid & ".fta" )

cmdline = "MyEncryptor.exe -r -f " & archive & " -o " & """" &
WScript.Arguments(0) & """"

oWShell.Run cmdline,,True

Set ie=CreateObject("InternetExplorer.Application")

ie.Navigate "c:\"
Do While ie.Busy Or ie.ReadyState<>4
WScript.Sleep 100
Loop

Const ssfSENDTO=9 'ShellSpecialFolderConstants.ssfSENDTO
Const ssfDESKTOP=0 ' ShellSpecialFolderConstants.ssfDESKTOP
ie.Document.Application.NameSpace(ssfDESKTOP).Item s().Item(archive).InvokeVerb
"copy"

Set oMapiMailItems =
ie.Document.Application.NameSpace(ssfSENDTO).Items
For Each item in oMapiMailItems
If item.Type = "MAPIMAIL File" Then
item.InvokeVerb "paste"
Exit For
End If
Set item = Nothing
Next

Set oMapiMailItems = Nothing
ie.Quit
Set ie = Nothing
WScript.Quit
Set WScript = Nothing
Set oWShell = Nothing
Set fso = Nothing

Apr 13 '07 #2
On 13 Apr 2007 00:46:47 -0700, Ben K wrote:
I saw some posting some time ago regarding a "trick" to automatically
pop up an email editor with attachment using the default mail client.
It is
basically using automation to do the following steps

1. Using Explorer and copy a file, "C:\temp\attachment.txt"
2. Locate the item of type "MAPIMAIL File" in "SendTo" in Explorer
(i.e. the default mail client program)
3. Paste the file to the "MAPIMAIL File" item in "SendTo". This will
cause a mail editor pops up with with the file copied in Step 1
as its attachment.

How do I create C# automation that emulates the manual steps above?
Below is the code in VB Script I got that does that.
================================================== =
Option Explicit
Dim fso, oWShell, ie, oMapiMailItems, item
Dim strGuid, archive, cmdline
Set fso = CreateObject( "Scripting.FileSystemObject" )

' seems a bug in the Guid method requires us to clip the returned
string
strGuid = Left(CreateObject("Scriptlet.TypeLib").Guid, 38)

Set oWShell = CreateObject("WScript.Shell")
archive = fso.BuildPath(oWShell.ExpandEnvironmentStrings("%T EMP%"), _
"MyEncryptor_" & strGuid & ".fta" )

cmdline = "MyEncryptor.exe -r -f " & archive & " -o " & """" &
WScript.Arguments(0) & """"

oWShell.Run cmdline,,True

Set ie=CreateObject("InternetExplorer.Application")

ie.Navigate "c:\"
Do While ie.Busy Or ie.ReadyState<>4
WScript.Sleep 100
Loop

Const ssfSENDTO=9 'ShellSpecialFolderConstants.ssfSENDTO
Const ssfDESKTOP=0 ' ShellSpecialFolderConstants.ssfDESKTOP
ie.Document.Application.NameSpace(ssfDESKTOP).Item s().Item(archive).InvokeVerb
"copy"

Set oMapiMailItems =
ie.Document.Application.NameSpace(ssfSENDTO).Items
For Each item in oMapiMailItems
If item.Type = "MAPIMAIL File" Then
item.InvokeVerb "paste"
Exit For
End If
Set item = Nothing
Next

Set oMapiMailItems = Nothing
ie.Quit
Set ie = Nothing
WScript.Quit
Set WScript = Nothing
Set oWShell = Nothing
Set fso = Nothing
You can use Process.Start with the mailto: keyword. It also supports
attachments

http://thinkersroom.com/bytes/2007/0...t-enail-trick/
--
Bits.Bytes
http://bytes.thinkersroom.com
Apr 14 '07 #3
Rad,

Thanks for replying. However, I found another link (http://
www.thescripts.com/forum/thread351279.html) saying that "mailto:" does
not support attachment. Is it a new interface that I can use to add
attachment?

Best regards,

Ben

On Apr 14, 12:15 am, "Rad [Visual C# MVP]" <nos...@nospam.comwrote:
On 13 Apr 2007 00:46:47 -0700, Ben K wrote:
I saw some posting some time ago regarding a "trick" to automatically
pop up an email editor with attachment using the default mail client.
It is
basically using automation to do the following steps
1. Using Explorer and copy a file, "C:\temp\attachment.txt"
2. Locate the item of type "MAPIMAIL File" in "SendTo" in Explorer
(i.e. the default mail client program)
3. Paste the file to the "MAPIMAIL File" item in "SendTo". This will
cause a mail editor pops up with with the file copied in Step 1
as its attachment.
How do I create C# automation that emulates the manual steps above?
Below is the code in VB Script I got that does that.
================================================== =
Option Explicit
Dim fso, oWShell, ie, oMapiMailItems, item
Dim strGuid, archive, cmdline
Set fso = CreateObject( "Scripting.FileSystemObject" )
' seems a bug in the Guid method requires us to clip the returned
string
strGuid = Left(CreateObject("Scriptlet.TypeLib").Guid, 38)
Set oWShell = CreateObject("WScript.Shell")
archive = fso.BuildPath(oWShell.ExpandEnvironmentStrings("%T EMP%"), _
"MyEncryptor_" & strGuid & ".fta" )
cmdline = "MyEncryptor.exe -r -f " & archive & " -o " & """" &
WScript.Arguments(0) & """"
oWShell.Run cmdline,,True
Set ie=CreateObject("InternetExplorer.Application")
ie.Navigate "c:\"
Do While ie.Busy Or ie.ReadyState<>4
WScript.Sleep 100
Loop
Const ssfSENDTO=9 'ShellSpecialFolderConstants.ssfSENDTO
Const ssfDESKTOP=0 ' ShellSpecialFolderConstants.ssfDESKTOP
ie.Document.Application.NameSpace(ssfDESKTOP).Item s().Item(archive).InvokeVerb
"copy"
Set oMapiMailItems =
ie.Document.Application.NameSpace(ssfSENDTO).Items
For Each item in oMapiMailItems
If item.Type = "MAPIMAIL File" Then
item.InvokeVerb "paste"
Exit For
End If
Set item = Nothing
Next
Set oMapiMailItems = Nothing
ie.Quit
Set ie = Nothing
WScript.Quit
Set WScript = Nothing
Set oWShell = Nothing
Set fso = Nothing

You can use Process.Start with the mailto: keyword. It also supports
attachments

http://thinkersroom.com/bytes/2007/0...t-enail-trick/
--
Bits.Byteshttp://bytes.thinkersroom.com

Apr 16 '07 #4
On 16 Apr 2007 01:07:54 -0700, Ben K wrote:
Rad,

Thanks for replying. However, I found another link (http://
www.thescripts.com/forum/thread351279.html) saying that "mailto:" does
not support attachment. Is it a new interface that I can use to add
attachment?

Best regards,
Yes, you're right. Mailto does not support attachments. I could have sworn
that it did but digging around has convinced me otherwise.
--
Bits.Bytes
http://bytes.thinkersroom.com
Apr 16 '07 #5
Rad,

So, is there anyway I can simulate the 3 steps in my original posting
using C# automation?

1. Using Explorer and copy a file, "C:\temp\attachment.txt"
2. Locate the item of type "MAPIMAIL File" in "SendTo" in Explorer
(i.e. the default mail client program)
3. Paste the file to the "MAPIMAIL File" item in "SendTo". This will
cause a mail editor pops up with with the file copied in Step 1
as its attachment.

Ben

On Apr 16, 11:54 am, "Rad [Visual C# MVP]" <nos...@nospam.comwrote:
On 16 Apr 2007 01:07:54 -0700, Ben K wrote:
Rad,
Thanks for replying. However, I found another link (http://
www.thescripts.com/forum/thread351279.html) saying that "mailto:" does
not support attachment. Is it a new interface that I can use to add
attachment?
Best regards,

Yes, you're right. Mailto does not support attachments. I could have sworn
that it did but digging around has convinced me otherwise.
--
Bits.Byteshttp://bytes.thinkersroom.com

Apr 16 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: runningdog | last post by:
Hi Can someone point me to some documentation on how to create and send email from a win forms app using the defalt mail client TIA Steve
88
by: Mike | last post by:
Is there a way to determine what a user's default email client is? I read a post from 3 years ago that said no. I guess I'm hoping something has come along since then.
3
by: Ed Sutton | last post by:
How can I show the email client UI so the user can send the attachment to the desired recipient. I get an exception if I leave the MailMessage.To property blank. Is there any way to do this? I...
7
by: Darin | last post by:
Currently, our software is creating an email message using the Outlook DLL to send emails out that have PDF attachments. I want to stop forcing the customer to use outlook to send email. So, I...
2
by: Landley | last post by:
Hello, Is there a way of creating an email, attaching a file and sending using the client's default mail client? I am looking for a none email client specific solution that does not involve...
2
by: KevinK | last post by:
Hi, Is there a way to let my Win forms application open my default mail client to send an email that the user still can modify? Thanks Kevin
2
by: Hanika | last post by:
Going on 3 days and I am wodnering if I am just not looking in the right place. If anyone nows how to do this please please help me. I am developing a Windows form application using VB.NET 2005....
2
by: anshukher | last post by:
hi, i am making a windows application in C# and in that when i click on the send button the default mail client should open along with the attachment that i have browsed already! i have tried to...
2
beacon
by: beacon | last post by:
I have a DAP that I've added a command button to (and by default is set to be coded using vbscript, but I'll use whatever works). The command button uses the onclick method to look at a dropdown...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.