1

我需要 .NET (C#) 和 MS Outlook 方面的帮助。我正在构建一个简单的桌面应用程序,并希望使用 Outlook 发送电子邮件。

  1. 如果我的桌面应用程序生成一条消息,它应该能够通过 Outlook 将其作为电子邮件发送(我们可以假设 Outlook 在同一台 PC 上运行)——这是一个非常简单的操作。

  2. 如果我能做到1,那就太好了。如果可能的话,我希望能够将项目插入到 Outlook 日历中。

我正在使用 VS 2008 专业版和 C#,目标是 .NET 3.5

非常感谢任何帮助,示例代码。

4

3 回答 3

2

此代码直接来自MSDN 示例

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;
    }

}
于 2008-12-29T00:23:36.710 回答
1

使用 MAPI,ap/invoke 接口使得在 C# 中使用类似MailItem.Send 方法成为可能。mapi32.MAPISendMail页面提供了设置接口的示例

/// <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);

相同的p/invoke页面还提供了一个警告:注意!托管代码不支持 MAPI32。

您应该考虑使用本机System.Net.Mail.SmtpClient类通过 SMTP 而不是 Outlook 发送邮件。

于 2008-12-28T11:26:39.790 回答
1

我强烈推荐使用 Redemption。它有一个非常易于使用、易于学习的 API,它可以做的不仅仅是发送电子邮件。

于 2008-12-28T23:39:43.257 回答