views:

876

answers:

3

Hi,

I need some help with .NET (C#) and MS Outlook. I'm building a simple desktop application and want to send an e-mail using Outlook.

  1. If my desktop app generates a message, it should be able to send it as an email via outlook (we can assume outlook is running on the same PC) - a very simple operation.

  2. If I can do 1, that's great. If possible, I would like to be able to insert items to the outlook calendar.

I'm using VS 2008 professional and C#, the target will be .NET 3.5

Any help, sample code is very appreciated.

A: 

Using MAPI, a p/invoke interface makes using something like MailItem.Send Method from C# possible. The mapi32.MAPISendMail page provides the example of setting up the interface:

/// <summary>
/// The MAPISendMail function sends a message.
///
/// This function differs from the MAPISendDocuments function in that it allows greater
/// flexibility in message generation.
/// </summary>
[DllImport("MAPI32.DLL", CharSet=CharSet.Ansi)]
public static extern uint MAPISendMail(IntPtr lhSession, IntPtr ulUIParam,
MapiMessage lpMessage, uint flFlags, uint ulReserved);

The same p/invoke page also provides a warning: Watch it! MAPI32 is not supported from managed code.

You should consider sending mail via SMTP, not outlook, using the native System.Net.Mail.SmtpClient class.

gimel
A: 

I highly recommend using Redemption. It have a very easy-to-use, easy-to-learn API, that can do much more than just sending emails.

SaguiItay
+1  A: 

This code is adopted straight from the MSDN example:

using System.Net;
using System.Net.Mime;
using System.Net.Mail;

...
...

public static void CreateMessageWithAttachment(string server)
{
    // Specify the file to be attached and sent
    string file = @"C:\Temp\data.xls";


    // Create a message and set up the recipients.
    MailMessage message = new MailMessage(
           "[email protected]",
           "[email protected]",
           "Subject: Email message with attachment.",
           "Body: See the attached spreadsheet.");


    // Create the file attachment for this e-mail message.
    Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);


    // Add time stamp information for the file.
    ContentDisposition disposition = data.ContentDisposition;
    disposition.CreationDate = System.IO.File.GetCreationTime(file);
    disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
    disposition.ReadDate = System.IO.File.GetLastAccessTime(file);


    // Add the file attachment...
    message.Attachments.Add(data);


    SmtpClient client = new SmtpClient(server);


    // Add credentials if the SMTP server requires them.
    client.Credentials = CredentialCache.DefaultNetworkCredentials;


    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("CreateMessageWithAttachment() Exception: {0}",
              ex.ToString());
        throw;
    }

}
Mitch Wheat
This will send an e-mail message without using Outlook which was one of his requirements.
Mateo