【问题标题】:A C# / .NET (VS 2008) to Outlook (2007) question一个 C# / .NET (VS 2008) 到 Outlook (2007) 的问题
【发布时间】:2008-12-28 10:48:31
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: c# .net outlook outlook-2007 email


    【解决方案1】:

    此代码直接来自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;
        }
    
    }
    

    【讨论】:

    • 这将在不使用 Outlook 的情况下发送一封电子邮件,这是他的要求之一。
    【解决方案2】:

    使用 MAPI,p/invoke 接口使得使用 C# 中的 MailItem.Send Method 之类的东西成为可能。 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 发送邮件。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-09
        相关资源
        最近更新 更多