Connecting Tech Pros Worldwide Forums | Help | Site Map

Send an email with attachment through default email client

Ben K
Guest
 
Posts: n/a
#1: Apr 13 '07
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


atlaste
Guest
 
Posts: n/a
#2: Apr 13 '07

re: Send an email with attachment through default email client


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:
Quote:
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

Rad [Visual C# MVP]
Guest
 
Posts: n/a
#3: Apr 14 '07

re: Send an email with attachment through default email client


On 13 Apr 2007 00:46:47 -0700, Ben K wrote:
Quote:
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
Ben K
Guest
 
Posts: n/a
#4: Apr 16 '07

re: Send an email with attachment through default email client


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:
Quote:
On 13 Apr 2007 00:46:47 -0700, Ben K wrote:
>
>
>
Quote:
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
>
Quote:
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.
>
Quote:
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" )
>
Quote:
' seems a bug in the Guid method requires us to clip the returned
string
strGuid = Left(CreateObject("Scriptlet.TypeLib").Guid, 38)
>
Quote:
Set oWShell = CreateObject("WScript.Shell")
archive = fso.BuildPath(oWShell.ExpandEnvironmentStrings("%T EMP%"), _
"MyEncryptor_" & strGuid & ".fta" )
>
Quote:
cmdline = "MyEncryptor.exe -r -f " & archive & " -o " & """" &
WScript.Arguments(0) & """"
>
Quote:
oWShell.Run cmdline,,True
>
Quote:
Set ie=CreateObject("InternetExplorer.Application")
>
Quote:
ie.Navigate "c:\"
Do While ie.Busy Or ie.ReadyState<>4
WScript.Sleep 100
Loop
>
Quote:
Const ssfSENDTO=9 'ShellSpecialFolderConstants.ssfSENDTO
Const ssfDESKTOP=0 ' ShellSpecialFolderConstants.ssfDESKTOP
ie.Document.Application.NameSpace(ssfDESKTOP).Item s().Item(archive).InvokeVerb
"copy"
>
Quote:
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
>
Quote:
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

Rad [Visual C# MVP]
Guest
 
Posts: n/a
#5: Apr 16 '07

re: Send an email with attachment through default email client


On 16 Apr 2007 01:07:54 -0700, Ben K wrote:
Quote:
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
Ben K
Guest
 
Posts: n/a
#6: Apr 16 '07

re: Send an email with attachment through default email client


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:
Quote:
On 16 Apr 2007 01:07:54 -0700, Ben K wrote:
>
Quote:
Rad,
>
Quote:
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?
>
Quote:
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

Closed Thread