将 MailMessage 发送到 Exchange 服务器和发送到 SMTP 服务器之间的区别
我正在尝试使用 MailMessage 类 使用 SmtpClient 类构造发送到 SMTP 服务器进行传递的电子邮件消息。 我的电子邮件是通过 Exchange 服务器在 Outlook 上配置的。 我对上述实现有以下疑问:
1)Exchange服务器和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(
"[email protected]",
"[email protected]",
"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();
}
I am trying to use the MailMessage class to construct e-mail messages that are transmitted to an SMTP server for delivery using the SmtpClient class.
My email is configured on outlook through an exchange server.
I had the following doubts with regards to the above implementation:
1) What is the difference between an Exchange Server and a SMTP server?
2) In my case, my outlook is configured on an exchange server using my credentials.How do I find the SMTP address so that i am able to implement the MailMessage Class?
3) Any ideas of sending emails through the application based on the exchange server if the above implementation technique is not feasible?
I am using Visual studio 2008, framework 3.5 SP1, working on winforms application with C# as the language. Please help me clear my doubts.
EDIT
I am using the following code. It doesn't throw any error, neither does it work. I am trying to send and email to myself bu to no avail
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(
"[email protected]",
"[email protected]",
"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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Exchange 服务器包含更多内容。
展望->工具->账户 ->编辑帐户。
它与交换服务器的地址相同。端口 25 是标准 SMTP 端口。
Exchange 可能需要身份验证。
您不能只使用
MailMessage
,您还需要SmtpClient
。使用 Exchange 的示例:将已发送的 MailMessage 放入“已发送文件夹”
Exchange server contains more stuff.
Outlook -> Tools -> Accounts -> Edit account.
It's the same address as the exchange server. Port 25 is the standard SMTP port.
Exchange might need authentication.
You can't just use
MailMessage
, you'll needSmtpClient
too.Example using Exchange: Getting a sent MailMessage into the "Sent Folder"
SMTP 是一种协议,是管理两个系统之间通信的一组规则。该协议定义了发送邮件的规则。
SMTP 服务器是使用该协议发送邮件的组件(主要是软件)。
MS Exchange 使用 SMTP 发送邮件,但它也管理域中用户的用户和邮箱。
SMTP is a protocol, a set of rules governing communication between two systems. This protocol defines the rules for sending mail.
An SMTP server is a component (mostly software) that uses this protocol to send mail.
MS Exchange uses SMTP to send mail, but it also governs users and mailboxes for users on a domain.