【发布时间】: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