【问题标题】:Difference between sending a MailMessage to an Exchange Server and sending to an SMTP server将 MailMessage 发送到 Exchange Server 和发送到 SMTP 服务器之间的区别
【发布时间】:2011-07-12 18:04:46
【问题描述】:

我正在尝试使用MailMessage class 构造电子邮件消息,这些消息将传输到 SMTP 服务器以使用 SmtpClient 类进行传递。 我的电子邮件是通过 Exchange 服务器在 Outlook 上配置的。 我对上述实现有以下疑问:

1) Exchange Server 和 SMTP 服务器有什么区别?

2) 就我而言,我的 Outlook 是使用我的凭据在交换服务器上配置的。如何找到 SMTP 地址以便能够实现 MailMessage 类?

3) 如果上述实现技术不可行,有什么想法可以通过基于交换服务器的应用程序发送电子邮件?

我正在使用 Visual Studio 2008,框架 3.5 SP1,使用 C# 作为语言开发 winforms 应用程序。请帮我解开疑惑。

编辑

我正在使用以下代码。它不会抛出任何错误,也不会工作。我正在尝试向自己发送电子邮件,但无济于事

public static void CreateMessageWithAttachment(string server)
    {
        // Specify the file to be attached and sent.
        // This example assumes that a file named Data.xls exists in the
        // current working directory.
        string file = "data.xls";
        // Create a message and set up the recipients.
        MailMessage message = new MailMessage(
           "ben@contoso.com",
           "ben@contoso.com",
           "Quarterly data report.",
           "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 to this e-mail message.
        message.Attachments.Add(data);

        //Send the message.
        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("Exception caught in CreateMessageWithAttachment(): {0}", 
                ex.ToString() );              
        }

        data.Dispose();
    }

【问题讨论】:

  • @Phoenix:contoso.com 是一个示例域。这些电子邮件不是真实的。不需要把它们弄脏。

标签: c# exchange-server smtpclient exchangewebservices


【解决方案1】:

1) Exchange Server 和 SMTP 服务器有什么区别?

Exchange 服务器包含更多内容。

2) 就我而言,我的 Outlook 是使用我的凭据在交换服务器上配置的。如何找到 SMTP 地址以便能够实现 MailMessage 类?

Outlook -> 工具 -> 帐户 -> 编辑帐户。

它与交换服务器的地址相同。端口 25 是标准 SMTP 端口。 Exchange 可能需要身份验证。

3) 如果上述实现技术不可行,有什么想法可以通过基于交换服务器的应用程序发送电子邮件?

你不能只使用MailMessage,你还需要SmtpClient。

使用 Exchange 的示例:Getting a sent MailMessage into the "Sent Folder"

【讨论】:

  • 感谢您的快速回复。我已经用我用来获得解决方案的代码更新了我的问题。请检查您是否可以找出我的代码中的任何错误
  • 1.你试过不带附件吗? 2. 您确定您使用的是正确的服务器地址吗? 3)您是否尝试过手动指定凭据?
  • 所有问题的答案都是肯定的。但是,例如我的交换服务器地址是 abcd.abc.company.com,它不起作用。所以现在我已将其更改为 abcd.abc.company.com:25,以指定 SMTP 端口号。但这也行不通。
  • 是的,你是对的。连接失败。我想我必须找出正确的 smtp 地址。谢谢你的帮助。欣赏它。
【解决方案2】:

SMTP 是一种协议,是一组管理两个系统之间通信的规则。该协议定义了发送邮件的规则。

SMTP 服务器是使用此协议发送邮件的组件(主要是软件)。

MS Exchange 使用 SMTP 发送邮件,但它也为域中的用户管理用户和邮箱。

【讨论】:

    猜你喜欢
    • 2015-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-01
    • 2012-01-14
    • 2017-02-24
    相关资源
    最近更新 更多