Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between sending a MailMessage to an Exchange Server and sending to an SMTP server

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();
    }
like image 302
reggie Avatar asked Mar 10 '11 14:03

reggie


Video Answer


2 Answers

1) What is the difference between an Exchange Server and a SMTP server?

Exchange server contains more stuff.

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?

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.

3) Any ideas of sending emails through the application based on the exchange server if the above implementation technique is not feasible?

You can't just use MailMessage, you'll need SmtpClient too.

Example using Exchange: Getting a sent MailMessage into the "Sent Folder"

like image 83
jgauffin Avatar answered Oct 22 '22 09:10

jgauffin


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.

like image 26
Tony The Lion Avatar answered Oct 22 '22 10:10

Tony The Lion