Open outlook compose window

  • Thread starter Thread starter MaxH
  • Start date Start date
M

MaxH

I need to perform programmatically 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
 
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(string Subject, string Body)
{
Outlook.ApplicationClass app = new Outlook.ApplicationClass();
Outlook.NameSpaceClass ns = (NameSpaceClass)app.GetNamespace("mapi");
ns.Logon("", "", true, true);
Outlook.MailItem mi =
(Outlook.MailItem)app.CreateItem(Outlook.OlItemType.olMailItem);
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.Diagnostics.Process.Start(string.Format("mailto:{0}?Subject={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.MailMessage - 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
http://www.origincontrols.com
 
Thank you very much.

Still a few comments/questions:

-----Original Message-----
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(string Subject, string Body)
{
Outlook.ApplicationClass app = new Outlook.ApplicationClass();
Outlook.NameSpaceClass ns = (NameSpaceClass)app.GetNamespace("mapi");
ns.Logon("", "", true, true);
Outlook.MailItem mi =
(Outlook.MailItem)app.CreateItem(Outlook.OlItemType.olMailItem);
mi.Subject = Subject;
mi.HTMLBody = Body;
mi.Display(1); // show it non - modally
app = null;
ns.Logoff();
ns = null;
mi = null;
}


Can I use similar code to access Outlook Express?



To use the default mail client you can simply execute a mailto: URI.
System.Diagnostics.Process.Start(string.Format("mailto:{0}?Subject={1}&Body=
{2}", To, Subject, Body));


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
 
Back
Top