Start default mailprogram with attachment

Hello, I would like to start the user's defaul mailprogram with an attachment. I have tried something like this:

string toEmail = "test@test.com";
string subject = "Testar";
string body = "test";

string message = string.Format("mailto:{0} subject={1}&body={2}&attach={3}", "", subject, body, @"c:\test.txt");

Process myProcess = new Process();
myProcess.StartInfo.FileName = message;
myProcess.Start();
myProcess.Dispose();

 


Everythin looks fine but the attachment is not there... If it's not possible to have an attachemnt in this way. Can someone please give me an direction how to start the user's default mailprogram with an attachment in it.

Best regards



Answer this question

Start default mailprogram with attachment

  • bmellow

    sorry, the link included the closing bracket - the correct link is http://ftp.ics.uci.edu/pub/ietf/uri/rfc2368.txt 

    Also the mailto: link I've given is not rendering correctly in this richtextbox as I surrounded it by quotes.

    Also, if this is a winform app you're using, and you know the mail client, then I'd recommend that you use com-interop to outlook, as you can then use the object model to do what you like (and also as the mailto: is really a bit of an unsupported hack). Theres a good article here that should get you started.

    Cathal


  • womalley

    Private Sub OutLookMailto(ByVal status As String)

    Dim mytbox As New TextBox

    Dim strBuild As New StringBuilder

    Dim eTo, eFrom, eCC, eSub, eBody, eAttach As String

    eTo = pTo.TrimEnd

    eFrom = pFrom.TrimEnd

    eCC = pCC.TrimEnd

    eSub = pSub.TrimEnd

    eBody = pBody.TrimEnd

    If lblHidden.Text <> "" Then

    'build attachments

    strBuild.Append("&attach=")

    strBuild.Append("""")

    strBuild.Append("""" & lblHidden.Text & """")

    eAttach = EncodeUnwanted(strBuild.ToString) 'breaking

    eAttach = strBuild.ToString

    'eAttach = "" 'testing

    End If

    'would be nice to bring up outlook here

    Dim emailstr As String = "mailto:" & eTo & " subject=" & eSub & "&cc=" & eCC _

    & "&body=" & eBody & eAttach

    HLinkLetter.NavigateUrl = emailstr

    End Sub

    This sample of course is not complete i do some extensive urlencoding and formating of the text...however i thought you could use it to get started. The weird thing is the attach sometimes causes the error message "The command line argument is not valid. Verify the switch you are using." - driving me crazy! works one day then later it wont for the same exact mailto string. My question echos yours whats the preferred ms way of creating a default mail program client with attachements....as well as everything else. This is all in a VB .NET 2003 web application.

    Whats the preferred ms way of starting the default mail client if mailto: is a hack


  • RaguV

    Well, my default is outlook but for all other users i do not know.
  • Gnugs

    First things first, mailto: does not support attachments according to the RFC, therefore support for it is mixed. Some mail clients only support being passed the fields specified in the RFC.

    Only a few mail clients support attachment details via the mailto: protocal, and they need them to use the attribute attachment, and also to have a path that's delimited with quotes. The file also needs to exist on the users machine e.g.

    mailto:test@test.com subject=test&attachment=\\usersmachine\test.txt

    Theres some notes and workarounds on this @ http://forums.devshed.com/archive/t-66699 that are probably worth reading.

    Cathal


  • Andrew Whiddett

    Anyone
  • aled

    Thank you for your reply. The first link in your reply is a dead link.

    If i try to have quotes in the path to the attachment i get: "The command line argument is not valid. verify the switch you are using. My default mailprogram is outlook. The file is there and this is for windows forms so I do not think the domain et cetera is somethin I have to worry about.

  • Start default mailprogram with attachment