简体   繁体   中英

A C# / .NET (VS 2008) to Outlook (2007) question

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.

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(
           "from@gmail.com",
           "to@gmail.com",
           "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;
    }

}

Using MAPI, ap/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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM