首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C# / .NET (VS 2008)对Outlook (2007)的提问

C# / .NET (VS 2008)对Outlook (2007)的提问
EN

Stack Overflow用户
提问于 2008-12-28 10:48:31
回答 3查看 1.3K关注 0票数 1

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

  1. 如果我的桌面应用程序生成一条消息,它应该能够通过outlook发送电子邮件(我们可以假设outlook运行在同一台PC上)--一个非常简单的操作。
  2. 如果我能做到1,那就太好了。如果可能,我希望能够将项目插入到outlook日历中。

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

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

EN

回答 3

Stack Overflow用户

发布于 2008-12-29 00:23:36

此代码是直接从MSDN示例中采用的。

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2008-12-28 11:26:39

使用MAPI,p/invoke接口使使用类似于来自MailItem.Send法的C#的东西成为可能。mapi32.MAPISendMail页面提供了设置接口的示例:

代码语言:javascript
运行
复制
/// <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发送邮件。

票数 1
EN

Stack Overflow用户

发布于 2008-12-28 23:39:43

我强烈建议使用救赎。它有一个非常容易使用,易学的API,它可以做的不仅仅是发送电子邮件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/396069

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档