Send an email with attachment through default email client

B

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
 
A

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.
 
R

Rad [Visual C# MVP]

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/
 
R

Rad [Visual C# MVP]

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.
 
B

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top