Fix the MailItem.Send() warning

Sometimes you need to create and send mails in your code. There are many ways to solve this requirement. You can connect to your Exchange with WebServices or you can use a WebService to connect to a SMTP server. But what if you are not able to connect to them, but you know the client is using Outlook. You can call Oulook to send your mails. That is pretty easy all you need is the interop namespace of Outlook (using Oulook = Micosoft.Office.Interop.Outlook;). The code you need for this should contain lines of the following snippet:

1
2
3
4
5
6
// ..call Outlook ..
// create the mailitem
Outlook.MailItem mItem = outlookapplication.CreateItem(Outlook.OlItemType.olMailItem);
// ..set subject, body recipient and much more..
// send the mail
mItem.Send();

Now you got a cool and easy way to send mails, but all Visual Studio is doing is to tell you a warning.
Warning 1 Ambiguity between method 'Microsoft.Office.Interop.Outlook._MailItem.Send()' and non-method 'Microsoft.Office.Interop.Outlook.ItemEvents_10_Event.Send'. Using method group.
Ok.. that looks like Microsoft had problems with some names of methods.. but how to fix it?
There is a fix for this problem, but in my eyes it is not as beautiful as it was before.. all you need to do is to replace the last line with the following:

1
((Outlook._MailItem)mItem).Send();

This line calls the send method as MailItem object. The object is the whole time a MailItem object, but in this case Visual Studio can't make a difference between some different objects.

Comments

Popular posts from this blog

How to support multiple languages in WPF