Home All Groups Group Topic Archive Search About

Send an email with attachment through default email client

Author
13 Apr 2007 7:46 AM
Ben K
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("%TEMP%"), _
                        "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).Items().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

Author
13 Apr 2007 10:16 AM
atlaste
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.

Show quoteHide quote
On Apr 13, 9:46 am, "Ben K" <benk***@hotmail.com> 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("%TEMP%"), _
>                         "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).Items().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
Are all your drivers up to date? click for free checkup

Author
14 Apr 2007 7:15 AM
Rad [Visual C# MVP]
On 13 Apr 2007 00:46:47 -0700, Ben K wrote:

Show quoteHide 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("%TEMP%"), _
>                         "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).Items().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/02/20/neat-enail-trick/
Author
16 Apr 2007 8:07 AM
Ben K
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

Show quoteHide quote
On Apr 14, 12:15 am, "Rad [Visual C# MVP]" <nos...@nospam.com> wrote:
> 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("%TEMP%"), _
> >                         "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).Items().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/02/20/neat-enail-trick/
> --
> Bits.Byteshttp://bytes.thinkersroom.com
Author
16 Apr 2007 6:54 PM
Rad [Visual C# MVP]
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.
Author
16 Apr 2007 9:20 PM
Ben K
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

Show quoteHide quote
On Apr 16, 11:54 am, "Rad [Visual C# MVP]" <nos...@nospam.com> wrote:
> 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

Bookmark and Share