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

I need help with .NET (C #) and MS Outlook. I am creating a simple desktop application and want to send an email using Outlook.

  • If my desktop app generates a message, it should send it as email via Outlook (we can assume Outlook is running on one PC) - a very simple operation.

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

I am using VS 2008 Professional and C #, target will be .NET 3.5

Any help, sample code is much appreciated.

+1


source to share


3 answers


This code is taken 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;
    }

}

      

+2


source


Using MAPI, the p / invoke interface makes it possible to use something like the MailItem.Send Method from C #. The mapi32.MAPISendMail page shows an example of configuring 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);

      



On the same p / invoke page, a warning appears: look! MAPI32 is not supported from managed code.

You should consider sending mail over SMTP rather than Outlook using the native System.Net.Mail.SmtpClient class .

+1


source


I highly recommend using Redemption. It has a very easy to use, easy to learn API that can do a lot more than just send emails.

+1


source







All Articles