I am trying to use the MailMessage class to create email messages that are sent to an SMTP server for delivery using the SmtpClient class. My email is configured in Outlook through the exchange server. I had the following doubts about the above implementation:
1) What is the difference between an Exchange server and an SMTP server?
2) In my case, my appearance is configured on the exchange server using my credentials. How to find the SMTP address so that I can implement the MailMessage class?
3) Any ideas on sending letters through an application based on an exchange server, if the above implementation method is not possible?
I am using Visual studio 2008, framework 3.5 SP1, working on a winforms application with C # as a language. Please help me sort out my doubts.
EDIT
I am using the following code. This does not cause any error, and it does not work. I try to send and email to myself to no avail.
public static void CreateMessageWithAttachment(string server)
{
string file = "data.xls";
MailMessage message = new MailMessage(
"ben@contoso.com",
"ben@contoso.com",
"Quarterly data report.",
"See the attached spreadsheet.");
Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
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);
message.Attachments.Add(data);
SmtpClient client = new SmtpClient(server);
client.Credentials = CredentialCache.DefaultNetworkCredentials;
try {
client.Send(message);
}
catch (Exception ex) {
Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}",
ex.ToString() );
}
data.Dispose();
}
source
share