tags:

views:

267

answers:

11

I’m using .NET 3.5, and I want to automatically send a mail. I’m currently using the following:

Microsoft.Office.Interop.Outlook.MailItem mailMsg = 
    (Microsoft.Office.Interop.Outlook.MailItem)outlookApplication.CreateItem(
     Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
mailMsg.To = recipient;
mailMsg.Subject = subject;
mailMsg.Body = body;
mailMsg.Send();

However, I’ve found several articles that seem to imply I should be using the following method:

System.Net.Mail.MailMessage mailMsg = new System.Net.Mail.MailMessage();
mailmsg.To = recipient;
mailmsg.Subject = subject;
mailmsg.Body = body;

Can anyone tell me what the difference between the two namespaces if, and why you might want to use one over the other?

+3  A: 

First one uses COM interop, which is unneeded overhead. Second is pure .net with all it's features. Plus it is more flexible.

Andrey
+10  A: 

The first one, I assume, required Outlook to be installed in the machine so that the Office Interop assemblies are installed. The second one is pure .Net framework.

Colin Desmond
Also it's worth noting that the overhead for instantiating outlook is not trivial.
Paddy
+5  A: 

The first method is using interop by creating an instance of Outlook (outlookApplication) and having that instance of Outlook send the e-mail.

The second is used for sending e-mail over regular old SMTP and doesn't require outlook at all.

Unless you have specific needs for interop there's no need to send an e-mail using outlook (and your code won't work on any machine that doesn't have outlook installed).

Jason Punyon
+1  A: 

The first one is using MS Office which you don't want to publish while System.Net.Mail is available when .Net framework is installed.

rdkleine
+1  A: 

The first example uses libraries installed by the Office Interop Assemblies download.

The second example uses libraries installed by default with the .NET framework, System.Net.

The first example uses Microsoft Interop Libraries. I would go with your second example, since it's part of the default .NET install. The Interop libraries will have more overhead that isn't required as well.

derek
+1  A: 

The Microsoft.Office uses Microsoft Outlook to send the email. It requires Outlook to be installed, and is (at least the last time I tried sending mail this way) more prone to difficulties. (For example, it prompts the user to let them know that a program is attempting to send mail on their behalf, etc.)

The System.Net.Mail just uses pure .NET, and the specified SMTP server to send mail. Trust me.. Avoid using the Office unless there's a need.

David Stratton
There is a app called ClickYes that removes the pesky pop-up (Or clicks it on your behalf). The link is http://www.contextmagic.com/express-clickyes/.I have to say I agree with you 100% on the System.Net.Mail strategy instead of using Outlook or Outlook Plug-Ins. The wrappers drive me around the bend.
Koekiebox
+1  A: 

You need to use the second option any day. It's pure .NET.

If you use first option, i guess Outlook should have been installed in that machine. When you deploy, you will have problems if you don't have MS Office installed in the server.

blntechie
+1  A: 

They are different. MailItem represents message item in Outlook. MailMessage represents an e-mail message that can be sent using the SmtpClient class.

Check MailItem and MailMessage.

Incognito
+1  A: 

First one is using COM Interop and uses Outlook as its base. It needs outlook configured. The second is using SMTP Client. The interop could run you into issues related to outlook, but will allow for some cool features like Opening a Mail Window (but its generally not worth it). The second one will send silent mail, though you can show some window of your own, but still it wont allow flexibility of Outlook Automation. My choice is System.Net.Mail.*.

Sudesh Sawant
+2  A: 

As other have mentioned, the first one uses outlook to send email. The disadvantage is that the user has to have outlook installed; the advantage is that it will look like outlook is sending it.

The second method will try to send mail directly. The advantage is that it doesn't require outlook to be installed, and is a lot less overhead. The disadvantage with this option is that most enterprises these days block port 25, so when you try to send the message, it will fail.

chris
+3  A: 

The second example needs a SMTP server, to make a direct connection, and uses this SMTP server to send the email. It has low overhead, will normally work.

If you need to compose & send an email on behalve of the current user, you could use outlook.

So far I have only seen answers with disadvantages for outlook. But it has a few advantages:

  • You do not have to ask the user for any configuration.
    • Outlook already knows the Exchange / SMTP server,
    • and the email address of the user
  • Email you send will be stored in the sent-items list of the user. So the user can see wat is sent in his name.
  • Add-ons that sign / encrypt outgoing email, or add a standard company disclaimer will be used, so you will follow company policies
  • Can prompt the user if it is allowed to send email (yes, this can be an advantage to)
  • You can choose to only compose the mail, present it to the user. The user can edit and choose to send it or not.

Edit: I use the SMTP method for sending technical emails (like log files & error messages) to our support unit, these mails go out fast and unnoticed.

The Outlook method I use for sending mails on behalve of my user to other humans. These mails are slow, but are trackable, etc.

GvS