Open outlook compose window

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MaxH

    Open outlook compose window

    I need to perform programmaticall y from a C# .net application
    the following tasks:
    _ open an outlook (or default e-mail client) compose
    message window;
    _ attach a document to the new message;
    _ set message attributes

    How can I do it?

    Thank you.

    Max

  • Eric Cadwell

    #2
    Re: Open outlook compose window

    To use Outlook add a COM reference to Outlook library. Here's a sample of
    opening a mail item and setting some properties. It exposes an
    Attachments.Add property but I have not tested it.

    private void OutlookMail(str ing Subject, string Body)
    {
    Outlook.Applica tionClass app = new Outlook.Applica tionClass();
    Outlook.NameSpa ceClass ns = (NameSpaceClass )app.GetNamespa ce("mapi");
    ns.Logon("", "", true, true);
    Outlook.MailIte m mi =
    (Outlook.MailIt em)app.CreateIt em(Outlook.OlIt emType.olMailIt em);
    mi.Subject = Subject;
    mi.HTMLBody = Body;
    mi.Display(1); // show it non - modally
    app = null;
    ns.Logoff();
    ns = null;
    mi = null;
    }

    To use the default mail client you can simply execute a mailto: URI.
    System.Diagnost ics.Process.Sta rt(string.Forma t("mailto:{0}?S ubject={1}&Body =
    {2}", To, Subject, Body));
    You can also access the System.Web.Mail instead of using Outlook or find a
    SMAPI wrapper (http://www.codeproject.com/csharp/simplemapidotnet.asp). As
    usual, each method has pros and cons, so check them out and see which fits
    best for your needs.

    Problems I can recall:
    SMAPI - didn't see a way to set HTML format. Doesn't show up in Sent Items.
    System.Web.Mail Message - doesn't open a window to set To and From address,
    would have to set in code or get input from user.
    Outlook - Requires that you reference a COM dll. Distribution issues for
    rich client.
    URL: No attachments, length of URL limited to 255. Cannot invoke Send.

    Be sure to do lots of testing as virus protection or windows security may
    try to block you.

    HTH,
    Eric Cadwell



    Comment

    • MaxH

      #3
      Re: Open outlook compose window

      Thank you very much.

      Still a few comments/questions:

      [color=blue]
      >-----Original Message-----
      >To use Outlook add a COM reference to Outlook library.[/color]
      Here's a sample of[color=blue]
      >opening a mail item and setting some properties. It exposes an
      >Attachments.Ad d property but I have not tested it.
      >
      >private void OutlookMail(str ing Subject, string Body)
      >{
      >Outlook.Applic ationClass app = new Outlook.Applica tionClass();
      >Outlook.NameSp aceClass ns =[/color]
      (NameSpaceClass )app.GetNamespa ce("mapi");[color=blue]
      >ns.Logon("", "", true, true);
      >Outlook.MailIt em mi =
      >(Outlook.MailI tem)app.CreateI tem(Outlook.OlI temType.olMailI tem);
      >mi.Subject = Subject;
      >mi.HTMLBody = Body;
      >mi.Display(1 ); // show it non - modally
      >app = null;
      >ns.Logoff();
      >ns = null;
      >mi = null;
      >}
      >[/color]


      Can I use similar code to access Outlook Express?



      [color=blue]
      >To use the default mail client you can simply execute a[/color]
      mailto: URI.[color=blue]
      >System.Diagnos tics.Process.St art(string.Form at("mailto:{0}? Subject={1}&Bod y=
      >{2}", To, Subject, Body));[/color]


      The above code launches in any case the compose window of
      Outlook Express, even if I work on a system having Netscape
      Mail as the default mail client.

      Besides that (it's not really a big problem since my
      application will probably have to interact with Outlook
      Express and not with Netscape...), I need to directly add
      an attachment to the email message, with no user action...



      Thanks Again.

      Max



      Comment

      Working...